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 are 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   */
42  public class ClassesPackagingTask
43      extends AbstractWarPackagingTask
44  {
45      private final Overlay currentProjectOverlay;
46  
47      /**
48       * @param currentProjectOverlay {@link #currentProjectOverlay}
49       */
50      public ClassesPackagingTask( Overlay currentProjectOverlay )
51      {
52          this.currentProjectOverlay = currentProjectOverlay;
53      }
54  
55      @Override
56      public void performPackaging( WarPackagingContext context )
57          throws MojoExecutionException
58      {
59          final File webappClassesDirectory = new File( context.getWebappDirectory(), CLASSES_PATH );
60          if ( !webappClassesDirectory.exists() )
61          {
62              webappClassesDirectory.mkdirs();
63          }
64  
65          if ( context.getClassesDirectory().exists() && !context.getClassesDirectory().equals( webappClassesDirectory ) )
66          {
67              if ( context.archiveClasses() )
68              {
69                  generateJarArchive( context );
70              }
71              else
72              {
73                  final PathSet sources = getFilesToIncludes( context.getClassesDirectory(), null, null );
74                  try
75                  {
76                      copyFiles( currentProjectOverlay.getId(), context, context.getClassesDirectory(), sources,
77                                 CLASSES_PATH, false );
78                  }
79                  catch ( IOException e )
80                  {
81                      throw new MojoExecutionException( "Could not copy webapp classes ["
82                          + context.getClassesDirectory().getAbsolutePath() + "]", e );
83                  }
84              }
85          }
86      }
87  
88      /**
89       * @param context The warPackingContext.
90       * @throws MojoExecutionException In case of an error.
91       */
92      protected void generateJarArchive( WarPackagingContext context )
93          throws MojoExecutionException
94      {
95          MavenProject project = context.getProject();
96          ArtifactFactory factory = context.getArtifactFactory();
97          Artifact artifact =
98              factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(), project.getVersion(), "jar" );
99          String archiveName;
100         try
101         {
102             archiveName = getArtifactFinalName( context, artifact );
103         }
104         catch ( InterpolationException e )
105         {
106             throw new MojoExecutionException( "Could not get the final name of the artifact [" + artifact.getGroupId()
107                 + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + "]", e );
108         }
109         final String targetFilename = LIB_PATH + archiveName;
110 
111         if ( context.getWebappStructure().registerFile( currentProjectOverlay.getId(), targetFilename ) )
112         {
113             context.addResource( targetFilename );
114 
115             final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
116             final File jarFile = new File( libDirectory, archiveName );
117             final ClassesPackager packager = new ClassesPackager();
118             packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
119                                      context.getSession(), project, context.getArchive(),
120                                      context.getOutputTimestamp() );
121         }
122         else
123         {
124             context.getLog().warn( "Could not generate archive classes file [" + targetFilename
125                                        + "] has already been copied." );
126         }
127     }
128 }