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