brett       2005/04/05 22:11:00

  Modified:    
maven-plugins/maven-assemble-plugin/src/main/java/org/apache/maven/plugin/assemble
                        AssembleMojo.java
               maven-plugins/maven-assemble-plugin/src/main/mdo
                        descriptor.mdo
               maven-plugins/maven-assemble-plugin/src/main/resources/assemblies
                        bin.xml src.xml
               maven-plugins/maven-assemble-plugin pom.xml
  Log:
  add dependency handling, and make bugfixes to the assemble plugin
  
  Revision  Changes    Path
  1.7       +251 -75   
maven-components/maven-plugins/maven-assemble-plugin/src/main/java/org/apache/maven/plugin/assemble/AssembleMojo.java
  
  Index: AssembleMojo.java
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-plugins/maven-assemble-plugin/src/main/java/org/apache/maven/plugin/assemble/AssembleMojo.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AssembleMojo.java 30 Mar 2005 15:58:52 -0000      1.6
  +++ AssembleMojo.java 6 Apr 2005 05:11:00 -0000       1.7
  @@ -16,12 +16,17 @@
    * limitations under the License.
    */
   
  +import org.apache.maven.artifact.Artifact;
  +import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
  +import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
   import org.apache.maven.plugin.AbstractPlugin;
   import org.apache.maven.plugin.PluginExecutionException;
   import org.apache.maven.plugins.assemble.model.Assembly;
  +import org.apache.maven.plugins.assemble.model.DependencySet;
   import org.apache.maven.plugins.assemble.model.FileSet;
   import org.apache.maven.plugins.assemble.model.io.xpp3.AssemblyXpp3Reader;
   import org.codehaus.plexus.archiver.Archiver;
  +import org.codehaus.plexus.archiver.ArchiverException;
   import org.codehaus.plexus.archiver.jar.JarArchiver;
   import org.codehaus.plexus.archiver.tar.TarArchiver;
   import org.codehaus.plexus.archiver.zip.ZipArchiver;
  @@ -32,18 +37,23 @@
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.Reader;
  +import java.util.ArrayList;
   import java.util.Iterator;
  +import java.util.List;
  +import java.util.Set;
   
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]">Brett Porter</a>
    * @version $Id$
    * @goal assemble
  + * @requiresDependencyResolution test
    * @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="descriptor" type="java.io.File" required="false" 
validator="" expression="#maven.assemble.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.assemble.descriptorId" description=""
  + * @parameter name="dependencies" type="java.util.Set" required="false" 
