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