1 package org.apache.maven.plugin.war.packaging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.war.Overlay;
26 import org.codehaus.plexus.interpolation.InterpolationException;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Set;
33
34
35
36
37
38
39
40
41 public class ArtifactsPackagingTask
42 extends AbstractWarPackagingTask
43 {
44
45 public static final String TLD_PATH = "WEB-INF/tld/";
46
47 public static final String SERVICES_PATH = "WEB-INF/services/";
48
49 public static final String MODULES_PATH = "WEB-INF/modules/";
50
51 private final Set artifacts;
52
53 private final String id;
54
55
56 public ArtifactsPackagingTask( Set artifacts, Overlay currentProjectOverlay )
57 {
58 this.artifacts = artifacts;
59 this.id = currentProjectOverlay.getId();
60 }
61
62
63 public void performPackaging( WarPackagingContext context )
64 throws MojoExecutionException
65 {
66 try
67 {
68 final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
69 final List duplicates = findDuplicates( context, artifacts );
70
71 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
72 {
73 Artifact artifact = (Artifact) iter.next();
74 String targetFileName = getArtifactFinalName( context, artifact );
75
76 context.getLog().debug( "Processing: " + targetFileName );
77
78 if ( duplicates.contains( targetFileName ) )
79 {
80 context.getLog().debug( "Duplicate found: " + targetFileName );
81 targetFileName = artifact.getGroupId() + "-" + targetFileName;
82 context.getLog().debug( "Renamed to: " + targetFileName );
83 }
84 context.getWebappStructure().registerTargetFileName( artifact, targetFileName );
85
86 if ( !artifact.isOptional() && filter.include( artifact ) )
87 {
88 try
89 {
90 String type = artifact.getType();
91 if ( "tld".equals( type ) )
92 {
93 copyFile( id, context, artifact.getFile(), TLD_PATH + targetFileName );
94 }
95 else if ( "aar".equals( type ) )
96 {
97 copyFile( id, context, artifact.getFile(), SERVICES_PATH + targetFileName );
98 }
99 else if ( "mar".equals( type ) )
100 {
101 copyFile( id, context, artifact.getFile(), MODULES_PATH + targetFileName );
102 }
103 else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
104 || "test-jar".equals( type ) )
105 {
106 copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
107 }
108 else if ( "par".equals( type ) )
109 {
110 targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
111 copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
112 }
113 else if ( "war".equals( type ) )
114 {
115
116 context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact + "]" );
117 }
118 else if ( "zip".equals( type ) )
119 {
120
121 context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact + "]" );
122 }
123 else
124 {
125 context.getLog().debug(
126 "Artifact of type [" + type + "] is not supported, ignoring [" + artifact + "]" );
127 }
128 }
129 catch ( IOException e )
130 {
131 throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
132 }
133 }
134 }
135 }
136 catch ( InterpolationException e )
137 {
138 throw new MojoExecutionException( e.getMessage(), e );
139 }
140 }
141
142
143
144
145
146
147
148
149
150 private List findDuplicates( WarPackagingContext context, Set artifacts )
151 throws InterpolationException
152 {
153 List duplicates = new ArrayList();
154 List identifiers = new ArrayList();
155 for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
156 {
157 Artifact artifact = (Artifact) iter.next();
158 String candidate = getArtifactFinalName( context, artifact );
159 if ( identifiers.contains( candidate ) )
160 {
161 duplicates.add( candidate );
162 }
163 else
164 {
165 identifiers.add( candidate );
166 }
167 }
168 return duplicates;
169 }
170 }