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