1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41
42 public class ClassesPackagingTask extends AbstractWarPackagingTask {
43 private final Overlay currentProjectOverlay;
44
45
46
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
85
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 }