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
.text
.SimpleDateFormat
;
8 import java
.util
.ArrayList
;
10 import java
.util
.List
;
11 import java
.util
.jar
.Attributes
;
12 import java
.util
.jar
.Manifest
;
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
;
24 public abstract class AbstractBundlesPackagerMojo
extends AbstractOsgiMojo
{
27 * Directory of the simple bundles
29 * @parameter expression="${bundlesDirectory}" default-value="."
32 private File bundlesDirectory
;
35 * Directory containing the packaged bundles.
37 * @parameter expression="${packagedBundlesDir}"
38 * default-value="${project.build.directory}/argeo-osgi"
41 protected File packagedBundlesDir
;
44 * Artifact id for the dependency pom
46 * @parameter expression="${bundlesPomArtifactId}" default-value="bundles"
49 protected String bundlesPomArtifactId
;
52 * Whether should fail if MANIFEST version are not in line with pom version.
54 * @parameter expression="${strictManifestVersion}" default-value="false"
57 protected boolean strictManifestVersion
;
60 * Whether should fail if symbolic name does not match artifact id.
62 * @parameter expression="${strictSymbolicName}" default-value="false"
65 protected boolean strictSymbolicName
;
67 private SimpleDateFormat sdf
= new SimpleDateFormat("yyyyMMdd_HHmmss");
69 protected List
analyze(boolean willGenerate
) throws MojoExecutionException
{
70 List list
= new ArrayList();
72 File
[] bundleDirs
= bundlesDirectory
.listFiles(new FileFilter() {
73 public boolean accept(File file
) {
74 if (!file
.isDirectory())
77 return manifestFileFromDir(file
).exists();
80 for (int i
= 0; i
< bundleDirs
.length
; i
++) {
82 File dir
= bundleDirs
[i
];
83 BundlePackage bundlePackage
;
85 bundlePackage
= processBundleDir(dir
, willGenerate
);
86 } catch (Exception e
) {
87 throw new MojoExecutionException("Could not analyze " + dir
, e
);
89 list
.add(bundlePackage
);
95 protected BundlePackage
processBundleDir(File dir
, boolean willGenerate
)
97 File manifestFile
= manifestFileFromDir(dir
);
98 String artifactId
= dir
.getName();
99 File destFile
= new File(packagedBundlesDir
.getPath() + File
.separator
100 + artifactId
+ ".jar");
102 String manifestStr
= FileUtils
.readFileToString(manifestFile
);
103 char lastChar
= manifestStr
.charAt(manifestStr
.length() - 1);
104 if (lastChar
!= '\n')
105 throw new RuntimeException("Manifest " + manifestFile
107 + " it does not end with and endline character.");
109 Manifest manifest
= readManifest(manifestFile
);
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
);
123 String versionMf
= manifest
.getMainAttributes().getValue(
125 int qIndex
= versionMf
.lastIndexOf(".SNAPSHOT");
126 String versionMfMain
;
128 versionMfMain
= versionMf
.substring(0, qIndex
);
130 versionMfMain
= versionMf
;
132 int sIndex
= project
.getModel().getVersion().lastIndexOf("-SNAPSHOT");
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";
142 versionMain
= project
.getVersion();
143 // buildId = "R_" + sdf.format(new Date());// R for release
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
);
157 String newVersionMf
= versionMfMain
+ "." + buildId
;
158 String newVersionArt
;
160 newVersionArt
= versionMfMain
+ "-SNAPSHOT";
162 newVersionArt
= versionMfMain
;
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());
171 // "project.getModel().getVersion()="
172 // + project.getModel().getVersion());
173 // getLog().info("versionMf=" + versionMf);
174 // getLog().info("buildId=" + buildId);
175 getLog().info("newVersionMf=" + newVersionMf
);
178 manifest
.getMainAttributes().putValue("Bundle-Version", newVersionMf
);
179 manifest
.getMainAttributes().put(Attributes
.Name
.MANIFEST_VERSION
,
182 Artifact artifact
= artifactFactory
.createBuildArtifact(project
183 .getGroupId(), artifactId
, newVersionArt
, "jar");
184 return new BundlePackage(artifact
, dir
, new Manifest(manifest
),
188 protected File
manifestFileFromDir(File dir
) {
189 return new File(dir
+ File
.separator
+ "META-INF" + File
.separator
193 protected File
bundlesPomFile() {
194 return new File(packagedBundlesDir
+ File
.separator
+ "bundles.pom");
197 protected Artifact
bundlesPomArtifact() {
198 return artifactFactory
.createBuildArtifact(project
.getGroupId(),
199 bundlesPomArtifactId
, project
.getVersion(), "pom");
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
;
208 public BundlePackage(Artifact artifact
, File bundleDir
,
209 Manifest manifest
, File packageFile
) {
211 this.artifact
= artifact
;
212 this.bundleDir
= bundleDir
;
213 this.manifest
= manifest
;
214 this.packageFile
= packageFile
;
217 public Artifact
getArtifact() {
221 public File
getPackageFile() {
225 public File
getBundleDir() {
229 public Manifest
getManifest() {
234 protected Manifest
readManifest(File file
) throws IOException
{
235 Manifest manifest
= new Manifest();
236 FileInputStream in
= new FileInputStream(file
);