View Javadoc

1   package org.alfresco.maven.plugin.amp.packaging;
2   
3   import org.apache.maven.artifact.Artifact;
4   import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
5   import org.apache.maven.plugin.MojoExecutionException;
6   import org.alfresco.maven.plugin.amp.Overlay;
7   
8   import java.io.IOException;
9   import java.util.ArrayList;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.Set;
13  
14  /***
15   * Handles the artifacts that needs to be packaged in the web application.
16   *
17   * @author Stephane Nicoll
18   */
19  public class ArtifactsPackagingTask
20      extends AbstractAmpPackagingTask
21  {
22  
23  	public static final String LIB_PATH = "lib/";
24  	
25      private final Set artifacts;
26  
27      private final String id;
28  
29  
30      public ArtifactsPackagingTask( Set artifacts )
31      {
32          this.artifacts = artifacts;
33          this.id = Overlay.currentProjectInstance().getId();
34      }
35  
36  
37      public void performPackaging( AmpPackagingContext context )
38          throws MojoExecutionException
39      {
40  
41          final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
42          final List duplicates = findDuplicates( context, artifacts );
43  
44          for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
45          {
46              Artifact artifact = (Artifact) iter.next();
47              String targetFileName = getArtifactFinalName( context, artifact );
48  
49              context.getLog().debug( "Processing: " + targetFileName );
50  
51              if ( duplicates.contains( targetFileName ) )
52              {
53                  context.getLog().debug( "Duplicate found: " + targetFileName );
54                  targetFileName = artifact.getGroupId() + "-" + targetFileName;
55                  context.getLog().debug( "Renamed to: " + targetFileName );
56              }
57  
58              if ( !artifact.isOptional() && filter.include( artifact ) )
59              {
60                  try
61                  {
62                      String type = artifact.getType();
63                      if ( "tld".equals( type ) )
64                      {
65                        //  copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
66                      }
67                      else if ( "aar".equals( type ) )
68                      {
69                          //copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
70                      }
71                      else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) ||
72                          "test-jar".equals( type ) )
73                      {
74                          copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
75                          context.getLog().debug("addng  " + artifact.getId() + " to AMP in " + LIB_PATH );
76                      }
77                      else if ( "par".equals( type ) )
78                      {
79                          targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
80                          copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );                        
81                      }
82                      else if ( "war".equals( type ) )
83                      {
84                          // Nothing to do here, it is an overlay and it's already handled
85                      }
86                      else if ("zip".equals( type ))
87                      {
88                          // Nothing to do here, it is an overlay and it's already handled
89                      }
90                      else if ("amp".equals( type ))
91                      {
92                          context.getLog().debug("skipping " + artifact.getId() + " in artifacts packaging phase. Will be processed in overlay");
93                      }
94                      else
95                      {
96                          context.getLog().debug(
97                              "Artifact of type[" + type + "] is not supported, ignoring[" + artifact + "]" );
98                      }
99                  }
100                 catch ( IOException e )
101                 {
102                     throw new MojoExecutionException( "Failed to copy file for artifact[" + artifact + "]", e );
103                 }
104             }
105         }
106     }
107 
108     /***
109      * Searches a set of artifacts for duplicate filenames and returns a list
110      * of duplicates.
111      *
112      * @param context   the packaging context
113      * @param artifacts set of artifacts
114      * @return List of duplicated artifacts as bundling file names
115      */
116     private List findDuplicates( AmpPackagingContext context, Set artifacts )
117     {
118         List duplicates = new ArrayList();
119         List identifiers = new ArrayList();
120         for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
121         {
122             Artifact artifact = (Artifact) iter.next();
123             String candidate = getArtifactFinalName( context, artifact );
124             if ( identifiers.contains( candidate ) )
125             {
126                 duplicates.add( candidate );
127             }
128             else
129             {
130                 identifiers.add( candidate );
131             }
132         }
133         return duplicates;
134     }
135 }