1 package org
.argeo
.slc
.maven
.plugins
.osgi
;
4 import java
.io
.FileFilter
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.IOException
;
7 import java
.util
.ArrayList
;
9 import java
.util
.jar
.Attributes
;
10 import java
.util
.jar
.Manifest
;
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
;
22 public abstract class AbstractBundlesPackagerMojo
extends AbstractOsgiMojo
{
25 * Directory of the simple bundles
27 * @parameter expression="${bundlesDirectory}" default-value="."
30 private File bundlesDirectory
;
33 * Directory containing the packaged bundles.
35 * @parameter expression="${packagedBundlesDir}"
36 * default-value="${project.build.directory}/argeo-osgi"
39 protected File packagedBundlesDir
;
42 * Artifact id for the dependency pom
44 * @parameter expression="${bundlesPomArtifactId}" default-value="bundles"
47 protected String bundlesPomArtifactId
;
49 protected List
analyze() throws MojoExecutionException
{
50 List list
= new ArrayList();
52 File
[] bundleDirs
= bundlesDirectory
.listFiles(new FileFilter() {
53 public boolean accept(File file
) {
54 if (!file
.isDirectory())
57 return manifestFileFromDir(file
).exists();
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");
67 String manifestStr
= FileUtils
.readFileToString(manifestFile
);
68 char lastChar
= manifestStr
.charAt(manifestStr
.length() - 1);
70 throw new RuntimeException(
73 + " is not valid, it does not end with and endline character.");
75 Manifest manifest
= readManifest(manifestFile
);
77 String symbolicNameMf
= manifest
.getMainAttributes().getValue(
78 "Bundle-SymbolicName");
79 if (!artifactId
.equals(symbolicNameMf
))
81 "Symbolic name " + symbolicNameMf
82 + " does not match with directory name "
86 String versionMf
= manifest
.getMainAttributes().getValue(
88 int qIndex
= versionMf
.lastIndexOf(".qualifier");
91 versionMfMain
= versionMf
.substring(0, qIndex
);
93 versionMfMain
= versionMf
;
95 int sIndex
= project
.getVersion().lastIndexOf("-SNAPSHOT");
97 boolean isSnapshot
= false;
98 if (sIndex
>= 0) {// SNAPSHOT
99 versionMain
= project
.getVersion().substring(0, sIndex
);
102 versionMain
= project
.getVersion();
105 if (!versionMain
.equals(versionMfMain
))
108 "Main manifest version "
112 + " do not match with main project version "
116 String newVersionArt
;
118 newVersionMf
= versionMfMain
+ ".SNAPSHOT";
119 newVersionArt
= versionMfMain
+ "-SNAPSHOT";
121 newVersionMf
= versionMfMain
;
122 newVersionArt
= versionMfMain
;
125 manifest
.getMainAttributes().putValue("Bundle-Version",
127 manifest
.getMainAttributes().put(
128 Attributes
.Name
.MANIFEST_VERSION
, "1.0");
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
);
142 protected File
manifestFileFromDir(File dir
) {
143 return new File(dir
+ File
.separator
+ "META-INF" + File
.separator
147 protected File
bundlesPomFile() {
148 return new File(packagedBundlesDir
+ File
.separator
+ "bundles.pom");
151 protected Artifact
bundlesPomArtifact() {
152 return artifactFactory
.createBuildArtifact(project
.getGroupId(),
153 bundlesPomArtifactId
, project
.getVersion(), "pom");
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
;
162 public BundlePackage(Artifact artifact
, File bundleDir
,
163 Manifest manifest
, File packageFile
) {
165 this.artifact
= artifact
;
166 this.bundleDir
= bundleDir
;
167 this.manifest
= manifest
;
168 this.packageFile
= packageFile
;
171 public Artifact
getArtifact() {
175 public File
getPackageFile() {
179 public File
getBundleDir() {
183 public Manifest
getManifest() {
188 protected Manifest
readManifest(File file
) throws IOException
{
189 Manifest manifest
= new Manifest();
190 FileInputStream in
= new FileInputStream(file
);