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 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.plugin.war.Overlay;
31 import org.codehaus.plexus.interpolation.InterpolationException;
32
33
34
35
36
37
38
39
40 public class ArtifactsPackagingTask
41 extends AbstractWarPackagingTask
42 {
43
44 public static final String TLD_PATH = "WEB-INF/tld/";
45
46 public static final String SERVICES_PATH = "WEB-INF/services/";
47
48 public static final String MODULES_PATH = "WEB-INF/modules/";
49
50 public static final String EXTENSIONS_PATH = "WEB-INF/extensions/";
51
52 private final Set<Artifact> artifacts;
53
54 private final String id;
55
56
57 public ArtifactsPackagingTask( Set<Artifact> artifacts, Overlay currentProjectOverlay )
58 {
59 this.artifacts = artifacts;
60 this.id = currentProjectOverlay.getId();
61 }
62
63
64 public void performPackaging( WarPackagingContext context )
65 throws MojoExecutionException
66 {
67 try
68 {
69 final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME );
70 final List<String> duplicates = findDuplicates( context, artifacts );
71
72 for ( Artifact artifact : artifacts )
73 {
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 ( "xar".equals( type ) )
104 {
105 copyFile( id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName );
106 }
107 else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
108 || "test-jar".equals( type ) || "bundle".equals( type ) )
109 {
110 copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
111 }
112 else if ( "par".equals( type ) )
113 {
114 targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
115 copyFile( id, context, artifact.getFile(), LIB_PATH + targetFileName );
116 }
117 else if ( "war".equals( type ) )
118 {
119
120 context.getLog().debug( "war artifacts are handled as overlays, ignoring [" + artifact + "]" );
121 }
122 else if ( "zip".equals( type ) )
123 {
124
125 context.getLog().debug( "zip artifacts are handled as overlays, ignoring [" + artifact + "]" );
126 }
127 else
128 {
129 context.getLog().debug(
130 "Artifact of type [" + type + "] is not supported, ignoring [" + artifact + "]" );
131 }
132 }
133 catch ( IOException e )
134 {
135 throw new MojoExecutionException( "Failed to copy file for artifact [" + artifact + "]", e );
136 }
137 }
138 }
139 }
140 catch ( InterpolationException e )
141 {
142 throw new MojoExecutionException( e.getMessage(), e );
143 }
144 }
145
146
147
148
149
150
151
152
153
154 private List<String> findDuplicates( WarPackagingContext context, Set<Artifact> artifacts )
155 throws InterpolationException
156 {
157 List<String> duplicates = new ArrayList<String>();
158 List<String> identifiers = new ArrayList<String>();
159 for ( Artifact artifact : artifacts )
160 {
161 String candidate = getArtifactFinalName( context, artifact );
162 if ( identifiers.contains( candidate ) )
163 {
164 duplicates.add( candidate );
165 }
166 else
167 {
168 identifiers.add( candidate );
169 }
170 }
171 return duplicates;
172 }
173 }