1 package org
.argeo
.slc
.maven
.plugins
.osgi
;
4 import java
.io
.FileFilter
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileWriter
;
7 import java
.io
.IOException
;
8 import java
.text
.SimpleDateFormat
;
9 import java
.util
.ArrayList
;
10 import java
.util
.Date
;
11 import java
.util
.List
;
12 import java
.util
.jar
.Attributes
;
13 import java
.util
.jar
.Manifest
;
15 import org
.apache
.commons
.io
.FileUtils
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.apache
.maven
.artifact
.Artifact
;
18 import org
.apache
.maven
.artifact
.metadata
.ArtifactMetadata
;
19 import org
.apache
.maven
.model
.Model
;
20 import org
.apache
.maven
.model
.io
.xpp3
.MavenXpp3Writer
;
21 import org
.apache
.maven
.plugin
.MojoExecutionException
;
22 import org
.apache
.maven
.project
.MavenProject
;
23 import org
.apache
.maven
.project
.artifact
.ProjectArtifactMetadata
;
29 public abstract class AbstractBundlesPackagerMojo
extends AbstractOsgiMojo
{
32 * Directory of the simple bundles
34 * @parameter expression="${bundlesDirectory}" default-value="."
37 private File bundlesDirectory
;
40 * Directory containing the packaged bundles.
42 * @parameter expression="${packagedBundlesDir}"
43 * default-value="${project.build.directory}/argeo-osgi"
46 protected File packagedBundlesDir
;
49 * Artifact id for the dependency pom
51 * @parameter expression="${bundlesPomArtifactId}" default-value="bundles"
54 protected String bundlesPomArtifactId
;
57 * Whether should fail if MANIFEST version are not in line with pom version.
59 * @parameter expression="${strictManifestVersion}" default-value="false"
62 protected boolean strictManifestVersion
;
65 * Whether the manifest should be updated with the release version.
67 * @parameter expression="${updateManifestWhenReleasing}"
68 * default-value="true"
71 protected boolean updateManifestWhenReleasing
;
74 * Whether should fail if symbolic name does not match artifact id.
76 * @parameter expression="${strictSymbolicName}" default-value="false"
79 protected boolean strictSymbolicName
;
81 private SimpleDateFormat sdf
= new SimpleDateFormat("yyyyMMdd_HHmmss");
83 protected List
analyze(boolean willGenerate
) throws MojoExecutionException
{
84 List list
= new ArrayList();
86 File
[] bundleDirs
= bundlesDirectory
.listFiles(new FileFilter() {
87 public boolean accept(File file
) {
88 if (!file
.isDirectory())
91 return manifestFileFromDir(file
).exists();
94 for (int i
= 0; i
< bundleDirs
.length
; i
++) {
96 File dir
= bundleDirs
[i
];
97 BundlePackage bundlePackage
;
99 bundlePackage
= processBundleDir(dir
, willGenerate
);
100 } catch (Exception e
) {
101 throw new MojoExecutionException("Could not analyze " + dir
, e
);
103 list
.add(bundlePackage
);
109 protected BundlePackage
processBundleDir(File dir
, boolean willGenerate
)
111 File manifestFile
= manifestFileFromDir(dir
);
112 String artifactId
= dir
.getName();
113 File destFile
= new File(packagedBundlesDir
.getPath() + File
.separator
114 + artifactId
+ ".jar");
116 String manifestStr
= FileUtils
.readFileToString(manifestFile
);
117 char lastChar
= manifestStr
.charAt(manifestStr
.length() - 1);
118 if (lastChar
!= '\n')
119 throw new RuntimeException("Manifest " + manifestFile
121 + " it does not end with and endline character.");
123 Manifest manifest
= readManifest(manifestFile
);
125 String symbolicNameMf
= manifest
.getMainAttributes().getValue(
126 "Bundle-SymbolicName");
127 if (!artifactId
.equals(symbolicNameMf
)) {
128 String msg
= "Symbolic name " + symbolicNameMf
129 + " does not match with directory name " + artifactId
;
130 if (strictSymbolicName
)
131 throw new RuntimeException(msg
);
137 String versionMf
= manifest
.getMainAttributes().getValue(
139 int qIndex
= versionMf
.lastIndexOf(".SNAPSHOT");
140 String versionMfMain
;
142 versionMfMain
= versionMf
.substring(0, qIndex
);
144 versionMfMain
= versionMf
;
146 int sIndex
= project
.getModel().getVersion().lastIndexOf("-SNAPSHOT");
149 boolean isSnapshot
= false;
150 if (sIndex
>= 0) {// SNAPSHOT
151 versionMain
= project
.getVersion().substring(0, sIndex
);
152 // buildId = "D_" + sdf.format(new Date());// D for dev
153 buildId
= "SNAPSHOT";
156 versionMain
= project
.getVersion();
157 // buildId = "R_" + sdf.format(new Date());// R for release
158 buildId
= "R" + sdf
.format(new Date());
161 if (!versionMain
.equals(versionMfMain
)) {
162 String msg
= "Main manifest version " + versionMfMain
163 + " of bundle " + artifactId
164 + " do not match with main project version " + versionMain
;
165 if (strictManifestVersion
)
166 throw new RuntimeException(msg
);
171 String newVersionMf
= versionMfMain
+ "." + buildId
;
172 String newVersionArt
;
174 newVersionArt
= versionMfMain
+ "-SNAPSHOT";
176 newVersionArt
= versionMfMain
;
179 // boolean debug = true;
180 boolean debug
= getLog().isDebugEnabled();
181 if (debug
&& willGenerate
) {
182 getLog().info("\n## " + artifactId
);
183 getLog().info("project.getVersion()=" + project
.getVersion());
185 // "project.getModel().getVersion()="
186 // + project.getModel().getVersion());
187 // getLog().info("versionMf=" + versionMf);
188 // getLog().info("buildId=" + buildId);
189 getLog().info("newVersionMf=" + newVersionMf
);
192 manifest
.getMainAttributes().putValue("Bundle-Version", newVersionMf
);
193 manifest
.getMainAttributes().put(Attributes
.Name
.MANIFEST_VERSION
,
196 Artifact artifact
= artifactFactory
.createBuildArtifact(project
197 .getGroupId(), artifactId
, newVersionArt
, "jar");
198 return new BundlePackage(artifact
, dir
, new Manifest(manifest
),
202 protected File
manifestFileFromDir(File dir
) {
203 return new File(dir
+ File
.separator
+ "META-INF" + File
.separator
207 protected File
bundlesPomFile() {
208 return new File(packagedBundlesDir
+ File
.separator
+ "bundles.pom");
211 protected Artifact
bundlesPomArtifact() {
212 return artifactFactory
.createBuildArtifact(project
.getGroupId(),
213 bundlesPomArtifactId
, project
.getVersion(), "pom");
216 protected StringBuffer
createPomFileHeader(String parentGroupId
,
217 String parentArtifactId
, String parentBaseVersion
, String groupId
,
218 String artifactId
, String packaging
) {
219 StringBuffer pom
= new StringBuffer();
220 // not using append() systematically for the sake of clarity
221 pom
.append("<project>\n");
222 pom
.append("\t<modelVersion>4.0.0</modelVersion>\n");
223 pom
.append("\t<parent>\n");
224 pom
.append("\t\t<groupId>" + parentGroupId
+ "</groupId>\n");
225 pom
.append("\t\t<artifactId>" + parentArtifactId
+ "</artifactId>\n");
226 pom
.append("\t\t<version>" + parentBaseVersion
+ "</version>\n");
227 pom
.append("\t</parent>\n");
228 pom
.append("\t<groupId>" + groupId
+ "</groupId>\n");
229 pom
.append("\t<artifactId>" + artifactId
+ "</artifactId>\n");
230 pom
.append("\t<packaging>" + packaging
+ "</packaging>\n");
233 // TODO: use the Model object e.g.: (from install plugin)
234 // Model model = new Model();
235 // model.setModelVersion( "4.0.0" );
236 // model.setGroupId( groupId );
237 // model.setArtifactId( artifactId );
238 // model.setVersion( version );
239 // model.setPackaging( packaging );
240 // model.setDescription( "POM was created from install:install-file" );
241 // fw = new FileWriter( tempFile );
242 // tempFile.deleteOnExit();
243 // new MavenXpp3Writer().write( fw, model );
244 // ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact,
246 // artifact.addMetadata( metadata );
250 /** Simple close the project tag */
251 protected String
closePomFile(StringBuffer pom
) {
252 pom
.append("</project>\n");
253 return pom
.toString();
256 protected static class BundlePackage
{
257 private final Artifact artifact
;
258 private final File bundleDir
;
259 private final Manifest manifest
;
260 private final File packageFile
;
262 public BundlePackage(Artifact artifact
, File bundleDir
,
263 Manifest manifest
, File packageFile
) {
265 this.artifact
= artifact
;
266 this.bundleDir
= bundleDir
;
267 this.manifest
= manifest
;
268 this.packageFile
= packageFile
;
271 public Artifact
getArtifact() {
275 public File
getPackageFile() {
279 public File
getBundleDir() {
283 public Manifest
getManifest() {
287 public File
getManifestFile() {
288 return new File(getPackageFile().getPath() + ".MF");
291 public File
getPomFile() {
292 return new File(getPackageFile().getPath() + ".pom.xml");
296 protected Manifest
readManifest(File file
) throws IOException
{
297 Manifest manifest
= new Manifest();
298 FileInputStream in
= new FileInputStream(file
);