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