1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the maven-sar-plugin.
15 *
16 * The Initial Developer of the Original Code is
17 * the Vermont Department of Taxes.
18 * Portions created by the Initial Developer are Copyright (C) 2007
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Tom Cort <tom.cort@state.vt.us>
23 *
24 * ***** END LICENSE BLOCK ***** */
25
26 package net.sf.mavensar;
27
28 import java.io.File;
29
30 import org.apache.maven.archiver.MavenArchiveConfiguration;
31 import org.apache.maven.archiver.MavenArchiver;
32
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.project.MavenProjectHelper;
38
39 import org.codehaus.plexus.archiver.jar.JarArchiver;
40
41 /**
42 * Creates a Service Archive.
43 *
44 * @goal sar
45 * @phase package
46 * @requiresDependencyResolution runtime
47 */
48 public class SarMojo extends AbstractMojo {
49
50 /**
51 * The Project.
52 *
53 * @parameter expression="${project}"
54 * @required
55 * @readonly
56 */
57 private MavenProject project;
58
59 /**
60 * The Project Helper.
61 *
62 * @component
63 */
64 private MavenProjectHelper helper;
65
66 /**
67 * Archive Configuration.
68 *
69 * @parameter
70 */
71 private MavenArchiveConfiguration archiveConfig = new MavenArchiveConfiguration();
72
73 /**
74 * The JAR Archiver.
75 *
76 * @parameter expression="${component.org.codehaus.plexus.archiver.Archiver#jar}"
77 * @required
78 */
79 private JarArchiver jarArchiver;
80
81 /**
82 * Directory where the sar file should be written to.
83 *
84 * @parameter expression="${project.build.directory}"
85 * @required
86 */
87 private File targetDir;
88
89 /**
90 * Directory containing the classes (usually target/classes).
91 *
92 * @parameter expression="${project.build.outputDirectory}"
93 * @required
94 */
95 private File classesDir;
96
97 /**
98 * Name of the generated SAR (without the ".sar" extension).
99 *
100 * @parameter expression="${project.build.finalName}"
101 * @required
102 */
103 private String targetFile;
104
105 /**
106 * Classifier to add to the artifact generated.
107 *
108 * @parameter
109 */
110 private String classifier;
111
112 /**
113 * List of files to exclude from the SAR.
114 */
115 private static final String[] EXCLUDES = new String[] { "**/*.html" };
116
117 /**
118 * List of files in include in the SAR.
119 */
120 private static final String[] INCLUDES = new String[] { "**/*.xml", "**/*.class" };
121
122 /**
123 * Executes the task. In this case, packages the sar.
124 *
125 * @throws MojoExecutionException
126 * on archiver error.
127 */
128 public final void execute() throws MojoExecutionException {
129
130 getLog().info("====== MAVEN-SAR-PLUGIN ======");
131 getLog().info("targetDir [" + targetDir + "]");
132 getLog().info("targetFile [" + targetFile + ".sar]");
133 getLog().info("classesDir [" + classesDir.toString() + "]");
134
135 File sar = new File(targetDir, targetFile + ".sar");
136
137 MavenArchiver archiver = new MavenArchiver();
138 archiver.setArchiver(jarArchiver);
139 archiver.setOutputFile(sar);
140
141 try {
142
143 // Attempt to locale jboss-service.xml, fail if not found.
144 File jbossServiceXml = new File(classesDir.toString() + "/META-INF/jboss-service.xml");
145
146 if (!jbossServiceXml.exists()) {
147 String msg = classesDir.toString() + "/META-INF/jboss-service.xml was not found.";
148 throw new MojoExecutionException(msg);
149 }
150
151 archiver.getArchiver().addDirectory(classesDir, INCLUDES, EXCLUDES);
152 archiver.createArchive(project, archiveConfig);
153
154 if (classifier == null) {
155 project.getArtifact().setFile(sar);
156 } else {
157 helper.attachArtifact(project, "sar", classifier, sar);
158 }
159
160 } catch (Exception e) {
161 throw new MojoExecutionException("Error packaging SAR", e);
162 }
163
164 getLog().info("====== MAVEN-SAR-PLUGIN ======");
165 }
166 }