You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by br...@apache.org on 2005/04/18 09:07:58 UTC

cvs commit: maven-components/maven-plugins/maven-assembly-plugin/src/main/resources/assemblies jar-with-dependencies.xml

brett       2005/04/18 00:07:58

  Modified:    maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly
                        AssemblyMojo.java
               maven-plugins/maven-assembly-plugin/src/main/mdo
                        descriptor.mdo
  Added:       maven-plugins/maven-assembly-plugin/src/main/resources/assemblies
                        jar-with-dependencies.xml
  Log:
  add ability to build a JAR with its dependencies unpacked inside.
  Currently ignores META-INF in dependencies, though should probably merge things like components.xml
  
  Revision  Changes    Path
  1.4       +91 -18    maven-components/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java
  
  Index: AssemblyMojo.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-plugins/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/AssemblyMojo.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AssemblyMojo.java	13 Apr 2005 04:29:25 -0000	1.3
  +++ AssemblyMojo.java	18 Apr 2005 07:07:57 -0000	1.4
  @@ -35,14 +35,19 @@
   import org.codehaus.plexus.util.IOUtil;
   
   import java.io.File;
  +import java.io.FileOutputStream;
   import java.io.FileReader;
  +import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.Reader;
   import java.util.ArrayList;
  +import java.util.Enumeration;
   import java.util.Iterator;
   import java.util.List;
   import java.util.Set;
  +import java.util.jar.JarEntry;
  +import java.util.jar.JarFile;
   
   /**
    * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  @@ -52,6 +57,7 @@
    * @description assemble an application bundle or distribution
    * @parameter name="basedir" type="String" required="true" validator="" expression="#basedir" description=""
    * @parameter name="outputDirectory" type="java.io.File" required="true" validator="" expression="#project.build.directory" description=""
  + * @parameter name="workDirectory" type="java.io.File" required="true" validator="" expression="#project.build.directory/assembly/work" description="Directory to unpack JARs into if needed"
    * @parameter name="descriptor" type="java.io.File" required="false" validator="" expression="#maven.assembly.descriptor" description=""
    * @parameter name="finalName" type="String" required="true" validator="" expression="#project.build.finalName" description=""
    * @parameter name="descriptorId" type="String" required="false" validator="" expression="#maven.assembly.descriptorId" description=""
  @@ -64,10 +70,7 @@
   
       private String basedir;
   
  -    /**
  -     * @todo use java.io.File
  -     */
  -    private String outputDirectory;
  +    private File outputDirectory;
   
       private File descriptor;
   
  @@ -77,6 +80,8 @@
   
       private Set dependencies;
   
  +    private File workDirectory;
  +
       public void execute()
           throws PluginExecutionException
       {
  @@ -136,8 +141,8 @@
                   // TODO: use component roles? Can we do that in a mojo?
                   Archiver archiver = createArchiver( format );
   
  -                processFileSets( archiver, assembly.getFileSets() );
  -                processDependencySets( archiver, assembly.getDependencySets() );
  +                processFileSets( archiver, assembly.getFileSets(), assembly.isIncludeBaseDirectory() );
  +                processDependencySets( archiver, assembly.getDependencySets(), assembly.isIncludeBaseDirectory() );
   
                   archiver.setDestFile( new File( outputDirectory, filename ) );
                   archiver.createArchive();
  @@ -149,14 +154,14 @@
           }
       }
   
  -    private void processDependencySets( Archiver archiver, List dependencySets )
  -        throws ArchiverException
  +    private void processDependencySets( Archiver archiver, List dependencySets, boolean includeBaseDirectory )
  +        throws ArchiverException, IOException
       {
           for ( Iterator i = dependencySets.iterator(); i.hasNext(); )
           {
               DependencySet depedencySet = (DependencySet) i.next();
               String output = depedencySet.getOutputDirectory();
  -            output = getOutputDirectory( output );
  +            output = getOutputDirectory( output, includeBaseDirectory );
   
               AndArtifactFilter filter = new AndArtifactFilter();
               filter.add( new ScopeArtifactFilter( depedencySet.getScope() ) );
  @@ -176,13 +181,68 @@
   
                   if ( filter.include( artifact ) )
                   {
  -                    archiver.addFile( artifact.getFile(), output + artifact.getFile().getName() );
  +                    String name = artifact.getFile().getName();
  +                    if ( depedencySet.isUnpack() )
  +                    {
  +                        // TODO: something like zipfileset in plexus-archiver
  +//                        archiver.addJar(  )
  +
  +                        File tempLocation = new File( workDirectory, name.substring( 0, name.length() - 4 ) );
  +                        boolean process = false;
  +                        if ( !tempLocation.exists() )
  +                        {
  +                            tempLocation.mkdirs();
  +                            process = true;
  +                        }
  +                        else if ( artifact.getFile().lastModified() > tempLocation.lastModified() )
  +                        {
  +                            process = true;
  +                        }
  +
  +                        if ( process )
  +                        {
  +                            unpackJar( artifact.getFile(), tempLocation );
  +                        }
  +                        archiver.addDirectory( tempLocation, null,
  +                                               (String[]) getJarExcludes().toArray( EMPTY_STRING_ARRAY ) );
  +                    }
  +                    else
  +                    {
  +                        archiver.addFile( artifact.getFile(), output + name );
  +                    }
                   }
               }
           }
       }
   
  -    private String getOutputDirectory( String output )
  +    private void unpackJar( File file, File tempLocation )
  +        throws IOException
  +    {
  +        JarFile jar = new JarFile( file );
  +        for ( Enumeration e = jar.entries(); e.hasMoreElements(); )
  +        {
  +            JarEntry entry = (JarEntry) e.nextElement();
  +
  +            if ( entry.isDirectory() )
  +            {
  +                new File( tempLocation, entry.getName() ).mkdir();
  +            }
  +            else
  +            {
  +                IOUtil.copy( jar.getInputStream( entry ),
  +                             new FileOutputStream( new File( tempLocation, entry.getName() ) ) );
  +            }
  +        }
  +    }
  +
  +    private List getJarExcludes()
  +    {
  +        List l = new ArrayList( getDefaultExcludes() );
  +        l.add( "META-INF/**" );
  +        return l;
  +    }
  +
  +    private String getOutputDirectory( String output, boolean includeBaseDirectory )
       {
           if ( output == null )
           {
  @@ -194,13 +254,23 @@
               output += '/';
           }
   
  -        if ( output.startsWith( "/" ) )
  +        if ( includeBaseDirectory )
           {
  -            output = finalName + output;
  +            if ( output.startsWith( "/" ) )
  +            {
  +                output = finalName + output;
  +            }
  +            else
  +            {
  +                output = finalName + "/" + output;
  +            }
           }
           else
           {
  -            output = finalName + "/" + output;
  +            if ( output.startsWith( "/" ) )
  +            {
  +                output = output.substring( 1 );
  +            }
           }
           return output;
       }
  @@ -247,7 +317,10 @@
           else if ( format.startsWith( "jar" ) )
           {
               // TODO: use MavenArchiver for manifest?
  -            archiver = new JarArchiver();
  +            JarArchiver jarArchiver = new JarArchiver();
  +            jarArchiver.setCompress( true );
  +            archiver = jarArchiver;
  +
           }
           else
           {
  @@ -257,7 +330,7 @@
           return archiver;
       }
   
  -    private void processFileSets( Archiver archiver, java.util.List fileSets )
  +    private void processFileSets( Archiver archiver, List fileSets, boolean includeBaseDirecetory )
           throws ArchiverException
       {
           for ( Iterator i = fileSets.iterator(); i.hasNext(); )
  @@ -280,7 +353,7 @@
                       output = directory;
                   }
               }
  -            output = getOutputDirectory( output );
  +            output = getOutputDirectory( output, includeBaseDirecetory );
   
               String[] includes = (String[]) fileSet.getIncludes().toArray( EMPTY_STRING_ARRAY );
               if ( includes.length == 0 )
  
  
  
  1.2       +10 -0     maven-components/maven-plugins/maven-assembly-plugin/src/main/mdo/descriptor.mdo
  
  Index: descriptor.mdo
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-plugins/maven-assembly-plugin/src/main/mdo/descriptor.mdo,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- descriptor.mdo	6 Apr 2005 06:20:07 -0000	1.1
  +++ descriptor.mdo	18 Apr 2005 07:07:57 -0000	1.2
  @@ -30,6 +30,11 @@
             </association>
           </field>
           <field>
  +          <name>includeBaseDirectory</name>
  +          <type>boolean</type>
  +          <defaultValue>true</defaultValue>
  +        </field>
  +        <field>
             <name>fileSets</name>
             <version>1.0.0</version>
             <association>
  @@ -90,6 +95,11 @@
             <type>String</type>
           </field>
           <field>
  +          <name>unpack</name>
  +          <type>boolean</type>
  +          <defaultValue>false</defaultValue>
  +        </field>
  +        <field>
             <name>scope</name>
             <version>1.0.0</version>
             <type>String</type>
  
  
  
  1.1                  maven-components/maven-plugins/maven-assembly-plugin/src/main/resources/assemblies/jar-with-dependencies.xml
  
  Index: jar-with-dependencies.xml
  ===================================================================
  <assembly>
    <!-- TODO: a jarjar format would be better -->
    <id>jar-with-dependencies</id>
    <formats>
      <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
      <fileSet>
        <!-- TODO: use expressions instead: ${project.build.directory}, ${project.build.finalName}, or have a <build /> tag to include the built artifact -->
        <directory>target/classes</directory>
        <outputDirectory>/</outputDirectory>
      </fileSet>
    </fileSets>
    <dependencySets>
      <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>true</unpack>
        <scope>runtime</scope>
      </dependencySet>
    </dependencySets>
  </assembly>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org


