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