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