1 package org.apache.maven.plugins.war.util;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.archiver.MavenArchiveConfiguration;
23 import org.apache.maven.archiver.MavenArchiver;
24 import org.apache.maven.artifact.DependencyResolutionRequiredException;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugins.war.packaging.AbstractWarPackagingTask;
28 import org.apache.maven.project.MavenProject;
29 import org.codehaus.plexus.archiver.ArchiverException;
30 import org.codehaus.plexus.archiver.jar.JarArchiver;
31 import org.codehaus.plexus.archiver.jar.ManifestException;
32
33 import java.io.File;
34 import java.io.IOException;
35
36 /**
37 * Packages the content of the classes directory.
38 *
39 * @author Stephane Nicoll
40 */
41 public class ClassesPackager
42 {
43
44 /**
45 * Package the classes
46 *
47 * @param classesDirectory the classes directory
48 * @param targetFile the target file
49 * @param jarArchiver the jar archiver to use
50 * @param session the current session
51 * @param project the related project
52 * @param archiveConfiguration the archive configuration to use
53 * @throws MojoExecutionException if an error occurred while creating the archive
54 */
55 public void packageClasses( File classesDirectory, File targetFile, JarArchiver jarArchiver, MavenSession session,
56 MavenProject project, MavenArchiveConfiguration archiveConfiguration )
57 throws MojoExecutionException
58 {
59
60 try
61 {
62 final MavenArchiver archiver = new MavenArchiver();
63 archiver.setArchiver( jarArchiver );
64 archiver.setOutputFile( targetFile );
65 archiver.getArchiver().addDirectory( classesDirectory );
66 archiver.createArchive( session, project, archiveConfiguration );
67 }
68 catch ( ArchiverException | ManifestException | IOException | DependencyResolutionRequiredException e )
69 {
70 throw new MojoExecutionException( "Could not create classes archive", e );
71 }
72 }
73
74 /**
75 * Returns the classes directory from the specified webapp directory.
76 *
77 * @param webappDirectory the webapp directory
78 * @return the classes directory of the specified webapp directory
79 */
80 public File getClassesDirectory( File webappDirectory )
81 {
82 return new File( webappDirectory, AbstractWarPackagingTask.CLASSES_PATH );
83 }
84 }