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