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 {
52 this.currentProjectOverlay = currentProjectOverlay;
53 }
54
55 public void performPackaging( WarPackagingContext context )
56 throws MojoExecutionException
57 {
58 final File webappClassesDirectory = new File( context.getWebappDirectory(), CLASSES_PATH );
59 if ( !webappClassesDirectory.exists() )
60 {
61 webappClassesDirectory.mkdirs();
62 }
63
64 if ( context.getClassesDirectory().exists() && !context.getClassesDirectory().equals( webappClassesDirectory ) )
65 {
66 if ( context.archiveClasses() )
67 {
68 generateJarArchive( context );
69 }
70 else
71 {
72 final PathSet sources = getFilesToIncludes( context.getClassesDirectory(), null, null );
73 try
74 {
75 copyFiles( currentProjectOverlay.getId(), context, context.getClassesDirectory(),
76 sources, CLASSES_PATH, false );
77 }
78 catch ( IOException e )
79 {
80 throw new MojoExecutionException(
81 "Could not copy webapp classes [" + context.getClassesDirectory().getAbsolutePath() + "]", e );
82 }
83 }
84 }
85 }
86
87 protected void generateJarArchive( WarPackagingContext context )
88 throws MojoExecutionException
89 {
90 MavenProject project = context.getProject();
91 ArtifactFactory factory = context.getArtifactFactory();
92 Artifact artifact = factory.createBuildArtifact( project.getGroupId(), project.getArtifactId(),
93 project.getVersion(), "jar" );
94 String archiveName = null;
95 try
96 {
97 archiveName = getArtifactFinalName( context, artifact );
98 }
99 catch ( InterpolationException e )
100 {
101 throw new MojoExecutionException(
102 "Could not get the final name of the artifact [" + artifact.getGroupId() + ":" + artifact.getArtifactId()
103 + ":" + artifact.getVersion() + "]", e );
104 }
105 final String targetFilename = LIB_PATH + archiveName;
106
107 if ( context.getWebappStructure().registerFile( currentProjectOverlay.getId(), targetFilename ) )
108 {
109 final File libDirectory = new File( context.getWebappDirectory(), LIB_PATH );
110 final File jarFile = new File( libDirectory, archiveName );
111 final ClassesPackager packager = new ClassesPackager();
112 packager.packageClasses( context.getClassesDirectory(), jarFile, context.getJarArchiver(),
113 context.getSession(), project, context.getArchive() );
114 }
115 else
116 {
117 context.getLog().warn(
118 "Could not generate archive classes file [" + targetFilename + "] has already been copied." );
119 }
120 }
121 }