validator="" expression="#project.artifacts" description=""
    */
   public class AssembleMojo
       extends AbstractPlugin
  @@ -63,6 +73,8 @@
   
       private String finalName;
   
  +    private Set dependencies;
  +
       public void execute()
           throws PluginExecutionException
       {
  @@ -108,7 +120,7 @@
               Assembly assembly = reader.read( r );
   
               // TODO: include dependencies marked for distribution under 
certain formats
  -            // TODO: how, might we plugin this into an installer, such as 
NSIS?
  +            // TODO: how, might we plug this into an installer, such as NSIS?
               // TODO: allow file mode specifications?
   
               String fullName = finalName + "-" + assembly.getId();
  @@ -120,96 +132,260 @@
                   String filename = fullName + "." + format;
   
                   // TODO: use component roles? Can we do that in a mojo?
  -                Archiver archiver;
  -                if ( format.startsWith( "tar" ) )
  +                Archiver archiver = createArchiver( format );
  +
  +                processFileSets( archiver, assembly.getFileSets() );
  +                processDependencySets( archiver, 
assembly.getDependencySets() );
  +
  +                archiver.setDestFile( new File( outputDirectory, filename ) 
);
  +                archiver.createArchive();
  +            }
  +        }
  +        finally
  +        {
  +            IOUtil.close( r );
  +        }
  +    }
  +
  +    private void processDependencySets( Archiver archiver, List 
dependencySets )
  +        throws ArchiverException
  +    {
  +        for ( Iterator i = dependencySets.iterator(); i.hasNext(); )
  +        {
  +            DependencySet depedencySet = (DependencySet) i.next();
  +            String output = depedencySet.getOutputDirectory();
  +            output = getOutputDirectory( output );
  +
  +            AndArtifactFilter filter = new AndArtifactFilter();
  +            filter.add( new ScopeArtifactFilter( depedencySet.getScope() ) );
  +            if ( !depedencySet.getIncludes().isEmpty() )
  +            {
  +                filter.add( new IncludesArtifactFilter( 
depedencySet.getIncludes() ) );
  +            }
  +            if ( !depedencySet.getExcludes().isEmpty() )
  +            {
  +                filter.add( new ExcludesArtifactFilter( 
depedencySet.getExcludes() ) );
  +            }
  +
  +            // TODO: includes and excludes
  +            for ( Iterator j = dependencies.iterator(); j.hasNext(); )
  +            {
  +                Artifact artifact = (Artifact) j.next();
  +
  +                if ( filter.include( artifact ) )
                   {
  -                    TarArchiver tarArchiver = new TarArchiver();
  -                    archiver = tarArchiver;
  -                    int index = format.indexOf( '.' );
  -                    if ( index >= 0 )
  -                    {
  -                        // TODO: this needs a cleanup in plexus archiver - 
use a real typesafe enum
  -                        TarArchiver.TarCompressionMethod 
tarCompressionMethod = new TarArchiver.TarCompressionMethod();
  -                        // TODO: this should accept gz and bz2 as well so we 
can skip over the switch
  -                        String compression = format.substring( index + 1 );
  -                        if ( compression.equals( "gz" ) )
  -                        {
  -                            tarCompressionMethod.setValue( "gzip" );
  -                        }
  -                        else if ( compression.equals( "bz2" ) )
  -                        {
  -                            tarCompressionMethod.setValue( "bzip2" );
  -                        }
  -                        else
  -                        {
  -                            // TODO: better handling
  -                            throw new IllegalArgumentException( "Unknown 
compression format: " + compression );
  -                        }
  -                        tarArchiver.setCompression( tarCompressionMethod );
  -                    }
  +                    archiver.addFile( artifact.getFile(), output + 
artifact.getFile().getName() );
                   }
  -                else if ( format.startsWith( "zip" ) )
  +            }
  +        }
  +    }
  +
  +    private String getOutputDirectory( String output )
  +    {
  +        if ( output == null )
  +        {
  +            output = "";
  +        }
  +        if ( !output.endsWith( "/" ) && !output.endsWith( "\\" ) )
  +        {
  +            // TODO: shouldn't archiver do this?
  +            output += '/';
  +        }
  +
  +        if ( output.startsWith( "/" ) )
  +        {
  +            output = finalName + output;
  +        }
  +        else
  +        {
  +            output = finalName + "/" + output;
  +        }
  +        return output;
  +    }
  +
  +    private Archiver createArchiver( String format )
  +        throws ArchiverException
  +    {
  +        Archiver archiver;
  +        if ( format.startsWith( "tar" ) )
  +        {
  +            TarArchiver tarArchiver = new TarArchiver();
  +            archiver = tarArchiver;
  +            int index = format.indexOf( '.' );
  +            if ( index >= 0 )
  +            {
  +                // TODO: this needs a cleanup in plexus archiver - use a 
real typesafe enum
  +                TarArchiver.TarCompressionMethod tarCompressionMethod = new 
TarArchiver.TarCompressionMethod();
  +                // TODO: this should accept gz and bz2 as well so we can 
skip over the switch
  +                String compression = format.substring( index + 1 );
  +                if ( compression.equals( "gz" ) )
                   {
  -                    archiver = new ZipArchiver();
  +                    tarCompressionMethod.setValue( "gzip" );
                   }
  -                else if ( format.startsWith( "jar" ) )
  +                else if ( compression.equals( "bz2" ) )
                   {
  -                    // TODO: use MavenArchiver for manifest?
  -                    archiver = new JarArchiver();
  +                    tarCompressionMethod.setValue( "bzip2" );
                   }
                   else
                   {
                       // TODO: better handling
  -                    throw new IllegalArgumentException( "Unknown format: " + 
format );
  +                    throw new IllegalArgumentException( "Unknown compression 
format: " + compression );
                   }
  +                tarArchiver.setCompression( tarCompressionMethod );
  +            }
  +        }
  +        else if ( format.startsWith( "zip" ) )
  +        {
  +            archiver = new ZipArchiver();
  +        }
  +        else if ( format.startsWith( "jar" ) )
  +        {
  +            // TODO: use MavenArchiver for manifest?
  +            archiver = new JarArchiver();
  +        }
  +        else
  +        {
  +            // TODO: better handling
  +            throw new IllegalArgumentException( "Unknown format: " + format 
);
  +        }
  +        return archiver;
  +    }
   
  -                for ( Iterator j = assembly.getFilesets().iterator(); 
j.hasNext(); )
  +    private void processFileSets( Archiver archiver, java.util.List fileSets 
)
  +        throws ArchiverException
  +    {
  +        for ( Iterator i = fileSets.iterator(); i.hasNext(); )
  +        {
  +            FileSet fileSet = (FileSet) i.next();
  +            String directory = fileSet.getDirectory();
  +            String output = fileSet.getOutputDirectory();
  +            if ( directory == null )
  +            {
  +                directory = basedir;
  +                if ( output == null )
                   {
  -                    FileSet fileset = (FileSet) j.next();
  -                    String directory = fileset.getDirectory();
  -                    String output = fileset.getOutputDirectory();
  -                    if ( directory == null )
  -                    {
  -                        directory = basedir;
  -                        if ( output == null )
  -                        {
  -                            output = "/";
  -                        }
  -                    }
  -                    else
  -                    {
  -                        if ( output == null )
  -                        {
  -                            output = directory;
  -                        }
  -                    }
  -                    if ( !output.endsWith( "/" ) && !output.endsWith( "\\" ) 
)
  -                    {
  -                        // TODO: shouldn't archiver do this?
  -                        output += '/';
  -                    }
  -
  -                    String[] includes = (String[]) 
fileset.getIncludes().toArray( EMPTY_STRING_ARRAY );
  -                    if ( includes.length == 0 )
  -                    {
  -                        includes = null;
  -                    }
  -                    String[] excludes = null;
  -                    if ( !fileset.getExcludes().isEmpty() )
  -                    {
  -                        excludes = (String[]) fileset.getExcludes().toArray( 
EMPTY_STRING_ARRAY );
  -                    }
  -                    // TODO: default excludes?
  -                    archiver.addDirectory( new File( directory ), output, 
includes, excludes );
  +                    output = "";
                   }
  +            }
  +            else
  +            {
  +                if ( output == null )
  +                {
  +                    output = directory;
  +                }
  +            }
  +            output = getOutputDirectory( output );
   
  -                archiver.setDestFile( new File( outputDirectory, filename ) 
);
  -                archiver.createArchive();
  +            String[] includes = (String[]) fileSet.getIncludes().toArray( 
EMPTY_STRING_ARRAY );
  +            if ( includes.length == 0 )
  +            {
  +                includes = null;
               }
  +
  +            List excludesList = fileSet.getExcludes();
  +            excludesList.addAll( getDefaultExcludes() );
  +            String[] excludes = (String[]) excludesList.toArray( 
EMPTY_STRING_ARRAY );
  +
  +            // TODO: default excludes should be in the archiver?
  +            archiver.addDirectory( new File( directory ), output, includes, 
excludes );
           }
  -        finally
  +    }
  +
  +    public List getDefaultExcludes()
  +    {
  +        List defaultExcludes = new ArrayList();
  +        defaultExcludes.add( "**/*~" );
  +        defaultExcludes.add( "**/#*#" );
  +        defaultExcludes.add( "**/.#*" );
  +        defaultExcludes.add( "**/%*%" );
  +        defaultExcludes.add( "**/._*" );
  +
  +        // CVS
  +        defaultExcludes.add( "**/CVS" );
  +        defaultExcludes.add( "**/CVS/**" );
  +        defaultExcludes.add( "**/.cvsignore" );
  +
  +        // SCCS
  +        defaultExcludes.add( "**/SCCS" );
  +        defaultExcludes.add( "**/SCCS/**" );
  +
  +        // Visual SourceSafe
  +        defaultExcludes.add( "**/vssver.scc" );
  +
  +        // Subversion
  +        defaultExcludes.add( "**/.svn" );
  +        defaultExcludes.add( "**/.svn/**" );
  +
  +        // Mac
  +        defaultExcludes.add( "**/.DS_Store" );
  +
  +        return defaultExcludes;
  +    }
  +
  +    // TODO: move to maven-artifact - generally useful
  +    private static class AndArtifactFilter
  +        implements ArtifactFilter
  +    {
  +        private final List filters = new ArrayList();
  +
  +        public boolean include( Artifact artifact )
           {
  -            IOUtil.close( r );
  +            boolean include = true;
  +            for ( Iterator i = filters.iterator(); i.hasNext() && include; )
  +            {
  +                ArtifactFilter filter = (ArtifactFilter) i.next();
  +                if ( !filter.include( artifact ) )
  +                {
  +                    include = false;
  +                }
  +            }
  +            return include;
  +        }
  +
  +        public void add( ArtifactFilter artifactFilter )
  +        {
  +            filters.add( artifactFilter );
  +        }
  +    }
  +
  +    private static class IncludesArtifactFilter
  +        implements ArtifactFilter
  +    {
  +        private final List patterns;
  +
  +        public IncludesArtifactFilter( List patterns )
  +        {
  +            this.patterns = patterns;
  +        }
  +
  +        public boolean include( Artifact artifact )
  +        {
  +            String id = artifact.getGroupId() + ":" + 
artifact.getArtifactId();
  +
  +            boolean matched = false;
  +            for ( Iterator i = patterns.iterator(); i.hasNext() & !matched; )
  +            {
  +                // TODO: what about wildcards? Just specifying groups? 
versions?
  +                if ( id.equals( i.next() ) )
  +                {
  +                    matched = true;
  +                }
  +            }
  +            return matched;
  +        }
  +    }
  +
  +    private static class ExcludesArtifactFilter
  +        extends IncludesArtifactFilter
  +    {
  +        public ExcludesArtifactFilter( List patterns )
  +        {
  +            super( patterns );
  +        }
  +
  +        public boolean include( Artifact artifact )
  +        {
  +            return !super.include( artifact );
           }
       }
   }
  
  
  
  1.3       +43 -2     
maven-components/maven-plugins/maven-assemble-plugin/src/main/mdo/descriptor.mdo
  
  Index: descriptor.mdo
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-plugins/maven-assemble-plugin/src/main/mdo/descriptor.mdo,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- descriptor.mdo    21 Mar 2005 07:23:48 -0000      1.2
  +++ descriptor.mdo    6 Apr 2005 05:11:00 -0000       1.3
  @@ -30,14 +30,21 @@
             </association>
           </field>
           <field>
  -          <name>filesets</name>
  +          <name>fileSets</name>
             <version>1.0.0</version>
  -          <required>true</required>
             <association>
               <type>FileSet</type>
               <multiplicity>*</multiplicity>
             </association>
           </field>
  +        <field>
  +          <name>dependencySets</name>
  +          <version>1.0.0</version>
  +          <association>
  +            <type>DependencySet</type>
  +            <multiplicity>*</multiplicity>
  +          </association>
  +        </field>
         </fields>
       </class>
       <class>
  @@ -73,6 +80,40 @@
           </field>
         </fields>
       </class>
  +    <class>
  +      <name>DependencySet</name>
  +      <version>1.0.0</version>
  +      <fields>
  +        <field>
  +          <name>outputDirectory</name>
  +          <version>1.0.0</version>
  +          <type>String</type>
  +        </field>
  +        <field>
  +          <name>scope</name>
  +          <version>1.0.0</version>
  +          <type>String</type>
  +          <defaultValue>runtime</defaultValue>
  +          <required>true</required>
  +        </field>
  +        <field>
  +          <name>includes</name>
  +          <version>1.0.0</version>
  +          <association>
  +            <type>String</type>
  +            <multiplicity>*</multiplicity>
  +          </association>
  +        </field>
  +        <field>
  +          <name>excludes</name>
  +          <version>1.0.0</version>
  +          <association>
  +            <type>String</type>
  +            <multiplicity>*</multiplicity>
  +          </association>
  +        </field>
  +      </fields>
  +    </class>
     </classes>
   </model>
   
  
  
  
  1.2       +7 -7      
maven-components/maven-plugins/maven-assemble-plugin/src/main/resources/assemblies/bin.xml
  
  Index: bin.xml
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-plugins/maven-assemble-plugin/src/main/resources/assemblies/bin.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- bin.xml   30 Mar 2005 15:41:23 -0000      1.1
  +++ bin.xml   6 Apr 2005 05:11:00 -0000       1.2
  @@ -5,22 +5,22 @@
       <format>tar.bz2</format>
       <format>zip</format>
     </formats>
  -  <filesets>
  -    <fileset>
  +  <fileSets>
  +    <fileSet>
         <includes>
           <include>README*</include>
           <include>LICENSE*</include>
           <include>NOTICE*</include>
         </includes>
  -    </fileset>
  +    </fileSet>
       <!-- TODO: docs? -->
  -    <fileset>
  +    <fileSet>
         <!-- TODO: use expresssions instead: ${project.build.directory}, 
${project.build.finalName} -->
         <directory>target</directory>
  -      <outputDirectory>/</outputDirectory>
  +      <outputDirectory></outputDirectory>
         <includes>
           <include>*.jar</include>
         </includes>
  -    </fileset>
  -  </filesets>
  +    </fileSet>
  +  </fileSets>
   </assembly>
  
  
  
  1.2       +6 -6      
maven-components/maven-plugins/maven-assemble-plugin/src/main/resources/assemblies/src.xml
  
  Index: src.xml
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-plugins/maven-assemble-plugin/src/main/resources/assemblies/src.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- src.xml   30 Mar 2005 15:58:52 -0000      1.1
  +++ src.xml   6 Apr 2005 05:11:00 -0000       1.2
  @@ -5,18 +5,18 @@
       <format>tar.bz2</format>
       <format>zip</format>
     </formats>
  -  <filesets>
  -    <fileset>
  +  <fileSets>
  +    <fileSet>
         <includes>
           <include>README*</include>
           <include>LICENSE*</include>
           <include>NOTICE*</include>
           <include>pom.xml</include>
         </includes>
  -    </fileset>
  -    <fileset>
  +    </fileSet>
  +    <fileSet>
         <!-- TODO: use expresssions instead: ${project.build.sourceDirectory}, 
etc -->
         <directory>src</directory>
  -    </fileset>
  -  </filesets>
  +    </fileSet>
  +  </fileSets>
   </assembly>
  
  
  
  1.9       +5 -0      
maven-components/maven-plugins/maven-assemble-plugin/pom.xml
  
  Index: pom.xml
  ===================================================================
  RCS file: 
/home/cvs/maven-components/maven-plugins/maven-assemble-plugin/pom.xml,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- pom.xml   6 Apr 2005 01:49:06 -0000       1.8
  +++ pom.xml   6 Apr 2005 05:11:00 -0000       1.9
  @@ -11,6 +11,11 @@
     <version>1.0-SNAPSHOT</version>
     <dependencies>
       <dependency>
  +      <groupId>org.apache.maven</groupId>
  +      <artifactId>maven-artifact</artifactId>
  +      <version>2.0-SNAPSHOT</version>
  +    </dependency>
  +    <dependency>
         <groupId>plexus</groupId>
         <artifactId>plexus-archiver</artifactId>
         <version>1.0-alpha-1</version>
  
  
  

Reply via email to