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.artifact.factory.ArtifactFactory;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.war.Overlay;
26  import org.apache.maven.plugins.war.util.ClassesPackager;
27  import org.apache.maven.plugins.war.util.PathSet;
28  import org.apache.maven.project.MavenProject;
29  import org.codehaus.plexus.interpolation.InterpolationException;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  /**
35   * Handles the classes directory that needs to be packaged in the web application.
36   * 
37   * Based on the {@link WarPackagingContext#archiveClasses()} flag the resources either copied into to
38   * <tt>WEB-INF/classes</tt> directory or archived in a jar within the <tt>WEB-INF/lib</tt> directory.
39   *
40   * @author Stephane Nicoll
41   * @version $Id: ClassesPackagingTask.java 1756988 2016-08-20 10:50:13Z khmarbaise $
42   */
43  public class ClassesPackagingTask
44      extends AbstractWarPackagingTask
45  {
46      private final Overlay currentProjectOverlay;
47  
48      /**
49       * @param currentProjectOverlay {@link #currentProjectOverlay}
50       */
51      public ClassesPackagingTask( Overlay currentProjectOverlay )
52      {
53          this.currentProjectOverlay = currentProjectOverlay;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      public void performPackaging( WarPackagingContext context )
60          throws MojoExecutionException
61      {
62          final File webappClassesDirectory = new File( context.getWebappDirectory(), CLASSES_PATH );
63          if ( !webappClassesDirectory.exists() )
64          {
65              webappClassesDirectory.mkdirs();
66          }
67  
68          if ( context.getClassesDirectory().exists() && !context.getClassesDirectory().equals( webappClassesDirectory ) )
69          {
70              if ( context.archiveClasses() )
71              {
72                  generateJarArchive( context );
73              }
74              else
75              {
76                  final PathSet sources = getFilesToIncludes( context.getClassesDirectory(), null, null );
77                  try
78                  {
79                      copyFiles( currentProjectOverlay.getId(), context, context.getClassesDirectory(), sources,
80                                 CLASSES_PATH, false );
81                  }
82                  catch ( IOException e )
83                  {
84                      throw new MojoExecutionException( "Could not copy webapp classes ["
85                          + context.getClassesDirectory().getAbsolutePath() + "]", e );
86                  }
87              }
88          }
89      }
90  
91      /**
92       * @param context The warPackingContext.
93       * @throws MojoExecutionException In casae of an error.
94       */
95      protected void generateJarArchive( WarPackagingContext context )
96          throws MojoExecutionException
97      {
98          MavenProject project = context.getProject();
99          ArtifactFactory factory = context.getArtifactFactory();
100         Artifact artifact =
101             factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), "jar" );
102         String archiveName;
103         try
104         {
105             archiveName = getArtifactFinalName( context, artifact );
106         }
107         catch ( InterpolationException e )
108         {
109             throw new MojoExecutionException( "Could not get the final name of the artifact [" + artifact.getGroupId()
110                 + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + "]", e );
111         }
112         final String targetFilename = LIB_PATH + archiveName;
113 
114         if ( context.getWebappStructure().registerFile( currentProjectOverlay.getId(), targetFilename ) )
115         {
116             final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
117             final File jarFile = new File( libDirectory, archiveName );
118             final ClassesPackager packager = new ClassesPackager();
119             packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
120                                      context.getSession(), project, context.getArchive() );
121         }
122         else
123         {
124             context.getLog().warn( "Could not generate archive classes file [" + targetFilename
125                                        + "] has already been copied." );
126         }
127     }
128 }