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.model.Dependency; |
24 | |
import org.apache.maven.plugin.MojoExecutionException; |
25 | |
import org.apache.maven.plugin.MojoFailureException; |
26 | |
import org.apache.maven.plugin.war.util.WebappStructure; |
27 | |
|
28 | |
import java.io.File; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 24 | public class DependenciesAnalysisPackagingTask |
39 | |
extends AbstractWarPackagingTask |
40 | |
{ |
41 | |
|
42 | |
public void performPackaging( final WarPackagingContext context ) |
43 | |
throws MojoExecutionException, MojoFailureException |
44 | |
{ |
45 | |
|
46 | 24 | context.getWebappStructure().analyseDependencies( new DependenciesAnalysisCallbackImpl( context ) ); |
47 | |
|
48 | 24 | } |
49 | |
|
50 | |
protected void handleDependency( WarPackagingContext context, Dependency dependency, String notBundledMessage, |
51 | |
String warOrZipMessage, String standardMessage, boolean removeFile ) |
52 | |
{ |
53 | 6 | if ( Artifact.SCOPE_PROVIDED.equals( dependency.getScope() ) |
54 | |
|| Artifact.SCOPE_TEST.equals( dependency.getScope() ) || dependency.isOptional() ) |
55 | |
{ |
56 | 0 | context.getLog().debug( notBundledMessage ); |
57 | |
} |
58 | 6 | else if ( "war".equals( dependency.getType() ) || "zip".equals( dependency.getType() ) ) |
59 | |
{ |
60 | 2 | context.getLog().warn( warOrZipMessage ); |
61 | |
} |
62 | 4 | else if ( "tld".equals( dependency.getType() ) || "aar".equals( dependency.getType() ) |
63 | |
|| "mar".equals( dependency.getType() ) || "xar".equals( dependency.getType() ) |
64 | |
|| "jar".equals( dependency.getType() ) || "ejb".equals( dependency.getType() ) |
65 | |
|| "ejb-client".equals( dependency.getType() ) || "test-jar".equals( dependency.getType() ) |
66 | |
|| "par".equals( dependency.getType() ) ) |
67 | |
{ |
68 | 4 | context.getLog().info( standardMessage ); |
69 | 4 | if ( removeFile ) |
70 | |
{ |
71 | 4 | removeDependency( context, dependency ); |
72 | |
} |
73 | |
} |
74 | 6 | } |
75 | |
|
76 | |
protected void handleDependencyScope( WarPackagingContext context, Dependency dependency, String warOrZipMessage, |
77 | |
String standardMessage, boolean removeFile ) |
78 | |
{ |
79 | 2 | if ( "war".equals( dependency.getType() ) || "zip".equals( dependency.getType() ) ) |
80 | |
{ |
81 | 0 | context.getLog().warn( warOrZipMessage ); |
82 | |
} |
83 | 2 | else if ( "tld".equals( dependency.getType() ) || "aar".equals( dependency.getType() ) |
84 | |
|| "mar".equals( dependency.getType() ) || "xar".equals( dependency.getType() ) |
85 | |
|| "jar".equals( dependency.getType() ) || "ejb".equals( dependency.getType() ) |
86 | |
|| "ejb-client".equals( dependency.getType() ) || "test-jar".equals( dependency.getType() ) |
87 | |
|| "par".equals( dependency.getType() ) ) |
88 | |
{ |
89 | 2 | context.getLog().info( standardMessage ); |
90 | 2 | if ( removeFile ) |
91 | |
{ |
92 | 2 | removeDependency( context, dependency ); |
93 | |
} |
94 | |
} |
95 | 2 | } |
96 | |
|
97 | |
private void removeDependency( WarPackagingContext context, Dependency dependency ) |
98 | |
{ |
99 | 6 | final String targetFileName = context.getWebappStructure().getCachedTargetFileName( dependency ); |
100 | 6 | if ( targetFileName != null ) |
101 | |
{ |
102 | 6 | final String type = dependency.getType(); |
103 | 6 | File targetFile = null; |
104 | 6 | if ( "tld".equals( type ) ) |
105 | |
{ |
106 | 0 | targetFile = new File( context.getWebappDirectory(), ArtifactsPackagingTask.TLD_PATH + targetFileName ); |
107 | |
} |
108 | 6 | else if ( "aar".equals( type ) ) |
109 | |
{ |
110 | 0 | targetFile = |
111 | |
new File( context.getWebappDirectory(), ArtifactsPackagingTask.SERVICES_PATH + targetFileName ); |
112 | |
} |
113 | 6 | else if ( "mar".equals( type ) ) |
114 | |
{ |
115 | 0 | targetFile = |
116 | |
new File( context.getWebappDirectory(), ArtifactsPackagingTask.MODULES_PATH + targetFileName ); |
117 | |
} |
118 | 6 | else if ( "xar".equals( type ) ) |
119 | |
{ |
120 | 0 | targetFile = |
121 | |
new File( context.getWebappDirectory(), ArtifactsPackagingTask.EXTENSIONS_PATH + targetFileName ); |
122 | |
} |
123 | 6 | else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) |
124 | |
|| "test-jar".equals( type ) ) |
125 | |
{ |
126 | 6 | targetFile = new File( context.getWebappDirectory(), LIB_PATH + targetFileName ); |
127 | |
} |
128 | 0 | else if ( "par".equals( type ) ) |
129 | |
{ |
130 | 0 | String targetFileName2 = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar"; |
131 | 0 | targetFile = new File( context.getWebappDirectory(), LIB_PATH + targetFileName2 ); |
132 | |
} |
133 | |
|
134 | |
|
135 | 6 | if ( targetFile == null ) |
136 | |
{ |
137 | 0 | context.getLog().error( "Could not get file from dependency [" + dependency + "]" ); |
138 | |
} |
139 | 6 | else if ( targetFile.exists() ) |
140 | |
{ |
141 | 6 | context.getLog().debug( "Removing file [" + targetFile.getAbsolutePath() + "]" ); |
142 | 6 | targetFile.delete(); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | context.getLog().warn( "File to remove [" + targetFile.getAbsolutePath() + "] has not been found" ); |
147 | |
} |
148 | 6 | } |
149 | |
else |
150 | |
{ |
151 | 0 | context.getLog().warn( "Could not retrieve the target file name of dependency [" + dependency + "]" ); |
152 | |
} |
153 | 6 | } |
154 | |
|
155 | |
|
156 | 24 | class DependenciesAnalysisCallbackImpl |
157 | |
implements WebappStructure.DependenciesAnalysisCallback |
158 | |
{ |
159 | |
private final WarPackagingContext context; |
160 | |
|
161 | |
DependenciesAnalysisCallbackImpl( WarPackagingContext context ) |
162 | 24 | { |
163 | 24 | this.context = context; |
164 | 24 | } |
165 | |
|
166 | |
public void unchangedDependency( Dependency dependency ) |
167 | |
{ |
168 | 22 | context.getLog().debug( "Dependency [" + dependency + "] has not changed since last build." ); |
169 | 22 | } |
170 | |
|
171 | |
public void newDependency( Dependency dependency ) |
172 | |
{ |
173 | 2 | context.getLog().debug( "New dependency [" + dependency + "]." ); |
174 | 2 | } |
175 | |
|
176 | |
public void removedDependency( Dependency dependency ) |
177 | |
{ |
178 | 4 | handleDependency( context, dependency, "Dependency [" + dependency |
179 | |
+ "] has been removed from the project but it was not bundled anyway.", "Dependency [" + dependency |
180 | |
+ "] has been removed from the project. If it was included in the build as an overlay, " |
181 | |
+ "consider cleaning the target directory of the project (mvn clean)", "Dependency [" + dependency |
182 | |
+ "] has been removed from the project.", true ); |
183 | 4 | } |
184 | |
|
185 | |
public void updatedVersion( Dependency dependency, String previousVersion ) |
186 | |
{ |
187 | 2 | handleDependency( context, dependency, "Version of dependency [" + dependency + "] has changed (" |
188 | |
+ previousVersion + " -> " + dependency.getVersion() + ") but it was not bundled anyway.", |
189 | |
"Version of dependency [" + dependency + "] has changed (" |
190 | |
+ previousVersion + " -> " + dependency.getVersion() |
191 | |
+ "). If it was included in the build as an overlay, " |
192 | |
+ "consider " |
193 | |
+ "cleaning the target directory of the project (mvn clean)", |
194 | |
"Version of dependency [" + dependency + "] has changed (" |
195 | |
+ previousVersion + " -> " + dependency.getVersion() + ").", |
196 | |
true ); |
197 | 2 | } |
198 | |
|
199 | |
public void updatedScope( Dependency dependency, String previousScope ) |
200 | |
{ |
201 | 2 | if ( Artifact.SCOPE_PROVIDED.equals( dependency.getScope() ) || Artifact.SCOPE_TEST.equals( |
202 | |
dependency.getScope() ) |
203 | |
&& ( !Artifact.SCOPE_PROVIDED.equals( previousScope ) |
204 | |
&& !Artifact.SCOPE_TEST.equals( previousScope ) ) ) |
205 | |
{ |
206 | |
|
207 | 2 | handleDependencyScope( context, dependency, "Scope of dependency [" + dependency + "] has changed (" |
208 | |
+ previousScope + " -> " + dependency.getScope() |
209 | |
+ "). If it was included in the build as an overlay, " |
210 | |
+ "consider cleaning the target directory of the project (mvn clean)", "Scope of dependency [" |
211 | |
+ dependency + "] has changed (" + previousScope + " -> " + dependency.getScope() + ").", true ); |
212 | |
} |
213 | |
|
214 | 2 | } |
215 | |
|
216 | |
|
217 | |
public void updatedOptionalFlag( Dependency dependency, boolean previousOptional ) |
218 | |
{ |
219 | 0 | if ( !previousOptional && dependency.isOptional() ) |
220 | |
{ |
221 | |
|
222 | 0 | handleDependency( context, dependency, |
223 | |
"Dependency [" + dependency + "] is now optional but it was not bundled anyway.", |
224 | |
"Dependency [" + dependency |
225 | |
+ "] is now optional. If it was included in the build as an overlay, " |
226 | |
+ "consider cleaning the target directory of the project (mvn clean)", |
227 | |
"Dependency [" + dependency + "] is now optional", true ); |
228 | |
|
229 | |
|
230 | |
} |
231 | 0 | } |
232 | |
|
233 | |
public void updatedUnknown( Dependency dependency, Dependency previousDep ) |
234 | |
{ |
235 | 0 | handleDependency( context, dependency, "Dependency [" + dependency + "] has changed (was " + previousDep |
236 | |
+ ") but it was not bundled anyway.", "Dependency [" + dependency + "] has changed (was " + previousDep |
237 | |
+ "). If it was included in the build as an overlay, " + "consider " |
238 | |
+ "cleaning the target directory of the project (mvn clean)", "Dependency [" + dependency |
239 | |
+ "] has changed (was " + previousDep + ").", true ); |
240 | 0 | } |
241 | |
|
242 | |
} |
243 | |
|
244 | |
} |