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