[m2] Jar aggregation use case (was RE: cvs commit: maven-components/maven-plugins/maven-assembly-plugin/src/main/resources/assemblies jar-with-dependencies.xml)

Posted by Vincent Massol <vm...@pivolis.com>.
Hi Brett,

> -----Original Message-----
> From: brett@apache.org [mailto:brett@apache.org]
> Sent: lundi 18 avril 2005 09:08
> To: maven-components-cvs@apache.org
> Subject: cvs commit: maven-components/maven-plugins/maven-assembly-
> plugin/src/main/resources/assemblies jar-with-dependencies.xml

[snip]

>   add ability to build a JAR with its dependencies unpacked inside.

Cool. Just a question: In the cargo's build I have the following structure:

cargo/
  |_ core/
    |_ util/
    |_ module/
    |_ container/
  |_ ant/
  |_ samples/
    |_ ant/
    |_ java/

Ideally I would like that the core's pom.xml produces an aggregated jar from
util/, module/ and container/ and that the ant/, samples/ant/ and
samples/java project have a single dependency on this aggregated jar.

How would that work? If I type "m2 install" it will not produce the
aggregated jar as I believe it'll be done during the assembly stage only,
right?

Of course, I could still have the other projects depend on the individual
jars but then it's not as good as they won't exercise the generated jar.

Any idea how this UC could be supported?

[snip]

Thanks
-Vincent

_________________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en fran�ais !
Yahoo! Mail : http://fr.mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@maven.apache.org
For additional commands, e-mail: dev-help@maven.apache.org