1 package org
.argeo
.build
;
3 import static java
.lang
.System
.Logger
.Level
.DEBUG
;
4 import static java
.lang
.System
.Logger
.Level
.ERROR
;
5 import static java
.lang
.System
.Logger
.Level
.INFO
;
6 import static java
.lang
.System
.Logger
.Level
.TRACE
;
7 import static java
.lang
.System
.Logger
.Level
.WARNING
;
8 import static java
.nio
.file
.FileVisitResult
.CONTINUE
;
9 import static java
.util
.jar
.Attributes
.Name
.MANIFEST_VERSION
;
10 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_EMBED
;
11 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_M2
;
12 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_M2_MERGE
;
13 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_M2_REPO
;
14 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED
;
15 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ARGEO_ORIGIN_URI
;
16 import static org
.argeo
.build
.Repackage
.ManifestConstants
.BUNDLE_LICENSE
;
17 import static org
.argeo
.build
.Repackage
.ManifestConstants
.BUNDLE_SYMBOLICNAME
;
18 import static org
.argeo
.build
.Repackage
.ManifestConstants
.BUNDLE_VERSION
;
19 import static org
.argeo
.build
.Repackage
.ManifestConstants
.ECLIPSE_SOURCE_BUNDLE
;
20 import static org
.argeo
.build
.Repackage
.ManifestConstants
.EXPORT_PACKAGE
;
21 import static org
.argeo
.build
.Repackage
.ManifestConstants
.IMPORT_PACKAGE
;
22 import static org
.argeo
.build
.Repackage
.ManifestConstants
.SPDX_LICENSE_IDENTIFIER
;
24 import java
.io
.BufferedWriter
;
26 import java
.io
.FileNotFoundException
;
27 import java
.io
.IOException
;
28 import java
.io
.InputStream
;
29 import java
.io
.OutputStream
;
30 import java
.lang
.System
.Logger
;
31 import java
.net
.MalformedURLException
;
33 import java
.nio
.charset
.StandardCharsets
;
34 import java
.nio
.file
.DirectoryStream
;
35 import java
.nio
.file
.FileSystem
;
36 import java
.nio
.file
.FileSystems
;
37 import java
.nio
.file
.FileVisitResult
;
38 import java
.nio
.file
.Files
;
39 import java
.nio
.file
.Path
;
40 import java
.nio
.file
.PathMatcher
;
41 import java
.nio
.file
.Paths
;
42 import java
.nio
.file
.SimpleFileVisitor
;
43 import java
.nio
.file
.StandardOpenOption
;
44 import java
.nio
.file
.attribute
.BasicFileAttributes
;
45 import java
.util
.ArrayList
;
46 import java
.util
.HashMap
;
47 import java
.util
.Iterator
;
48 import java
.util
.List
;
50 import java
.util
.Objects
;
51 import java
.util
.Properties
;
53 import java
.util
.TreeMap
;
54 import java
.util
.TreeSet
;
55 import java
.util
.concurrent
.CompletableFuture
;
56 import java
.util
.jar
.Attributes
;
57 import java
.util
.jar
.JarEntry
;
58 import java
.util
.jar
.JarInputStream
;
59 import java
.util
.jar
.JarOutputStream
;
60 import java
.util
.jar
.Manifest
;
61 import java
.util
.zip
.Deflater
;
63 import aQute
.bnd
.osgi
.Analyzer
;
64 import aQute
.bnd
.osgi
.Jar
;
67 * Simple tool repackaging existing jar files into OSGi bundles in an A2
70 public class Repackage
{
71 final static Logger logger
= System
.getLogger(Repackage
.class.getName());
74 * Environment variable on whether sources should be packaged separately or
75 * integrated in the bundles.
77 final static String ENV_SOURCE_BUNDLES
= "SOURCE_BUNDLES";
79 /** Whether repackaging should run in parallel or sequentially. */
80 final static boolean parallel
= true;
83 /** Summary of all license seen during the repackaging. */
84 final static Map
<String
, Set
<String
>> licensesUsed
= new TreeMap
<>();
86 /** Main entry point. */
87 public static void main(String
[] args
) {
88 if (args
.length
< 2) {
89 System
.err
.println("Usage: <path to a2 output dir> <category1> <category2> ...");
92 Path a2Base
= Paths
.get(args
[0]).toAbsolutePath().normalize();
93 Path descriptorsBase
= Paths
.get(".").toAbsolutePath().normalize();
94 Repackage factory
= new Repackage(a2Base
, descriptorsBase
);
96 List
<CompletableFuture
<Void
>> toDos
= new ArrayList
<>();
97 for (int i
= 1; i
< args
.length
; i
++) {
98 Path p
= Paths
.get(args
[i
]);
100 toDos
.add(CompletableFuture
.runAsync(() -> factory
.processCategory(p
)));
102 factory
.processCategory(p
);
104 CompletableFuture
.allOf(toDos
.toArray(new CompletableFuture
[toDos
.size()])).join();
107 StringBuilder sb
= new StringBuilder();
108 for (String licenseId
: licensesUsed
.keySet())
109 for (String name
: licensesUsed
.get(licenseId
))
110 sb
.append((licenseId
.equals("") ?
"Proprietary" : licenseId
) + "\t\t" + name
+ "\n");
111 logger
.log(INFO
, "# License summary:\n" + sb
);
114 /** Name of the file centralising information for multiple M2 artifacts. */
115 final static String COMMON_BND
= "common.bnd";
116 /** Name of the file centralising information for mergin M2 artifacts. */
117 final static String MERGE_BND
= "merge.bnd";
119 * Subdirectory of the jar file where origin informations (changes, legal
120 * notices etc. are stored)
122 final static String A2_ORIGIN
= "A2-ORIGIN";
124 /** Directory where to download archives */
125 final Path originBase
;
126 /** Directory where to download Maven artifacts */
127 final Path mavenBase
;
129 /** A2 repository base for binary bundles */
131 /** A2 repository base for source bundles */
132 final Path a2SrcBase
;
133 /** A2 base for native components */
134 final Path a2LibBase
;
135 /** Location of the descriptors driving the packaging */
136 final Path descriptorsBase
;
137 /** URIs of archives to download */
138 final Properties uris
= new Properties();
139 /** Mirrors for archive download. Key is URI prefix, value list of base URLs */
140 final Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
142 /** Whether sources should be packaged separately */
143 final boolean sourceBundles
;
145 /** Constructor initialises the various variables */
146 public Repackage(Path a2Base
, Path descriptorsBase
) {
147 sourceBundles
= Boolean
.parseBoolean(System
.getenv(ENV_SOURCE_BUNDLES
));
149 logger
.log(INFO
, "Sources will be packaged separately");
151 Objects
.requireNonNull(a2Base
);
152 Objects
.requireNonNull(descriptorsBase
);
153 this.originBase
= Paths
.get(System
.getProperty("user.home"), ".cache", "argeo/build/origin");
154 this.mavenBase
= Paths
.get(System
.getProperty("user.home"), ".m2", "repository");
156 // TODO define and use a build base
157 this.a2Base
= a2Base
;
158 this.a2SrcBase
= a2Base
.getParent().resolve(a2Base
.getFileName() + ".src");
159 this.a2LibBase
= a2Base
.resolve("lib");
160 this.descriptorsBase
= descriptorsBase
;
161 if (!Files
.exists(this.descriptorsBase
))
162 throw new IllegalArgumentException(this.descriptorsBase
+ " does not exist");
165 Path urisPath
= this.descriptorsBase
.resolve("uris.properties");
166 if (Files
.exists(urisPath
)) {
167 try (InputStream in
= Files
.newInputStream(urisPath
)) {
169 } catch (IOException e
) {
170 throw new IllegalStateException("Cannot load " + urisPath
, e
);
175 Path eclipseMirrorsPath
= this.descriptorsBase
.resolve("eclipse.mirrors.txt");
176 List
<String
> eclipseMirrors
= new ArrayList
<>();
177 if (Files
.exists(eclipseMirrorsPath
)) {
179 eclipseMirrors
= Files
.readAllLines(eclipseMirrorsPath
, StandardCharsets
.UTF_8
);
180 } catch (IOException e
) {
181 throw new IllegalStateException("Cannot load " + eclipseMirrorsPath
, e
);
183 for (Iterator
<String
> it
= eclipseMirrors
.iterator(); it
.hasNext();) {
184 String value
= it
.next();
185 if (value
.strip().equals(""))
189 mirrors
.put("http://www.eclipse.org/downloads", eclipseMirrors
);
195 /** Process a whole category/group id. */
196 void processCategory(Path categoryRelativePath
) {
198 Path targetCategoryBase
= descriptorsBase
.resolve(categoryRelativePath
);
199 DirectoryStream
<Path
> bnds
= Files
.newDirectoryStream(targetCategoryBase
,
200 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
201 && !p
.getFileName().toString().equals(MERGE_BND
));
202 for (Path p
: bnds
) {
203 processSingleM2ArtifactDistributionUnit(p
);
206 DirectoryStream
<Path
> dus
= Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
));
207 for (Path duDir
: dus
) {
208 if (duDir
.getFileName().toString().startsWith("eclipse-")) {
209 processEclipseArchive(duDir
);
211 processM2BasedDistributionUnit(duDir
);
214 } catch (IOException e
) {
215 throw new RuntimeException("Cannot process category " + categoryRelativePath
, e
);
219 /** Process a standalone Maven artifact. */
220 void processSingleM2ArtifactDistributionUnit(Path bndFile
) {
222 Path categoryRelativePath
= descriptorsBase
.relativize(bndFile
.getParent());
223 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
225 Properties fileProps
= new Properties();
226 try (InputStream in
= Files
.newInputStream(bndFile
)) {
229 String repoStr
= fileProps
.containsKey(ARGEO_ORIGIN_M2_REPO
.toString())
230 ? fileProps
.getProperty(ARGEO_ORIGIN_M2_REPO
.toString())
233 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
234 // use file name as symbolic name
235 String symbolicName
= bndFile
.getFileName().toString();
236 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
237 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
240 String m2Coordinates
= fileProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
241 if (m2Coordinates
== null)
242 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile
);
243 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
244 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
245 Path downloaded
= downloadMaven(url
, artifact
);
247 // some proprietary artifacts do not allow any modification
248 boolean doNotModify
= Boolean
.parseBoolean(
249 fileProps
.getOrDefault(ManifestConstants
.ARGEO_DO_NOT_MODIFY
.toString(), "false").toString());
251 Path unmodifiedTarget
= targetCategoryBase
.resolve(
252 fileProps
.getProperty(BUNDLE_SYMBOLICNAME
.toString()) + "." + artifact
.getBranch() + ".jar");
253 Files
.copy(downloaded
, unmodifiedTarget
);
257 A2Origin origin
= new A2Origin();
258 Path targetBundleDir
= processBndJar(downloaded
, targetCategoryBase
, fileProps
, artifact
, origin
);
260 downloadAndProcessM2Sources(repoStr
, artifact
, targetBundleDir
, false);
262 createJar(targetBundleDir
, origin
);
263 } catch (Exception e
) {
264 throw new RuntimeException("Cannot process " + bndFile
, e
);
269 * Process multiple Maven artifacts coming from a same project and therefore
270 * with information in common (typically the version), generating single bundles
271 * or merging them if necessary.
273 void processM2BasedDistributionUnit(Path duDir
) {
275 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
276 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
278 Path mergeBnd
= duDir
.resolve(MERGE_BND
);
279 if (Files
.exists(mergeBnd
)) // merge
280 mergeM2Artifacts(mergeBnd
);
282 Path commonBnd
= duDir
.resolve(COMMON_BND
);
283 if (!Files
.exists(commonBnd
))
286 Properties commonProps
= new Properties();
287 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
288 commonProps
.load(in
);
291 String m2Version
= commonProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
292 if (m2Version
== null) {
293 logger
.log(WARNING
, "Ignoring " + duDir
+ " as it is not an M2-based distribution unit");
294 return;// ignore, this is probably an Eclipse archive
296 if (!m2Version
.startsWith(":")) {
297 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
299 m2Version
= m2Version
.substring(1);
301 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(duDir
,
302 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
303 && !p
.getFileName().toString().equals(MERGE_BND
));
305 Properties fileProps
= new Properties();
306 try (InputStream in
= Files
.newInputStream(p
)) {
309 String m2Coordinates
= fileProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
310 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
311 artifact
.setVersion(m2Version
);
313 // prepare manifest entries
314 Properties mergeProps
= new Properties();
315 mergeProps
.putAll(commonProps
);
317 fileEntries
: for (Object key
: fileProps
.keySet()) {
318 if (ARGEO_ORIGIN_M2
.toString().equals(key
))
319 continue fileEntries
;
320 String value
= fileProps
.getProperty(key
.toString());
321 Object previousValue
= mergeProps
.put(key
.toString(), value
);
322 if (previousValue
!= null) {
324 commonBnd
+ ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
327 mergeProps
.put(ARGEO_ORIGIN_M2
.toString(), artifact
.toM2Coordinates());
328 if (!mergeProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
329 // use file name as symbolic name
330 String symbolicName
= p
.getFileName().toString();
331 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
332 mergeProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
335 String repoStr
= mergeProps
.containsKey(ARGEO_ORIGIN_M2_REPO
.toString())
336 ? mergeProps
.getProperty(ARGEO_ORIGIN_M2_REPO
.toString())
340 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
341 Path downloaded
= downloadMaven(url
, artifact
);
343 A2Origin origin
= new A2Origin();
344 Path targetBundleDir
= processBndJar(downloaded
, targetCategoryBase
, mergeProps
, artifact
, origin
);
345 downloadAndProcessM2Sources(repoStr
, artifact
, targetBundleDir
, false);
346 createJar(targetBundleDir
, origin
);
348 } catch (IOException e
) {
349 throw new RuntimeException("Cannot process " + duDir
, e
);
353 /** Merge multiple Maven artifacts. */
354 void mergeM2Artifacts(Path mergeBnd
) throws IOException
{
355 Path duDir
= mergeBnd
.getParent();
356 String category
= duDir
.getParent().getFileName().toString();
357 Path targetCategoryBase
= a2Base
.resolve(category
);
359 Properties mergeProps
= new Properties();
360 try (InputStream in
= Files
.newInputStream(mergeBnd
)) {
364 String m2Version
= mergeProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
365 if (m2Version
== null) {
366 logger
.log(WARNING
, "Ignoring " + duDir
+ " as it is not an M2-based distribution unit");
367 return;// ignore, this is probably an Eclipse archive
369 if (!m2Version
.startsWith(":")) {
370 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
372 m2Version
= m2Version
.substring(1);
373 mergeProps
.put(BUNDLE_VERSION
.toString(), m2Version
);
375 String artifactsStr
= mergeProps
.getProperty(ARGEO_ORIGIN_M2_MERGE
.toString());
376 if (artifactsStr
== null)
377 throw new IllegalArgumentException(mergeBnd
+ ": " + ARGEO_ORIGIN_M2_MERGE
+ " must be set");
379 String repoStr
= mergeProps
.containsKey(ARGEO_ORIGIN_M2_REPO
.toString())
380 ? mergeProps
.getProperty(ARGEO_ORIGIN_M2_REPO
.toString())
383 String bundleSymbolicName
= mergeProps
.getProperty(BUNDLE_SYMBOLICNAME
.toString());
384 if (bundleSymbolicName
== null)
385 throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd
);
386 CategoryNameVersion nameVersion
= new M2Artifact(category
+ ":" + bundleSymbolicName
+ ":" + m2Version
);
388 A2Origin origin
= new A2Origin();
389 Path bundleDir
= targetCategoryBase
.resolve(bundleSymbolicName
+ "." + nameVersion
.getBranch());
391 String
[] artifacts
= artifactsStr
.split(",");
392 artifacts
: for (String str
: artifacts
) {
393 String m2Coordinates
= str
.trim();
394 if ("".equals(m2Coordinates
))
396 M2Artifact artifact
= new M2Artifact(m2Coordinates
.trim());
397 if (artifact
.getVersion() == null)
398 artifact
.setVersion(m2Version
);
399 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
400 Path downloaded
= downloadMaven(url
, artifact
);
402 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(downloaded
), false)) {
403 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
404 if (entry
.isDirectory())
406 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".SF")) {
407 origin
.deleted
.add("cryptographic signatures from " + artifact
);
410 if (entry
.getName().endsWith("module-info.class")) { // skip Java 9 module info
411 origin
.deleted
.add("Java module information (module-info.class) from " + artifact
);
414 if (entry
.getName().startsWith("META-INF/versions/")) { // skip multi-version
415 origin
.deleted
.add("additional Java versions (META-INF/versions) from " + artifact
);
418 if (entry
.getName().startsWith("META-INF/maven/")) {
419 origin
.deleted
.add("Maven information (META-INF/maven) from " + artifact
);
422 if (entry
.getName().startsWith(".cache/")) { // Apache SSHD
423 origin
.deleted
.add("cache directory (.cache) from " + artifact
);
426 if (entry
.getName().equals("META-INF/DEPENDENCIES")) {
427 origin
.deleted
.add("Dependencies (META-INF/DEPENDENCIES) from " + artifact
);
430 if (entry
.getName().equals("META-INF/MANIFEST.MF")) {
431 Path originalManifest
= bundleDir
.resolve(A2_ORIGIN
).resolve(artifact
.getGroupId())
432 .resolve(artifact
.getArtifactId()).resolve("MANIFEST.MF");
433 Files
.createDirectories(originalManifest
.getParent());
434 try (OutputStream out
= Files
.newOutputStream(originalManifest
)) {
435 Files
.copy(jarIn
, originalManifest
);
438 "original MANIFEST (" + bundleDir
.relativize(originalManifest
) + ") from " + artifact
);
442 if (entry
.getName().endsWith("NOTICE") || entry
.getName().endsWith("NOTICE.txt")
443 || entry
.getName().endsWith("LICENSE") || entry
.getName().endsWith("LICENSE.md")
444 || entry
.getName().endsWith("LICENSE-notice.md") || entry
.getName().endsWith("COPYING")
445 || entry
.getName().endsWith("COPYING.LESSER")) {
446 Path artifactOriginDir
= bundleDir
.resolve(A2_ORIGIN
).resolve(artifact
.getGroupId())
447 .resolve(artifact
.getArtifactId());
448 Path target
= artifactOriginDir
.resolve(entry
.getName());
449 Files
.createDirectories(target
.getParent());
450 Files
.copy(jarIn
, target
);
451 origin
.moved
.add(entry
.getName() + " in " + artifact
+ " to " + bundleDir
.relativize(target
));
454 Path target
= bundleDir
.resolve(entry
.getName());
455 Files
.createDirectories(target
.getParent());
456 if (!Files
.exists(target
)) {
457 Files
.copy(jarIn
, target
);
459 if (entry
.getName().startsWith("META-INF/services/")) {
460 try (OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
)) {
461 out
.write("\n".getBytes());
462 jarIn
.transferTo(out
);
463 logger
.log(DEBUG
, artifact
.getArtifactId() + " - Appended " + entry
.getName());
465 origin
.modified
.add(entry
.getName() + ", merging from " + artifact
);
466 } else if (entry
.getName().startsWith("org/apache/batik/")) {
467 logger
.log(TRACE
, "Skip " + entry
.getName());
470 throw new IllegalStateException("File " + target
+ " from " + artifact
+ " already exists");
473 logger
.log(TRACE
, () -> "Copied " + target
);
476 origin
.added
.add("binary content of " + artifact
);
479 downloadAndProcessM2Sources(repoStr
, artifact
, bundleDir
, true);
482 // additional service files
483 Path servicesDir
= duDir
.resolve("services");
484 if (Files
.exists(servicesDir
)) {
485 for (Path p
: Files
.newDirectoryStream(servicesDir
)) {
486 Path target
= bundleDir
.resolve("META-INF/services/").resolve(p
.getFileName());
487 try (InputStream in
= Files
.newInputStream(p
);
488 OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
);) {
489 out
.write("\n".getBytes());
491 logger
.log(DEBUG
, "Appended " + p
);
493 origin
.added
.add(bundleDir
.relativize(target
).toString());
497 Map
<String
, String
> entries
= new TreeMap
<>();
498 try (Analyzer bndAnalyzer
= new Analyzer()) {
499 bndAnalyzer
.setProperties(mergeProps
);
500 Jar jar
= new Jar(bundleDir
.toFile());
501 bndAnalyzer
.setJar(jar
);
502 Manifest manifest
= bndAnalyzer
.calcManifest();
504 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
505 Object value
= manifest
.getMainAttributes().get(key
);
507 switch (key
.toString()) {
509 case "Bnd-LastModified":
513 if ("Require-Capability".equals(key
.toString())
514 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
515 continue keys
;// hack for very old classes
516 entries
.put(key
.toString(), value
.toString());
518 } catch (Exception e
) {
519 throw new RuntimeException("Cannot process " + mergeBnd
, e
);
522 Manifest manifest
= new Manifest();
523 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
524 Files
.createDirectories(manifestPath
.getParent());
525 for (String key
: entries
.keySet()) {
526 String value
= entries
.get(key
);
527 manifest
.getMainAttributes().putValue(key
, value
);
530 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
533 createJar(bundleDir
, origin
);
536 /** Generate MANIFEST using BND. */
537 Path
processBndJar(Path downloaded
, Path targetCategoryBase
, Properties fileProps
, M2Artifact artifact
,
540 Map
<String
, String
> additionalEntries
= new TreeMap
<>();
541 boolean doNotModifyManifest
= Boolean
.parseBoolean(
542 fileProps
.getOrDefault(ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED
.toString(), "false").toString());
544 // Note: we always force the symbolic name
545 if (doNotModifyManifest
) {
546 fileEntries
: for (Object key
: fileProps
.keySet()) {
547 if (ARGEO_ORIGIN_M2
.toString().equals(key
))
548 continue fileEntries
;
549 String value
= fileProps
.getProperty(key
.toString());
550 additionalEntries
.put(key
.toString(), value
);
553 if (artifact
!= null) {
554 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
555 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), artifact
.getName());
557 if (!fileProps
.containsKey(BUNDLE_VERSION
.toString())) {
558 fileProps
.put(BUNDLE_VERSION
.toString(), artifact
.getVersion());
562 if (!fileProps
.containsKey(EXPORT_PACKAGE
.toString())) {
563 fileProps
.put(EXPORT_PACKAGE
.toString(),
564 "*;version=\"" + fileProps
.getProperty(BUNDLE_VERSION
.toString()) + "\"");
567 try (Analyzer bndAnalyzer
= new Analyzer()) {
568 bndAnalyzer
.setProperties(fileProps
);
569 Jar jar
= new Jar(downloaded
.toFile());
570 bndAnalyzer
.setJar(jar
);
571 Manifest manifest
= bndAnalyzer
.calcManifest();
573 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
574 Object value
= manifest
.getMainAttributes().get(key
);
576 switch (key
.toString()) {
578 case "Bnd-LastModified":
582 if ("Require-Capability".equals(key
.toString())
583 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
584 continue keys
;// !! hack for very old classes
585 additionalEntries
.put(key
.toString(), value
.toString());
589 Path targetBundleDir
= processBundleJar(downloaded
, targetCategoryBase
, additionalEntries
, origin
);
590 logger
.log(DEBUG
, () -> "Processed " + downloaded
);
591 return targetBundleDir
;
592 } catch (Exception e
) {
593 throw new RuntimeException("Cannot BND process " + downloaded
, e
);
598 /** Download and integrates sources for a single Maven artifact. */
599 void downloadAndProcessM2Sources(String repoStr
, M2Artifact artifact
, Path targetBundleDir
, boolean merging
)
602 M2Artifact sourcesArtifact
= new M2Artifact(artifact
.toM2Coordinates(), "sources");
603 URL sourcesUrl
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, sourcesArtifact
);
604 Path sourcesDownloaded
= downloadMaven(sourcesUrl
, artifact
, true);
605 processM2SourceJar(sourcesDownloaded
, targetBundleDir
, merging ? artifact
: null);
606 logger
.log(TRACE
, () -> "Processed source " + sourcesDownloaded
);
607 } catch (Exception e
) {
608 logger
.log(ERROR
, () -> "Cannot download source for " + artifact
);
613 /** Integrate sources from a downloaded jar file. */
614 void processM2SourceJar(Path file
, Path bundleDir
, M2Artifact mergingFrom
) throws IOException
{
615 A2Origin origin
= new A2Origin();
616 Path sourceDir
= sourceBundles ? bundleDir
.getParent().resolve(bundleDir
.toString() + ".src")
617 : bundleDir
.resolve("OSGI-OPT/src");
618 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
620 String mergingMsg
= "";
621 if (mergingFrom
!= null)
622 mergingMsg
= " of " + mergingFrom
;
624 Files
.createDirectories(sourceDir
);
626 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
627 if (entry
.isDirectory())
629 if (entry
.getName().startsWith("META-INF")) {// skip META-INF entries
630 origin
.deleted
.add("META-INF directory from the sources" + mergingMsg
);
633 if (entry
.getName().startsWith("module-info.java")) {// skip Java module information
634 origin
.deleted
.add("Java module information from the sources (module-info.java)" + mergingMsg
);
637 if (entry
.getName().startsWith("/")) { // absolute paths
638 // TODO does it really happen?
639 logger
.log(WARNING
, entry
.getName() + " has an absolute path");
640 origin
.deleted
.add(entry
.getName() + " from the sources" + mergingMsg
);
643 Path target
= sourceDir
.resolve(entry
.getName());
644 Files
.createDirectories(target
.getParent());
645 if (!Files
.exists(target
)) {
646 Files
.copy(jarIn
, target
);
647 logger
.log(TRACE
, () -> "Copied source " + target
);
649 logger
.log(TRACE
, () -> target
+ " already exists, skipping...");
655 origin
.appendChanges(sourceDir
);
657 origin
.added
.add("source code under OSGI-OPT/src");
658 origin
.appendChanges(bundleDir
);
662 /** Download a Maven artifact. */
663 Path
downloadMaven(URL url
, M2Artifact artifact
) throws IOException
{
664 return downloadMaven(url
, artifact
, false);
667 /** Download a Maven artifact. */
668 Path
downloadMaven(URL url
, M2Artifact artifact
, boolean sources
) throws IOException
{
669 return download(url
, mavenBase
, artifact
.getGroupId().replace(".", "/") //
670 + '/' + artifact
.getArtifactId() + '/' + artifact
.getVersion() //
671 + '/' + artifact
.getArtifactId() + "-" + artifact
.getVersion() + (sources ?
"-sources" : "") + ".jar");
677 /** Process an archive in Eclipse format. */
678 void processEclipseArchive(Path duDir
) {
680 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
681 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
682 Files
.createDirectories(targetCategoryBase
);
683 // first delete all directories from previous builds
684 for (Path dir
: Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
)))
685 deleteDirectory(dir
);
687 Files
.createDirectories(originBase
);
689 Path commonBnd
= duDir
.resolve(COMMON_BND
);
690 Properties commonProps
= new Properties();
691 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
692 commonProps
.load(in
);
694 String url
= commonProps
.getProperty(ARGEO_ORIGIN_URI
.toString());
696 url
= uris
.getProperty(duDir
.getFileName().toString());
698 throw new IllegalStateException("No url available for " + duDir
);
699 commonProps
.put(ARGEO_ORIGIN_URI
.toString(), url
);
701 Path downloaded
= tryDownloadArchive(url
, originBase
);
703 FileSystem zipFs
= FileSystems
.newFileSystem(downloaded
, (ClassLoader
) null);
706 List
<PathMatcher
> includeMatchers
= new ArrayList
<>();
707 Properties includes
= new Properties();
708 try (InputStream in
= Files
.newInputStream(duDir
.resolve("includes.properties"))) {
711 for (Object pattern
: includes
.keySet()) {
712 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
713 includeMatchers
.add(pathMatcher
);
716 List
<PathMatcher
> excludeMatchers
= new ArrayList
<>();
717 Path excludeFile
= duDir
.resolve("excludes.properties");
718 if (Files
.exists(excludeFile
)) {
719 Properties excludes
= new Properties();
720 try (InputStream in
= Files
.newInputStream(excludeFile
)) {
723 for (Object pattern
: excludes
.keySet()) {
724 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
725 excludeMatchers
.add(pathMatcher
);
729 // keys are the bundle directories
730 Map
<Path
, A2Origin
> origins
= new HashMap
<>();
731 Files
.walkFileTree(zipFs
.getRootDirectories().iterator().next(), new SimpleFileVisitor
<Path
>() {
734 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
735 includeMatchers
: for (PathMatcher includeMatcher
: includeMatchers
) {
736 if (includeMatcher
.matches(file
)) {
737 for (PathMatcher excludeMatcher
: excludeMatchers
) {
738 if (excludeMatcher
.matches(file
)) {
739 logger
.log(TRACE
, "Skipping excluded " + file
);
740 return FileVisitResult
.CONTINUE
;
743 if (file
.getFileName().toString().contains(".source_")) {
744 processEclipseSourceJar(file
, targetCategoryBase
);
745 logger
.log(DEBUG
, () -> "Processed source " + file
);
747 Map
<String
, String
> map
= new HashMap
<>();
748 for (Object key
: commonProps
.keySet())
749 map
.put(key
.toString(), commonProps
.getProperty(key
.toString()));
750 A2Origin origin
= new A2Origin();
751 Path bundleDir
= processBundleJar(file
, targetCategoryBase
, map
, origin
);
752 origins
.put(bundleDir
, origin
);
753 logger
.log(DEBUG
, () -> "Processed " + file
);
755 break includeMatchers
;
758 return FileVisitResult
.CONTINUE
;
762 DirectoryStream
<Path
> dirs
= Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
)
763 && p
.getFileName().toString().indexOf('.') >= 0 && !p
.getFileName().toString().endsWith(".src"));
764 for (Path bundleDir
: dirs
) {
765 A2Origin origin
= origins
.get(bundleDir
);
766 Objects
.requireNonNull(origin
, "No A2 origin found for " + bundleDir
);
767 createJar(bundleDir
, origin
);
769 } catch (IOException e
) {
770 throw new RuntimeException("Cannot process " + duDir
, e
);
775 /** Process sources in Eclipse format. */
776 void processEclipseSourceJar(Path file
, Path targetBase
) throws IOException
{
778 A2Origin origin
= new A2Origin();
780 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
781 Manifest manifest
= jarIn
.getManifest();
783 String
[] relatedBundle
= manifest
.getMainAttributes().getValue(ECLIPSE_SOURCE_BUNDLE
.toString())
785 String version
= relatedBundle
[1].substring("version=\"".length());
786 version
= version
.substring(0, version
.length() - 1);
787 NameVersion nameVersion
= new NameVersion(relatedBundle
[0], version
);
788 bundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
790 Path sourceDir
= sourceBundles ? bundleDir
.getParent().resolve(bundleDir
.toString() + ".src")
791 : bundleDir
.resolve("OSGI-OPT/src");
793 Files
.createDirectories(sourceDir
);
795 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
796 if (entry
.isDirectory())
798 if (entry
.getName().startsWith("META-INF"))// skip META-INF entries
800 Path target
= sourceDir
.resolve(entry
.getName());
801 Files
.createDirectories(target
.getParent());
802 Files
.copy(jarIn
, target
);
803 logger
.log(TRACE
, () -> "Copied source " + target
);
808 origin
.appendChanges(sourceDir
);
810 origin
.added
.add("source code under OSGI-OPT/src");
811 origin
.appendChanges(bundleDir
);
814 } catch (IOException e
) {
815 throw new IllegalStateException("Cannot process " + file
, e
);
822 /** Normalise a bundle. */
823 Path
processBundleJar(Path file
, Path targetBase
, Map
<String
, String
> entries
, A2Origin origin
) throws IOException
{
824 boolean embed
= Boolean
.parseBoolean(entries
.getOrDefault(ARGEO_ORIGIN_EMBED
.toString(), "false").toString());
825 NameVersion nameVersion
;
828 boolean isSingleton
= false;
830 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
831 Manifest sourceManifest
= jarIn
.getManifest();
832 manifest
= sourceManifest
!= null ?
new Manifest(sourceManifest
) : new Manifest();
834 String rawSourceSymbolicName
= manifest
.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME
.toString());
835 if (rawSourceSymbolicName
!= null) {
836 // make sure there is no directive
837 String
[] arr
= rawSourceSymbolicName
.split(";");
838 for (int i
= 1; i
< arr
.length
; i
++) {
839 if (arr
[i
].trim().equals("singleton:=true"))
841 logger
.log(DEBUG
, file
.getFileName() + " is a singleton");
844 // remove problematic entries in MANIFEST
845 manifest
.getEntries().clear();
847 String ourSymbolicName
= entries
.get(BUNDLE_SYMBOLICNAME
.toString());
848 String ourVersion
= entries
.get(BUNDLE_VERSION
.toString());
850 if (ourSymbolicName
!= null && ourVersion
!= null) {
851 nameVersion
= new NameVersion(ourSymbolicName
, ourVersion
);
853 nameVersion
= nameVersionFromManifest(manifest
);
854 if (ourVersion
!= null && !nameVersion
.getVersion().equals(ourVersion
)) {
856 "Original version is " + nameVersion
.getVersion() + " while new version is " + ourVersion
);
857 entries
.put(BUNDLE_VERSION
.toString(), ourVersion
);
859 if (ourSymbolicName
!= null) {
860 // we always force our symbolic name
861 nameVersion
.setName(ourSymbolicName
);
864 bundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
866 if (sourceManifest
!= null) {// copy original MANIFEST
867 Path originalManifest
= bundleDir
.resolve(A2_ORIGIN
).resolve("MANIFEST.MF");
868 Files
.createDirectories(originalManifest
.getParent());
869 try (OutputStream out
= Files
.newOutputStream(originalManifest
)) {
870 sourceManifest
.write(out
);
872 origin
.added
.add("original MANIFEST (" + bundleDir
.relativize(originalManifest
) + ")");
875 // force Java 9 module name
876 entries
.put(ManifestConstants
.AUTOMATIC_MODULE_NAME
.toString(), nameVersion
.getName());
878 boolean isNative
= false;
881 if (bundleDir
.startsWith(a2LibBase
)) {
883 Path libRelativePath
= a2LibBase
.relativize(bundleDir
);
884 os
= libRelativePath
.getName(0).toString();
885 arch
= libRelativePath
.getName(1).toString();
891 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
892 if (entry
.isDirectory())
894 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".SF")) {
895 origin
.deleted
.add("cryptographic signatures");
898 if (entry
.getName().endsWith("module-info.class")) { // skip Java 9 module info
899 origin
.deleted
.add("Java module information (module-info.class)");
902 if (entry
.getName().startsWith("META-INF/versions/")) { // skip multi-version
903 origin
.deleted
.add("additional Java versions (META-INF/versions)");
906 if (entry
.getName().startsWith("META-INF/maven/")) {
907 origin
.deleted
.add("Maven information (META-INF/maven)");
910 // skip file system providers as they cause issues with native image
911 if (entry
.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider")) {
913 .add("file system providers (META-INF/services/java.nio.file.spi.FileSystemProvider)");
916 if (entry
.getName().startsWith("OSGI-OPT/src/")) { // skip embedded sources
917 origin
.deleted
.add("embedded sources");
920 Path target
= bundleDir
.resolve(entry
.getName());
921 Files
.createDirectories(target
.getParent());
922 Files
.copy(jarIn
, target
);
925 if (isNative
&& (entry
.getName().endsWith(".so") || entry
.getName().endsWith(".dll")
926 || entry
.getName().endsWith(".jnilib"))) {
927 Path categoryDir
= bundleDir
.getParent();
928 boolean copyDll
= false;
929 Path targetDll
= categoryDir
.resolve(bundleDir
.relativize(target
));
930 if (nameVersion
.getName().equals("com.sun.jna")) {
931 if (arch
.equals("x86_64"))
933 if (os
.equals("macosx"))
935 if (target
.getParent().getFileName().toString().equals(os
+ "-" + arch
)) {
938 targetDll
= categoryDir
.resolve(target
.getFileName());
943 Files
.createDirectories(targetDll
.getParent());
944 if (Files
.exists(targetDll
))
945 Files
.delete(targetDll
);
946 Files
.copy(target
, targetDll
);
948 Files
.delete(target
);
949 origin
.deleted
.add(bundleDir
.relativize(target
).toString());
951 logger
.log(TRACE
, () -> "Copied " + target
);
957 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
958 Files
.createDirectories(manifestPath
.getParent());
960 if (isSingleton
&& entries
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
961 entries
.put(BUNDLE_SYMBOLICNAME
.toString(),
962 entries
.get(BUNDLE_SYMBOLICNAME
.toString()) + ";singleton:=true");
965 if (embed
) {// copy embedded jar
966 Files
.copy(file
, bundleDir
.resolve(file
.getFileName()));
967 entries
.put(ManifestConstants
.BUNDLE_CLASSPATH
.toString(), file
.getFileName().toString());
970 // Final MANIFEST decisions
971 // This also where we check the original OSGi metadata and compare with our
973 for (String key
: entries
.keySet()) {
974 String value
= entries
.get(key
);
975 String previousValue
= manifest
.getMainAttributes().getValue(key
);
976 boolean wasDifferent
= previousValue
!= null && !previousValue
.equals(value
);
977 boolean keepPrevious
= false;
979 if (SPDX_LICENSE_IDENTIFIER
.toString().equals(key
) && previousValue
!= null)
981 else if (BUNDLE_VERSION
.toString().equals(key
) && wasDifferent
)
982 if (previousValue
.equals(value
+ ".0")) // typically a Maven first release
986 if (logger
.isLoggable(DEBUG
))
987 logger
.log(DEBUG
, file
.getFileName() + ": " + key
+ " was NOT modified, value kept is "
988 + previousValue
+ ", not overriden with " + value
);
989 value
= previousValue
;
993 manifest
.getMainAttributes().putValue(key
, value
);
994 if (wasDifferent
&& !keepPrevious
) {
995 if (IMPORT_PACKAGE
.toString().equals(key
) || EXPORT_PACKAGE
.toString().equals(key
))
996 logger
.log(TRACE
, () -> file
.getFileName() + ": " + key
+ " was modified");
999 file
.getFileName() + ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
1002 // de-pollute MANIFEST
1004 case "Archiver-Version":
1007 case "Originally-Created-By":
1009 case "Bnd-LastModified":
1010 manifest
.getMainAttributes().remove(key
);
1012 default: // do nothing
1015 // !! hack to remove unresolvable
1016 if (key
.equals("Provide-Capability") || key
.equals("Require-Capability"))
1017 if (nameVersion
.getName().equals("osgi.core") || nameVersion
.getName().equals("osgi.cmpn")) {
1018 manifest
.getMainAttributes().remove(key
);
1023 String spdxLicenceId
= manifest
.getMainAttributes().getValue(SPDX_LICENSE_IDENTIFIER
.toString());
1024 String bundleLicense
= manifest
.getMainAttributes().getValue(BUNDLE_LICENSE
.toString());
1025 if (spdxLicenceId
== null) {
1026 logger
.log(ERROR
, file
.getFileName() + ": " + SPDX_LICENSE_IDENTIFIER
+ " not available, " + BUNDLE_LICENSE
1027 + " is " + bundleLicense
);
1029 if (!licensesUsed
.containsKey(spdxLicenceId
))
1030 licensesUsed
.put(spdxLicenceId
, new TreeSet
<>());
1031 licensesUsed
.get(spdxLicenceId
).add(nameVersion
.toString());
1034 origin
.modified
.add("jar MANIFEST (META-INF/MANIFEST.MF)");
1035 // write the MANIFEST
1036 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
1037 manifest
.write(out
);
1045 /** Recursively deletes a directory. */
1046 static void deleteDirectory(Path path
) throws IOException
{
1047 if (!Files
.exists(path
))
1049 Files
.walkFileTree(path
, new SimpleFileVisitor
<Path
>() {
1051 public FileVisitResult
postVisitDirectory(Path directory
, IOException e
) throws IOException
{
1054 Files
.delete(directory
);
1059 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1066 /** Extract name/version from a MANIFEST. */
1067 NameVersion
nameVersionFromManifest(Manifest manifest
) {
1068 Attributes attrs
= manifest
.getMainAttributes();
1070 String symbolicName
= attrs
.getValue(ManifestConstants
.BUNDLE_SYMBOLICNAME
.toString());
1071 if (symbolicName
== null)
1073 // make sure there is no directive
1074 symbolicName
= symbolicName
.split(";")[0];
1076 String version
= attrs
.getValue(ManifestConstants
.BUNDLE_VERSION
.toString());
1077 return new NameVersion(symbolicName
, version
);
1080 /** Try to download from an URI. */
1081 Path
tryDownloadArchive(String uri
, Path dir
) throws IOException
{
1083 List
<String
> urlBases
= null;
1084 String uriPrefix
= null;
1085 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
1086 if (uri
.startsWith(uriPref
)) {
1087 if (mirrors
.get(uriPref
).size() > 0) {
1088 urlBases
= mirrors
.get(uriPref
);
1089 uriPrefix
= uriPref
;
1094 if (urlBases
== null)
1096 return downloadArchive(new URL(uri
), dir
);
1097 } catch (FileNotFoundException e
) {
1098 throw new FileNotFoundException("Cannot find " + uri
);
1102 for (String urlBase
: urlBases
) {
1103 String relativePath
= uri
.substring(uriPrefix
.length());
1104 URL url
= new URL(urlBase
+ relativePath
);
1106 return downloadArchive(url
, dir
);
1107 } catch (FileNotFoundException e
) {
1108 logger
.log(WARNING
, "Cannot download " + url
+ ", trying another mirror");
1111 throw new FileNotFoundException("Cannot find " + uri
);
1115 * Effectively download. Synchronised in order to avoid downloading twice in
1118 synchronized Path
downloadArchive(URL url
, Path dir
) throws IOException
{
1119 return download(url
, dir
, (String
) null);
1122 /** Effectively download. */
1123 Path
download(URL url
, Path dir
, String name
) throws IOException
{
1127 // We use also use parent directory in case the archive itself has a fixed name
1128 String
[] segments
= url
.getPath().split("/");
1129 name
= segments
.length
> 1 ? segments
[segments
.length
- 2] + '-' + segments
[segments
.length
- 1]
1130 : segments
[segments
.length
- 1];
1133 dest
= dir
.resolve(name
);
1134 if (Files
.exists(dest
)) {
1135 logger
.log(TRACE
, () -> "File " + dest
+ " already exists for " + url
+ ", not downloading again");
1138 Files
.createDirectories(dest
.getParent());
1141 try (InputStream in
= url
.openStream()) {
1142 Files
.copy(in
, dest
);
1143 logger
.log(DEBUG
, () -> "Downloaded " + dest
+ " from " + url
);
1148 /** Create a JAR file from a directory. */
1149 Path
createJar(Path bundleDir
, A2Origin origin
) throws IOException
{
1151 origin
.appendChanges(bundleDir
);
1153 Path jarPath
= bundleDir
.getParent().resolve(bundleDir
.getFileName() + ".jar");
1154 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
1156 try (InputStream in
= Files
.newInputStream(manifestPath
)) {
1157 manifest
= new Manifest(in
);
1159 try (JarOutputStream jarOut
= new JarOutputStream(Files
.newOutputStream(jarPath
), manifest
)) {
1160 jarOut
.setLevel(Deflater
.DEFAULT_COMPRESSION
);
1161 Files
.walkFileTree(bundleDir
, new SimpleFileVisitor
<Path
>() {
1164 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1165 if (file
.getFileName().toString().equals("MANIFEST.MF"))
1166 return super.visitFile(file
, attrs
);
1167 JarEntry entry
= new JarEntry(
1168 bundleDir
.relativize(file
).toString().replace(File
.separatorChar
, '/'));
1169 jarOut
.putNextEntry(entry
);
1170 Files
.copy(file
, jarOut
);
1171 return super.visitFile(file
, attrs
);
1176 deleteDirectory(bundleDir
);
1178 if (sourceBundles
) {
1179 Path bundleCategoryDir
= bundleDir
.getParent();
1180 Path sourceDir
= bundleCategoryDir
.resolve(bundleDir
.toString() + ".src");
1181 if (!Files
.exists(sourceDir
)) {
1182 logger
.log(WARNING
, sourceDir
+ " does not exist, skipping...");
1187 Path relPath
= a2Base
.relativize(bundleCategoryDir
);
1188 Path srcCategoryDir
= a2SrcBase
.resolve(relPath
);
1189 Path srcJarP
= srcCategoryDir
.resolve(sourceDir
.getFileName() + ".jar");
1190 Files
.createDirectories(srcJarP
.getParent());
1192 String bundleSymbolicName
= manifest
.getMainAttributes().getValue("Bundle-SymbolicName").toString();
1193 // in case there are additional directives
1194 bundleSymbolicName
= bundleSymbolicName
.split(";")[0];
1195 Manifest srcManifest
= new Manifest();
1196 srcManifest
.getMainAttributes().put(MANIFEST_VERSION
, "1.0");
1197 srcManifest
.getMainAttributes().putValue(BUNDLE_SYMBOLICNAME
.toString(), bundleSymbolicName
+ ".src");
1198 srcManifest
.getMainAttributes().putValue(BUNDLE_VERSION
.toString(),
1199 manifest
.getMainAttributes().getValue(BUNDLE_VERSION
.toString()).toString());
1200 srcManifest
.getMainAttributes().putValue(ECLIPSE_SOURCE_BUNDLE
.toString(), bundleSymbolicName
1201 + ";version=\"" + manifest
.getMainAttributes().getValue(BUNDLE_VERSION
.toString()));
1203 try (JarOutputStream srcJarOut
= new JarOutputStream(Files
.newOutputStream(srcJarP
), srcManifest
)) {
1204 srcJarOut
.setLevel(Deflater
.BEST_COMPRESSION
);
1205 Files
.walkFileTree(sourceDir
, new SimpleFileVisitor
<Path
>() {
1208 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1209 if (file
.getFileName().toString().equals("MANIFEST.MF"))
1210 return super.visitFile(file
, attrs
);
1211 JarEntry entry
= new JarEntry(
1212 sourceDir
.relativize(file
).toString().replace(File
.separatorChar
, '/'));
1213 srcJarOut
.putNextEntry(entry
);
1214 Files
.copy(file
, srcJarOut
);
1215 return super.visitFile(file
, attrs
);
1220 deleteDirectory(sourceDir
);
1226 /** MANIFEST headers. */
1227 enum ManifestConstants
{
1229 /** OSGi bundle symbolic name. */
1230 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
1231 /** OSGi bundle version. */
1232 BUNDLE_VERSION("Bundle-Version"), //
1233 /** OSGi bundle license. */
1234 BUNDLE_LICENSE("Bundle-License"), //
1235 /** OSGi exported packages list. */
1236 EXPORT_PACKAGE("Export-Package"), //
1237 /** OSGi imported packages list. */
1238 IMPORT_PACKAGE("Import-Package"), //
1239 /** OSGi path to embedded jar. */
1240 BUNDLE_CLASSPATH("Bundle-Classpath"), //
1242 /** Java module name. */
1243 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
1245 /** Eclipse source bundle. */
1246 ECLIPSE_SOURCE_BUNDLE("Eclipse-SourceBundle"), //
1249 * SPDX license identifier.
1251 * @see https://spdx.org/licenses/
1253 SPDX_LICENSE_IDENTIFIER("SPDX-License-Identifier"), //
1256 * Maven coordinates of the origin, possibly partial when using common.bnd or
1259 ARGEO_ORIGIN_M2("Argeo-Origin-M2"), //
1260 /** List of Maven coordinates to merge. */
1261 ARGEO_ORIGIN_M2_MERGE("Argeo-Origin-M2-Merge"), //
1262 /** Maven repository, if not the default one. */
1263 ARGEO_ORIGIN_M2_REPO("Argeo-Origin-M2-Repo"), //
1265 * Do not perform BND analysis of the origin component. Typically IMport_package
1266 * and Export-Package will be kept untouched.
1268 ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED("Argeo-Origin-ManifestNotModified"), //
1270 * Embed the original jar without modifying it (may be required by some
1271 * proprietary licenses, such as JCR Day License).
1273 ARGEO_ORIGIN_EMBED("Argeo-Origin-Embed"), //
1275 * Do not modify original jar (may be required by some proprietary licenses,
1276 * such as JCR Day License).
1278 ARGEO_DO_NOT_MODIFY("Argeo-Origin-Do-Not-Modify"), //
1280 * Origin (non-Maven) URI of the component. It may be anything (jar, archive,
1283 ARGEO_ORIGIN_URI("Argeo-Origin-URI"), //
1288 private ManifestConstants(String value
) {
1293 public String
toString() {
1300 * Gathers modifications performed on the original binaries and sources,
1301 * especially in order to comply with their license requirements.
1304 Set
<String
> modified
= new TreeSet
<>();
1305 Set
<String
> deleted
= new TreeSet
<>();
1306 Set
<String
> added
= new TreeSet
<>();
1307 Set
<String
> moved
= new TreeSet
<>();
1309 /** Append changes to the A2-ORIGIN/changes file. */
1310 void appendChanges(Path baseDirectory
) throws IOException
{
1311 Path changesFile
= baseDirectory
.resolve("A2-ORIGIN/changes");
1312 Files
.createDirectories(changesFile
.getParent());
1313 try (BufferedWriter writer
= Files
.newBufferedWriter(changesFile
, StandardOpenOption
.APPEND
,
1314 StandardOpenOption
.CREATE
)) {
1315 for (String msg
: added
)
1316 writer
.write("- Added " + msg
+ ".\n");
1317 for (String msg
: modified
)
1318 writer
.write("- Modified " + msg
+ ".\n");
1319 for (String msg
: moved
)
1320 writer
.write("- Moved " + msg
+ ".\n");
1321 for (String msg
: deleted
)
1322 writer
.write("- Deleted " + msg
+ ".\n");
1327 /** Simple representation of an M2 artifact. */
1328 class M2Artifact
extends CategoryNameVersion
{
1329 private String classifier
;
1331 M2Artifact(String m2coordinates
) {
1332 this(m2coordinates
, null);
1335 M2Artifact(String m2coordinates
, String classifier
) {
1336 String
[] parts
= m2coordinates
.split(":");
1337 setCategory(parts
[0]);
1339 if (parts
.length
> 2) {
1340 setVersion(parts
[2]);
1342 this.classifier
= classifier
;
1345 String
getGroupId() {
1346 return super.getCategory();
1349 String
getArtifactId() {
1350 return super.getName();
1353 String
toM2Coordinates() {
1354 return getCategory() + ":" + getName() + (getVersion() != null ?
":" + getVersion() : "");
1357 String
getClassifier() {
1358 return classifier
!= null ? classifier
: "";
1361 String
getExtension() {
1366 /** Utilities around Maven (conventions based). */
1367 class M2ConventionsUtils
{
1368 final static String MAVEN_CENTRAL_BASE_URL
= "https://repo1.maven.org/maven2/";
1370 /** The file name of this artifact when stored */
1371 static String
artifactFileName(M2Artifact artifact
) {
1372 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
1373 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
1374 + artifact
.getExtension();
1377 /** Absolute path to the file */
1378 static String
artifactPath(String artifactBasePath
, M2Artifact artifact
) {
1379 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
1382 /** Absolute path to the file */
1383 static String
artifactUrl(String repoUrl
, M2Artifact artifact
) {
1384 if (repoUrl
.endsWith("/"))
1385 return repoUrl
+ artifactPath("/", artifact
).substring(1);
1387 return repoUrl
+ artifactPath("/", artifact
);
1390 /** Absolute path to the file */
1391 static URL
mavenRepoUrl(String repoBase
, M2Artifact artifact
) {
1392 String url
= artifactUrl(repoBase
== null ? MAVEN_CENTRAL_BASE_URL
: repoBase
, artifact
);
1394 return new URL(url
);
1395 } catch (MalformedURLException e
) {
1396 // it should not happen
1397 throw new IllegalStateException(e
);
1401 /** Absolute path to the directories where the files will be stored */
1402 static String
artifactParentPath(String artifactBasePath
, M2Artifact artifact
) {
1403 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
1406 /** Relative path to the directories where the files will be stored */
1407 static String
artifactParentPath(M2Artifact artifact
) {
1408 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/' + artifact
.getVersion();
1412 private M2ConventionsUtils() {
1416 /** Combination of a category, a name and a version. */
1417 class CategoryNameVersion
extends NameVersion
{
1418 private String category
;
1420 CategoryNameVersion() {
1423 CategoryNameVersion(String category
, String name
, String version
) {
1424 super(name
, version
);
1425 this.category
= category
;
1428 CategoryNameVersion(String category
, NameVersion nameVersion
) {
1430 this.category
= category
;
1433 String
getCategory() {
1437 void setCategory(String category
) {
1438 this.category
= category
;
1442 public String
toString() {
1443 return category
+ ":" + super.toString();
1448 /** Combination of a name and a version. */
1449 class NameVersion
implements Comparable
<NameVersion
> {
1450 private String name
;
1451 private String version
;
1456 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1457 NameVersion(String nameVersion
) {
1458 int index
= nameVersion
.indexOf(";version=");
1460 setName(nameVersion
);
1463 setName(nameVersion
.substring(0, index
));
1464 setVersion(nameVersion
.substring(index
+ ";version=".length()));
1468 NameVersion(String name
, String version
) {
1470 this.version
= version
;
1473 NameVersion(NameVersion nameVersion
) {
1474 this.name
= nameVersion
.getName();
1475 this.version
= nameVersion
.getVersion();
1482 void setName(String name
) {
1486 String
getVersion() {
1490 void setVersion(String version
) {
1491 this.version
= version
;
1494 String
getBranch() {
1495 String
[] parts
= getVersion().split("\\.");
1496 if (parts
.length
< 2)
1497 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1498 return parts
[0] + "." + parts
[1];
1502 public boolean equals(Object obj
) {
1503 if (obj
instanceof NameVersion
) {
1504 NameVersion nameVersion
= (NameVersion
) obj
;
1505 return name
.equals(nameVersion
.getName()) && version
.equals(nameVersion
.getVersion());
1511 public int hashCode() {
1512 return name
.hashCode();
1516 public String
toString() {
1517 return name
+ ":" + version
;
1520 public int compareTo(NameVersion o
) {
1521 if (o
.getName().equals(name
))
1522 return version
.compareTo(o
.getVersion());
1524 return name
.compareTo(o
.getName());