1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
43
44
45
46
47
48 public class SarMojo extends AbstractMojo {
49
50
51
52
53
54
55
56
57 private MavenProject project;
58
59
60
61
62
63
64 private MavenProjectHelper helper;
65
66
67
68
69
70
71 private MavenArchiveConfiguration archiveConfig = new MavenArchiveConfiguration();
72
73
74
75
76
77
78
79 private JarArchiver jarArchiver;
80
81
82
83
84
85
86
87 private File targetDir;
88
89
90
91
92
93
94
95 private File classesDir;
96
97
98
99
100
101
102
103 private String targetFile;
104
105
106
107
108
109
110 private String classifier;
111
112
113
114
115 private static final String[] EXCLUDES = new String[] { "**/*.html" };
116
117
118
119
120 private static final String[] INCLUDES = new String[] { "**/*.xml", "**/*.class" };
121
122
123
124
125
126
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
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 }