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