]> git.argeo.org Git - gpl/argeo-slc.git/blob - maven-argeo-osgi-plugin/src/main/java/org/argeo/slc/maven/plugins/osgi/AbstractBundlesPackagerMojo.java
Add in JVM launch of the Equinox runtime
[gpl/argeo-slc.git] / maven-argeo-osgi-plugin / src / main / java / org / argeo / slc / maven / plugins / osgi / AbstractBundlesPackagerMojo.java
1 package org.argeo.slc.maven.plugins.osgi;
2
3 import java.io.File;
4 import java.io.FileFilter;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.text.SimpleDateFormat;
8 import java.util.ArrayList;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.jar.Attributes;
12 import java.util.jar.Manifest;
13
14 import org.apache.commons.io.FileUtils;
15 import org.apache.commons.io.IOUtils;
16 import org.apache.maven.artifact.Artifact;
17 import org.apache.maven.plugin.MojoExecutionException;
18 import org.apache.maven.project.MavenProject;
19
20 /**
21 * @author mbaudier
22 *
23 */
24 public abstract class AbstractBundlesPackagerMojo extends AbstractOsgiMojo {
25
26 /**
27 * Directory of the simple bundles
28 *
29 * @parameter expression="${bundlesDirectory}" default-value="."
30 * @required
31 */
32 private File bundlesDirectory;
33
34 /**
35 * Directory containing the packaged bundles.
36 *
37 * @parameter expression="${packagedBundlesDir}"
38 * default-value="${project.build.directory}/argeo-osgi"
39 * @required
40 */
41 protected File packagedBundlesDir;
42
43 /**
44 * Artifact id for the dependency pom
45 *
46 * @parameter expression="${bundlesPomArtifactId}" default-value="bundles"
47 * @required
48 */
49 protected String bundlesPomArtifactId;
50
51 /**
52 * Whether should fail if MANIFEST version are not in line with pom version.
53 *
54 * @parameter expression="${strictManifestVersion}" default-value="false"
55 * @required
56 */
57 protected boolean strictManifestVersion;
58
59 /**
60 * Whether should fail if symbolic name does not match artifact id.
61 *
62 * @parameter expression="${strictSymbolicName}" default-value="false"
63 * @required
64 */
65 protected boolean strictSymbolicName;
66
67 private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
68
69 protected List analyze(boolean willGenerate) throws MojoExecutionException {
70 List list = new ArrayList();
71
72 File[] bundleDirs = bundlesDirectory.listFiles(new FileFilter() {
73 public boolean accept(File file) {
74 if (!file.isDirectory())
75 return false;
76
77 return manifestFileFromDir(file).exists();
78 }
79 });
80 for (int i = 0; i < bundleDirs.length; i++) {
81
82 File dir = bundleDirs[i];
83 BundlePackage bundlePackage;
84 try {
85 bundlePackage = processBundleDir(dir, willGenerate);
86 } catch (Exception e) {
87 throw new MojoExecutionException("Could not analyze " + dir, e);
88 }
89 list.add(bundlePackage);
90
91 }
92 return list;
93 }
94
95 protected BundlePackage processBundleDir(File dir, boolean willGenerate)
96 throws Exception {
97 File manifestFile = manifestFileFromDir(dir);
98 String artifactId = dir.getName();
99 File destFile = new File(packagedBundlesDir.getPath() + File.separator
100 + artifactId + ".jar");
101
102 String manifestStr = FileUtils.readFileToString(manifestFile);
103 char lastChar = manifestStr.charAt(manifestStr.length() - 1);
104 if (lastChar != '\n')
105 throw new RuntimeException("Manifest " + manifestFile
106 + " is not valid,"
107 + " it does not end with and endline character.");
108
109 Manifest manifest = readManifest(manifestFile);
110 // Symbolic name
111 String symbolicNameMf = manifest.getMainAttributes().getValue(
112 "Bundle-SymbolicName");
113 if (!artifactId.equals(symbolicNameMf)) {
114 String msg = "Symbolic name " + symbolicNameMf
115 + " does not match with directory name " + artifactId;
116 if (strictSymbolicName)
117 throw new RuntimeException(msg);
118 else
119 getLog().warn(msg);
120 }
121
122 // Version
123 String versionMf = manifest.getMainAttributes().getValue(
124 "Bundle-Version");
125 int qIndex = versionMf.lastIndexOf(".SNAPSHOT");
126 String versionMfMain;
127 if (qIndex >= 0)
128 versionMfMain = versionMf.substring(0, qIndex);
129 else
130 versionMfMain = versionMf;
131
132 int sIndex = project.getModel().getVersion().lastIndexOf("-SNAPSHOT");
133 String versionMain;
134 String buildId;
135 boolean isSnapshot = false;
136 if (sIndex >= 0) {// SNAPSHOT
137 versionMain = project.getVersion().substring(0, sIndex);
138 // buildId = "D_" + sdf.format(new Date());// D for dev
139 buildId = "SNAPSHOT";
140 isSnapshot = true;
141 } else {
142 versionMain = project.getVersion();
143 // buildId = "R_" + sdf.format(new Date());// R for release
144 buildId = "";
145 }
146
147 if (!versionMain.equals(versionMfMain)) {
148 String msg = "Main manifest version " + versionMfMain
149 + " of bundle " + artifactId
150 + " do not match with main project version " + versionMain;
151 if (strictManifestVersion)
152 throw new RuntimeException(msg);
153 else
154 getLog().warn(msg);
155 }
156
157 String newVersionMf = versionMfMain + "." + buildId;
158 String newVersionArt;
159 if (isSnapshot) {
160 newVersionArt = versionMfMain + "-SNAPSHOT";
161 } else {
162 newVersionArt = versionMfMain;
163 }
164
165 //boolean debug = true;
166 boolean debug = getLog().isDebugEnabled();
167 if (debug && willGenerate) {
168 getLog().info("\n## " + artifactId);
169 getLog().info("project.getVersion()=" + project.getVersion());
170 // getLog().info(
171 // "project.getModel().getVersion()="
172 // + project.getModel().getVersion());
173 // getLog().info("versionMf=" + versionMf);
174 // getLog().info("buildId=" + buildId);
175 getLog().info("newVersionMf=" + newVersionMf);
176 }
177
178 manifest.getMainAttributes().putValue("Bundle-Version", newVersionMf);
179 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,
180 "1.0");
181
182 Artifact artifact = artifactFactory.createBuildArtifact(project
183 .getGroupId(), artifactId, newVersionArt, "jar");
184 return new BundlePackage(artifact, dir, new Manifest(manifest),
185 destFile);
186 }
187
188 protected File manifestFileFromDir(File dir) {
189 return new File(dir + File.separator + "META-INF" + File.separator
190 + "MANIFEST.MF");
191 }
192
193 protected File bundlesPomFile() {
194 return new File(packagedBundlesDir + File.separator + "bundles.pom");
195 }
196
197 protected Artifact bundlesPomArtifact() {
198 return artifactFactory.createBuildArtifact(project.getGroupId(),
199 bundlesPomArtifactId, project.getVersion(), "pom");
200 }
201
202 protected static class BundlePackage {
203 private final Artifact artifact;
204 private final File bundleDir;
205 private final Manifest manifest;
206 private final File packageFile;
207
208 public BundlePackage(Artifact artifact, File bundleDir,
209 Manifest manifest, File packageFile) {
210 super();
211 this.artifact = artifact;
212 this.bundleDir = bundleDir;
213 this.manifest = manifest;
214 this.packageFile = packageFile;
215 }
216
217 public Artifact getArtifact() {
218 return artifact;
219 }
220
221 public File getPackageFile() {
222 return packageFile;
223 }
224
225 public File getBundleDir() {
226 return bundleDir;
227 }
228
229 public Manifest getManifest() {
230 return manifest;
231 }
232 }
233
234 protected Manifest readManifest(File file) throws IOException {
235 Manifest manifest = new Manifest();
236 FileInputStream in = new FileInputStream(file);
237 manifest.read(in);
238 in.close();
239 return manifest;
240 }
241 }