View Javadoc

1   package org.apache.maven.plugin.war.packaging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Analyzes the dependencies of the project with its previous state and update
32   * the target directory accordingly.
33   *
34   * @author Stephane Nicoll
35   * 
36   * @version $Id: DependenciesAnalysisPackagingTask.html 867902 2013-06-30 13:58:43Z olamy $
37   */
38  public class DependenciesAnalysisPackagingTask
39      extends AbstractWarPackagingTask
40  {
41  
42      public void performPackaging( final WarPackagingContext context )
43          throws MojoExecutionException, MojoFailureException
44      {
45  
46          context.getWebappStructure().analyseDependencies( new DependenciesAnalysisCallbackImpl( context ) );
47  
48      }
49  
50      protected void handleDependency( WarPackagingContext context, Dependency dependency, String notBundledMessage,
51                                       String warOrZipMessage, String standardMessage, boolean removeFile )
52      {
53          if ( Artifact.SCOPE_PROVIDED.equals( dependency.getScope() )
54              || Artifact.SCOPE_TEST.equals( dependency.getScope() ) || dependency.isOptional() )
55          {
56              context.getLog().debug( notBundledMessage );
57          }
58          else if ( "war".equals( dependency.getType() ) || "zip".equals( dependency.getType() ) )
59          {
60              context.getLog().warn( warOrZipMessage );
61          }
62          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              context.getLog().info( standardMessage );
69              if ( removeFile )
70              {
71                  removeDependency( context, dependency );
72              }
73          }
74      }
75  
76      protected void handleDependencyScope( WarPackagingContext context, Dependency dependency, String warOrZipMessage,
77                                            String standardMessage, boolean removeFile )
78      {
79          if ( "war".equals( dependency.getType() ) || "zip".equals( dependency.getType() ) )
80          {
81              context.getLog().warn( warOrZipMessage );
82          }
83          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              context.getLog().info( standardMessage );
90              if ( removeFile )
91              {
92                  removeDependency( context, dependency );
93              }
94          }
95      }
96  
97      private void removeDependency( WarPackagingContext context, Dependency dependency )
98      {
99          final String targetFileName = context.getWebappStructure().getCachedTargetFileName( dependency );
100         if ( targetFileName != null )
101         {
102             final String type = dependency.getType();
103             File targetFile = null;
104             if ( "tld".equals( type ) )
105             {
106                 targetFile = new File( context.getWebappDirectory(), ArtifactsPackagingTask.TLD_PATH + targetFileName );
107             }
108             else if ( "aar".equals( type ) )
109             {
110                 targetFile =
111                     new File( context.getWebappDirectory(), ArtifactsPackagingTask.SERVICES_PATH + targetFileName );
112             }
113             else if ( "mar".equals( type ) )
114             {
115                 targetFile =
116                     new File( context.getWebappDirectory(), ArtifactsPackagingTask.MODULES_PATH + targetFileName );
117             }
118             else if ( "xar".equals( type ) )
119             {
120                 targetFile =
121                     new File( context.getWebappDirectory(), ArtifactsPackagingTask.EXTENSIONS_PATH + targetFileName );
122             }
123             else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type )
124                 || "test-jar".equals( type ) )
125             {
126                 targetFile = new File( context.getWebappDirectory(), LIB_PATH + targetFileName );
127             }
128             else if ( "par".equals( type ) )
129             {
130                 String targetFileName2 = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar";
131                 targetFile = new File( context.getWebappDirectory(), LIB_PATH + targetFileName2 );
132             }
133 
134             // now remove
135             if ( targetFile == null )
136             {
137                 context.getLog().error( "Could not get file from dependency [" + dependency + "]" );
138             }
139             else if ( targetFile.exists() )
140             {
141                 context.getLog().debug( "Removing file [" + targetFile.getAbsolutePath() + "]" );
142                 targetFile.delete();
143             }
144             else
145             {
146                 context.getLog().warn( "File to remove [" + targetFile.getAbsolutePath() + "] has not been found" );
147             }
148         }
149         else
150         {
151             context.getLog().warn( "Could not retrieve the target file name of dependency [" + dependency + "]" );
152         }
153     }
154 
155 
156     class DependenciesAnalysisCallbackImpl
157         implements WebappStructure.DependenciesAnalysisCallback
158     {
159         private final WarPackagingContext context;
160 
161         DependenciesAnalysisCallbackImpl( WarPackagingContext context )
162         {
163             this.context = context;
164         }
165 
166         public void unchangedDependency( Dependency dependency )
167         {
168             context.getLog().debug( "Dependency [" + dependency + "] has not changed since last build." );
169         }
170 
171         public void newDependency( Dependency dependency )
172         {
173             context.getLog().debug( "New dependency [" + dependency + "]." );
174         }
175 
176         public void removedDependency( Dependency dependency )
177         {
178             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         }
184 
185         public void updatedVersion( Dependency dependency, String previousVersion )
186         {
187             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         }
198 
199         public void updatedScope( Dependency dependency, String previousScope )
200         {
201             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                 // It's now provided or test so it should be removed
207                 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         }
215 
216 
217         public void updatedOptionalFlag( Dependency dependency, boolean previousOptional )
218         {
219             if ( !previousOptional && dependency.isOptional() )
220             {
221                 // It wasn't optional but now it is anymore
222                 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         }
232 
233         public void updatedUnknown( Dependency dependency, Dependency previousDep )
234         {
235             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         }
241 
242     }
243 
244 }