View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.war.packaging;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.factory.ArtifactFactory;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugins.war.Overlay;
28  import org.apache.maven.plugins.war.util.ClassesPackager;
29  import org.apache.maven.plugins.war.util.PathSet;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.interpolation.InterpolationException;
32  
33  /**
34   * Handles the classes directory that needs to be packaged in the web application.
35   *
36   * Based on the {@link WarPackagingContext#archiveClasses()} flag, the resources are either copied into to
37   * {@code WEB-INF/classes} directory or archived in a jar within the {@code WEB-INF/lib} directory.
38   *
39   * @author Stephane Nicoll
40   */
41  public class ClassesPackagingTask extends AbstractWarPackagingTask {
42      private final Overlay currentProjectOverlay;
43  
44      /**
45       * @param currentProjectOverlay {@link #currentProjectOverlay}
46       */
47      public ClassesPackagingTask(Overlay currentProjectOverlay) {
48          this.currentProjectOverlay = currentProjectOverlay;
49      }
50  
51      @Override
52      public void performPackaging(WarPackagingContext context) throws MojoExecutionException {
53          final File webappClassesDirectory = new File(context.getWebappDirectory(), CLASSES_PATH);
54          if (!webappClassesDirectory.exists()) {
55              webappClassesDirectory.mkdirs();
56          }
57  
58          if (context.getClassesDirectory().exists()
59                  && !context.getClassesDirectory().equals(webappClassesDirectory)) {
60              if (context.archiveClasses()) {
61                  generateJarArchive(context);
62              } else {
63                  final PathSet sources = getFilesToIncludes(context.getClassesDirectory(), null, null);
64                  try {
65                      copyFiles(
66                              currentProjectOverlay.getId(),
67                              context,
68                              context.getClassesDirectory(),
69                              sources,
70                              CLASSES_PATH,
71                              false);
72                  } catch (IOException e) {
73                      throw new MojoExecutionException(
74                              "Could not copy webapp classes ["
75                                      + context.getClassesDirectory().getAbsolutePath() + "]",
76                              e);
77                  }
78              }
79          }
80      }
81  
82      /**
83       * @param context The warPackingContext.
84       * @throws MojoExecutionException In case of an error.
85       */
86      protected void generateJarArchive(WarPackagingContext context) throws MojoExecutionException {
87          MavenProject project = context.getProject();
88          ArtifactFactory factory = context.getArtifactFactory();
89          Artifact artifact =
90                  factory.createBuildArtifact(project.getGroupId(), project.getArtifactId(), project.getVersion(), "jar");
91          String archiveName;
92          try {
93              archiveName = getArtifactFinalName(context, artifact);
94          } catch (InterpolationException e) {
95              throw new MojoExecutionException(
96                      "Could not get the final name of the artifact [" + artifact.getGroupId() + ":"
97                              + artifact.getArtifactId() + ":" + artifact.getVersion() + "]",
98                      e);
99          }
100         final String targetFilename = LIB_PATH + archiveName;
101 
102         if (context.getWebappStructure().registerFile(currentProjectOverlay.getId(), targetFilename)) {
103             context.addResource(targetFilename);
104 
105             final File libDirectory = new File(context.getWebappDirectory(), LIB_PATH);
106             final File jarFile = new File(libDirectory, archiveName);
107             final ClassesPackager packager = new ClassesPackager();
108             packager.packageClasses(
109                     context.getClassesDirectory(),
110                     jarFile,
111                     context.getJarArchiver(),
112                     context.getSession(),
113                     project,
114                     context.getArchive(),
115                     context.getOutputTimestamp());
116         } else {
117             context.getLog()
118                     .warn("Could not generate archive classes file [" + targetFilename + "] has already been copied.");
119         }
120     }
121 }