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