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