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