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