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