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.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
35
36
37
38
39
40
41 public class ClassesPackagingTask extends AbstractWarPackagingTask {
42 private final Overlay currentProjectOverlay;
43
44
45
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
84
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 }