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
.nio
.file
.StandardOpenOption
.APPEND
;
10 import static java
.nio
.file
.StandardOpenOption
.CREATE
;
11 import static java
.util
.jar
.Attributes
.Name
.MANIFEST_VERSION
;
12 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_DO_NOT_MODIFY
;
13 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_M2
;
14 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_M2_MERGE
;
15 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_M2_REPO
;
16 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_NO_METADATA_GENERATION
;
17 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_SOURCES_URI
;
18 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ARGEO_ORIGIN_URI
;
19 import static org
.argeo
.build
.Repackage
.ManifestHeader
.AUTOMATIC_MODULE_NAME
;
20 import static org
.argeo
.build
.Repackage
.ManifestHeader
.BUNDLE_LICENSE
;
21 import static org
.argeo
.build
.Repackage
.ManifestHeader
.BUNDLE_SYMBOLICNAME
;
22 import static org
.argeo
.build
.Repackage
.ManifestHeader
.BUNDLE_VERSION
;
23 import static org
.argeo
.build
.Repackage
.ManifestHeader
.ECLIPSE_SOURCE_BUNDLE
;
24 import static org
.argeo
.build
.Repackage
.ManifestHeader
.EXPORT_PACKAGE
;
25 import static org
.argeo
.build
.Repackage
.ManifestHeader
.IMPORT_PACKAGE
;
26 import static org
.argeo
.build
.Repackage
.ManifestHeader
.SPDX_LICENSE_IDENTIFIER
;
28 import java
.io
.BufferedWriter
;
30 import java
.io
.FileNotFoundException
;
31 import java
.io
.IOException
;
32 import java
.io
.InputStream
;
33 import java
.io
.OutputStream
;
34 import java
.lang
.System
.Logger
;
36 import java
.net
.URISyntaxException
;
37 import java
.nio
.charset
.StandardCharsets
;
38 import java
.nio
.file
.DirectoryStream
;
39 import java
.nio
.file
.FileSystem
;
40 import java
.nio
.file
.FileSystems
;
41 import java
.nio
.file
.FileVisitResult
;
42 import java
.nio
.file
.Files
;
43 import java
.nio
.file
.Path
;
44 import java
.nio
.file
.PathMatcher
;
45 import java
.nio
.file
.Paths
;
46 import java
.nio
.file
.SimpleFileVisitor
;
47 import java
.nio
.file
.StandardCopyOption
;
48 import java
.nio
.file
.StandardOpenOption
;
49 import java
.nio
.file
.attribute
.BasicFileAttributes
;
50 import java
.util
.ArrayList
;
51 import java
.util
.HashMap
;
52 import java
.util
.Iterator
;
53 import java
.util
.List
;
55 import java
.util
.Objects
;
56 import java
.util
.Properties
;
58 import java
.util
.StringJoiner
;
59 import java
.util
.TreeMap
;
60 import java
.util
.TreeSet
;
61 import java
.util
.concurrent
.CompletableFuture
;
62 import java
.util
.jar
.Attributes
;
63 import java
.util
.jar
.JarEntry
;
64 import java
.util
.jar
.JarInputStream
;
65 import java
.util
.jar
.JarOutputStream
;
66 import java
.util
.jar
.Manifest
;
67 import java
.util
.zip
.Deflater
;
69 import aQute
.bnd
.osgi
.Analyzer
;
70 import aQute
.bnd
.osgi
.Jar
;
72 /** Repackages existing jar files into OSGi bundles in an A2 repository. */
73 public class Repackage
{
74 final static Logger logger
= System
.getLogger(Repackage
.class.getName());
77 * Environment variable on whether sources should be packaged separately or
78 * integrated in the bundles.
80 final static String ENV_SOURCE_BUNDLES
= "SOURCE_BUNDLES";
81 /** Environment variable on whether operations should be parallelised. */
82 final static String ENV_ARGEO_BUILD_SEQUENTIAL
= "ARGEO_BUILD_SEQUENTIAL";
84 /** Whether repackaging should run in parallel (default) or sequentially. */
85 final static boolean sequential
= Boolean
.parseBoolean(System
.getenv(ENV_ARGEO_BUILD_SEQUENTIAL
));
87 /** Main entry point. */
88 public static void main(String
[] args
) {
90 logger
.log(INFO
, "Build will be sequential");
91 if (args
.length
< 2) {
92 System
.err
.println("Usage: <path to a2 output dir> <category1> <category2> ...");
95 Path a2Base
= Paths
.get(args
[0]).toAbsolutePath().normalize();
96 Path descriptorsBase
= Paths
.get(".").toAbsolutePath().normalize();
97 Repackage factory
= new Repackage(a2Base
, descriptorsBase
);
99 List
<CompletableFuture
<Void
>> toDos
= new ArrayList
<>();
100 for (int i
= 1; i
< args
.length
; i
++) {
101 Path categoryPath
= Paths
.get(args
[i
]);
102 factory
.cleanPreviousFailedBuild(categoryPath
);
103 if (sequential
) // sequential processing happens here
104 factory
.processCategory(categoryPath
);
106 toDos
.add(CompletableFuture
.runAsync(() -> factory
.processCategory(categoryPath
)));
108 if (!sequential
)// parallel processing
109 CompletableFuture
.allOf(toDos
.toArray(new CompletableFuture
[toDos
.size()])).join();
112 StringBuilder sb
= new StringBuilder();
113 for (String licenseId
: licensesUsed
.keySet())
114 for (String name
: licensesUsed
.get(licenseId
))
115 sb
.append((licenseId
.equals("") ?
"Proprietary" : licenseId
) + "\t\t" + name
+ "\n");
116 logger
.log(INFO
, "# License summary:\n" + sb
);
119 /** Deletes remaining sub directories. */
120 void cleanPreviousFailedBuild(Path categoryPath
) {
121 Path outputCategoryPath
= a2Base
.resolve(categoryPath
);
122 if (!Files
.exists(outputCategoryPath
))
124 // clean previous failed build
126 for (Path subDir
: Files
.newDirectoryStream(outputCategoryPath
, (d
) -> Files
.isDirectory(d
))) {
127 if (Files
.exists(subDir
)) {
128 logger
.log(WARNING
, "Bundle dir " + subDir
129 + " already exists, probably from a previous failed build, deleting it...");
130 deleteDirectory(subDir
);
133 } catch (IOException e
) {
134 logger
.log(ERROR
, "Cannot clean previous build", e
);
138 /** MANIFEST headers. */
139 enum ManifestHeader
{
141 /** OSGi bundle symbolic name. */
142 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
143 /** OSGi bundle version. */
144 BUNDLE_VERSION("Bundle-Version"), //
145 /** OSGi bundle license. */
146 BUNDLE_LICENSE("Bundle-License"), //
147 /** OSGi exported packages list. */
148 EXPORT_PACKAGE("Export-Package"), //
149 /** OSGi imported packages list. */
150 IMPORT_PACKAGE("Import-Package"), //
151 /** OSGi path to embedded jar. */
152 BUNDLE_CLASSPATH("Bundle-Classpath"), //
154 /** Java module name. */
155 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
157 /** Eclipse source bundle. */
158 ECLIPSE_SOURCE_BUNDLE("Eclipse-SourceBundle"), //
161 * SPDX license identifier.
163 * @see https://spdx.org/licenses/
165 SPDX_LICENSE_IDENTIFIER("SPDX-License-Identifier"), //
168 * Maven coordinates of the origin, possibly partial when using common.bnd or
171 ARGEO_ORIGIN_M2("Argeo-Origin-M2"), //
172 /** List of Maven coordinates to merge. */
173 ARGEO_ORIGIN_M2_MERGE("Argeo-Origin-M2-Merge"), //
174 /** Maven repository, if not the default one. */
175 ARGEO_ORIGIN_M2_REPO("Argeo-Origin-M2-Repo"), //
177 * Do not perform BND analysis of the origin component. Typically Import_package
178 * and Export-Package will be kept untouched.
180 ARGEO_ORIGIN_NO_METADATA_GENERATION("Argeo-Origin-NoMetadataGeneration"), //
182 // * Embed the original jar without modifying it (may be required by some
183 // * proprietary licenses, such as JCR Day License).
185 // ARGEO_ORIGIN_EMBED("Argeo-Origin-Embed"), //
187 * Do not modify original jar (may be required by some proprietary licenses,
188 * such as JCR Day License).
190 ARGEO_DO_NOT_MODIFY("Argeo-Origin-Do-Not-Modify"), //
192 * Origin (non-Maven) URI of the component. It may be anything (jar, archive,
195 ARGEO_ORIGIN_URI("Argeo-Origin-URI"), //
197 * Origin (non-Maven) URI of the source of the component. It may be anything
198 * (jar, archive, code repository, etc.).
200 ARGEO_ORIGIN_SOURCES_URI("Argeo-Origin-Sources-URI"), //
203 final String headerName
;
205 private ManifestHeader(String headerName
) {
206 this.headerName
= headerName
;
210 public String
toString() {
214 /** Get the value from either a {@link Manifest} or a {@link Properties}. */
215 String
get(Object map
) {
216 if (map
instanceof Manifest manifest
)
217 return manifest
.getMainAttributes().getValue(headerName
);
218 else if (map
instanceof Properties props
)
219 return props
.getProperty(headerName
);
221 throw new IllegalArgumentException("Unsupported mapping " + map
.getClass());
224 /** Put the value into either a {@link Manifest} or a {@link Properties}. */
225 void put(Object map
, String value
) {
226 if (map
instanceof Manifest manifest
)
227 manifest
.getMainAttributes().putValue(headerName
, value
);
228 else if (map
instanceof Properties props
)
229 props
.setProperty(headerName
, value
);
231 throw new IllegalArgumentException("Unsupported mapping " + map
.getClass());
235 /** Name of the file centralising information for multiple M2 artifacts. */
236 final static String COMMON_BND
= "common.bnd";
237 /** Name of the file centralising information for mergin M2 artifacts. */
238 final static String MERGE_BND
= "merge.bnd";
240 * Subdirectory of the jar file where origin informations (changes, legal
241 * notices etc. are stored)
243 final static String ARGEO_ORIGIN
= "ARGEO-ORIGIN";
244 /** File detailing modifications to the original component. */
245 final static String CHANGES
= ARGEO_ORIGIN
+ "/changes";
247 * Name of the file at the root of the repackaged jar, which prominently
248 * notifies that the component has be repackaged.
250 final static String README_REPACKAGED
= "README.repackaged";
253 /** Summary of all license seen during the repackaging. */
254 final static Map
<String
, Set
<String
>> licensesUsed
= new TreeMap
<>();
256 /** Directory where to download archives */
257 final Path originBase
;
258 /** Directory where to download Maven artifacts */
259 final Path mavenBase
;
261 /** A2 repository base for binary bundles */
263 /** A2 repository base for source bundles */
264 final Path a2SrcBase
;
265 /** A2 base for native components */
266 final Path a2LibBase
;
267 /** Location of the descriptors driving the packaging */
268 final Path descriptorsBase
;
269 /** URIs of archives to download */
270 final Properties uris
= new Properties();
271 /** Mirrors for archive download. Key is URI prefix, value list of base URLs */
272 final Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
274 /** Whether sources should be packaged separately */
275 final boolean separateSources
;
277 /** Constructor initialises the various variables */
278 public Repackage(Path a2Base
, Path descriptorsBase
) {
279 separateSources
= Boolean
.parseBoolean(System
.getenv(ENV_SOURCE_BUNDLES
));
281 logger
.log(INFO
, "Sources will be packaged separately");
283 Objects
.requireNonNull(a2Base
);
284 Objects
.requireNonNull(descriptorsBase
);
285 this.originBase
= Paths
.get(System
.getProperty("user.home"), ".cache", "argeo/build/origin");
286 this.mavenBase
= Paths
.get(System
.getProperty("user.home"), ".m2", "repository");
288 // TODO define and use a build base
289 this.a2Base
= a2Base
;
290 this.a2SrcBase
= separateSources ? a2Base
.getParent().resolve(a2Base
.getFileName() + ".src") : a2Base
;
291 this.a2LibBase
= a2Base
.resolve("lib");
292 this.descriptorsBase
= descriptorsBase
;
293 if (!Files
.exists(this.descriptorsBase
))
294 throw new IllegalArgumentException(this.descriptorsBase
+ " does not exist");
297 Path urisPath
= this.descriptorsBase
.resolve("uris.properties");
298 if (Files
.exists(urisPath
)) {
299 try (InputStream in
= Files
.newInputStream(urisPath
)) {
301 } catch (IOException e
) {
302 throw new IllegalStateException("Cannot load " + urisPath
, e
);
307 Path eclipseMirrorsPath
= this.descriptorsBase
.resolve("eclipse.mirrors.txt");
308 List
<String
> eclipseMirrors
= new ArrayList
<>();
309 if (Files
.exists(eclipseMirrorsPath
)) {
311 eclipseMirrors
= Files
.readAllLines(eclipseMirrorsPath
, StandardCharsets
.UTF_8
);
312 } catch (IOException e
) {
313 throw new IllegalStateException("Cannot load " + eclipseMirrorsPath
, e
);
315 for (Iterator
<String
> it
= eclipseMirrors
.iterator(); it
.hasNext();) {
316 String value
= it
.next();
317 if (value
.strip().equals(""))
321 mirrors
.put("http://www.eclipse.org/downloads", eclipseMirrors
);
327 /** Process a whole category/group id. */
328 void processCategory(Path categoryRelativePath
) {
330 Path targetCategoryBase
= descriptorsBase
.resolve(categoryRelativePath
);
331 DirectoryStream
<Path
> bnds
= Files
.newDirectoryStream(targetCategoryBase
,
332 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
333 && !p
.getFileName().toString().equals(MERGE_BND
));
334 for (Path p
: bnds
) {
335 processSingleM2ArtifactDistributionUnit(p
);
338 DirectoryStream
<Path
> dus
= Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
));
339 for (Path duDir
: dus
) {
340 if (duDir
.getFileName().toString().startsWith("eclipse-")) {
341 processEclipseArchive(duDir
);
343 processM2BasedDistributionUnit(duDir
);
346 } catch (IOException e
) {
347 throw new RuntimeException("Cannot process category " + categoryRelativePath
, e
);
351 /** Process a standalone Maven artifact. */
352 void processSingleM2ArtifactDistributionUnit(Path bndFile
) {
354 Path categoryRelativePath
= descriptorsBase
.relativize(bndFile
.getParent());
355 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
357 Properties fileProps
= new Properties();
358 try (InputStream in
= Files
.newInputStream(bndFile
)) {
361 // use file name as symbolic name
362 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
363 String symbolicName
= bndFile
.getFileName().toString();
364 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
365 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
368 String m2Coordinates
= fileProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
369 if (m2Coordinates
== null)
370 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile
);
371 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
373 Path downloaded
= downloadMaven(fileProps
, artifact
);
375 boolean doNotModify
= Boolean
376 .parseBoolean(fileProps
.getOrDefault(ARGEO_DO_NOT_MODIFY
.toString(), "false").toString());
378 processNotModified(targetCategoryBase
, downloaded
, fileProps
, artifact
);
382 // regular processing
383 A2Origin origin
= new A2Origin();
384 Path bundleDir
= processBndJar(downloaded
, targetCategoryBase
, fileProps
, artifact
, origin
);
385 downloadAndProcessM2Sources(fileProps
, artifact
, bundleDir
, false, false);
386 createJar(bundleDir
, origin
);
387 } catch (Exception e
) {
388 throw new RuntimeException("Cannot process " + bndFile
, e
);
393 * Process multiple Maven artifacts coming from a same project and therefore
394 * with information in common (typically the version), generating single bundles
395 * or merging them if necessary.
400 void processM2BasedDistributionUnit(Path duDir
) {
402 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
403 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
405 Path mergeBnd
= duDir
.resolve(MERGE_BND
);
406 if (Files
.exists(mergeBnd
)) // merge
407 mergeM2Artifacts(mergeBnd
);
409 Path commonBnd
= duDir
.resolve(COMMON_BND
);
410 if (!Files
.exists(commonBnd
))
413 Properties commonProps
= new Properties();
414 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
415 commonProps
.load(in
);
418 String m2Version
= commonProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
419 if (m2Version
== null) {
420 logger
.log(WARNING
, "Ignoring " + duDir
+ " as it is not an M2-based distribution unit");
421 return;// ignore, this is probably an Eclipse archive
423 if (!m2Version
.startsWith(":")) {
424 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
426 m2Version
= m2Version
.substring(1);
428 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(duDir
,
429 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
430 && !p
.getFileName().toString().equals(MERGE_BND
));
432 Properties fileProps
= new Properties();
433 try (InputStream in
= Files
.newInputStream(p
)) {
436 String m2Coordinates
= fileProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
437 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
438 if (artifact
.getVersion() == null) {
439 artifact
.setVersion(m2Version
);
441 logger
.log(DEBUG
, p
.getFileName() + " : Using version " + artifact
.getVersion()
442 + " specified in descriptor rather than " + m2Version
+ " specified in " + COMMON_BND
);
445 // prepare manifest entries
446 Properties mergedProps
= new Properties();
447 mergedProps
.putAll(commonProps
);
449 fileEntries
: for (Object key
: fileProps
.keySet()) {
450 if (ARGEO_ORIGIN_M2
.toString().equals(key
))
451 continue fileEntries
;
452 String value
= fileProps
.getProperty(key
.toString());
453 Object previousValue
= mergedProps
.put(key
.toString(), value
);
454 if (previousValue
!= null) {
456 commonBnd
+ ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
459 mergedProps
.put(ARGEO_ORIGIN_M2
.toString(), artifact
.toM2Coordinates());
460 if (!mergedProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
461 // use file name as symbolic name
462 String symbolicName
= p
.getFileName().toString();
463 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
464 mergedProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
468 Path downloaded
= downloadMaven(mergedProps
, artifact
);
470 boolean doNotModify
= Boolean
471 .parseBoolean(mergedProps
.getOrDefault(ARGEO_DO_NOT_MODIFY
.toString(), "false").toString());
473 processNotModified(targetCategoryBase
, downloaded
, mergedProps
, artifact
);
475 A2Origin origin
= new A2Origin();
476 Path targetBundleDir
= processBndJar(downloaded
, targetCategoryBase
, mergedProps
, artifact
, origin
);
477 downloadAndProcessM2Sources(mergedProps
, artifact
, targetBundleDir
, false, false);
478 createJar(targetBundleDir
, origin
);
481 } catch (IOException e
) {
482 throw new RuntimeException("Cannot process " + duDir
, e
);
486 /** Merge multiple Maven artifacts. */
487 void mergeM2Artifacts(Path mergeBnd
) throws IOException
{
488 Path duDir
= mergeBnd
.getParent();
489 String category
= duDir
.getParent().getFileName().toString();
490 Path targetCategoryBase
= a2Base
.resolve(category
);
492 Properties mergeProps
= new Properties();
493 // first, load common properties
494 Path commonBnd
= duDir
.resolve(COMMON_BND
);
495 if (Files
.exists(commonBnd
))
496 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
499 // then, the merge properties themselves
500 try (InputStream in
= Files
.newInputStream(mergeBnd
)) {
504 String m2Version
= mergeProps
.getProperty(ARGEO_ORIGIN_M2
.toString());
505 if (m2Version
== null) {
506 logger
.log(WARNING
, "Ignoring merging in " + duDir
+ " as it is not an M2-based distribution unit");
507 return;// ignore, this is probably an Eclipse archive
509 if (!m2Version
.startsWith(":")) {
510 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
512 m2Version
= m2Version
.substring(1);
513 mergeProps
.put(BUNDLE_VERSION
.toString(), m2Version
);
515 String artifactsStr
= mergeProps
.getProperty(ARGEO_ORIGIN_M2_MERGE
.toString());
516 if (artifactsStr
== null)
517 throw new IllegalArgumentException(mergeBnd
+ ": " + ARGEO_ORIGIN_M2_MERGE
+ " must be set");
519 String bundleSymbolicName
= mergeProps
.getProperty(BUNDLE_SYMBOLICNAME
.toString());
520 if (bundleSymbolicName
== null)
521 throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd
);
522 CategoryNameVersion nameVersion
= new M2Artifact(category
+ ":" + bundleSymbolicName
+ ":" + m2Version
);
524 A2Origin origin
= new A2Origin();
525 Path bundleDir
= targetCategoryBase
.resolve(bundleSymbolicName
+ "." + nameVersion
.getBranch());
527 StringJoiner originDesc
= new StringJoiner(",");
528 String
[] artifacts
= artifactsStr
.split(",");
529 artifacts
: for (String str
: artifacts
) {
530 String m2Coordinates
= str
.trim();
531 if ("".equals(m2Coordinates
))
533 M2Artifact artifact
= new M2Artifact(m2Coordinates
.trim());
534 if (artifact
.getVersion() == null)
535 artifact
.setVersion(m2Version
);
536 originDesc
.add(artifact
.toString());
537 Path downloaded
= downloadMaven(mergeProps
, artifact
);
539 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(downloaded
), false)) {
540 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
541 if (entry
.isDirectory())
543 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".DSA")
544 || entry
.getName().endsWith(".SF")) {
545 origin
.deleted
.add("cryptographic signatures from " + artifact
);
548 if (entry
.getName().endsWith("module-info.class")) { // skip Java 9 module info
549 origin
.deleted
.add("Java module information (module-info.class) from " + artifact
);
552 if (entry
.getName().startsWith("META-INF/versions/")) { // skip multi-version
553 origin
.deleted
.add("additional Java versions (META-INF/versions) from " + artifact
);
556 if (entry
.getName().startsWith("META-INF/maven/")) {
557 origin
.deleted
.add("Maven information (META-INF/maven) from " + artifact
);
560 if (entry
.getName().startsWith(".cache/")) { // Apache SSHD
561 origin
.deleted
.add("cache directory (.cache) from " + artifact
);
564 if (entry
.getName().equals("META-INF/DEPENDENCIES")) {
565 origin
.deleted
.add("Dependencies (META-INF/DEPENDENCIES) from " + artifact
);
568 if (entry
.getName().equals("META-INF/MANIFEST.MF")) {
569 Path originalManifest
= bundleDir
.resolve(ARGEO_ORIGIN
).resolve(artifact
.getGroupId())
570 .resolve(artifact
.getArtifactId()).resolve("MANIFEST.MF");
571 Files
.createDirectories(originalManifest
.getParent());
572 try (OutputStream out
= Files
.newOutputStream(originalManifest
)) {
573 Files
.copy(jarIn
, originalManifest
);
576 "original MANIFEST (" + bundleDir
.relativize(originalManifest
) + ") from " + artifact
);
580 if (entry
.getName().endsWith("NOTICE") || entry
.getName().endsWith("NOTICE.txt")
581 || entry
.getName().endsWith("NOTICE.md") || entry
.getName().endsWith("LICENSE")
582 || entry
.getName().endsWith("LICENSE.md") || entry
.getName().endsWith("LICENSE-notice.md")
583 || entry
.getName().endsWith("COPYING") || entry
.getName().endsWith("COPYING.LESSER")) {
584 Path artifactOriginDir
= bundleDir
.resolve(ARGEO_ORIGIN
).resolve(artifact
.getGroupId())
585 .resolve(artifact
.getArtifactId());
586 Path target
= artifactOriginDir
.resolve(entry
.getName());
587 Files
.createDirectories(target
.getParent());
588 Files
.copy(jarIn
, target
);
589 origin
.moved
.add(entry
.getName() + " in " + artifact
+ " to " + bundleDir
.relativize(target
));
592 Path target
= bundleDir
.resolve(entry
.getName());
593 Files
.createDirectories(target
.getParent());
594 if (!Files
.exists(target
)) {
595 Files
.copy(jarIn
, target
);
597 if (entry
.getName().startsWith("META-INF/services/")) {
598 try (OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
)) {
599 out
.write("\n".getBytes());
600 jarIn
.transferTo(out
);
601 logger
.log(DEBUG
, artifact
.getArtifactId() + " - Appended " + entry
.getName());
603 origin
.modified
.add(entry
.getName() + ", merging from " + artifact
);
604 } else if (entry
.getName().startsWith("org/apache/batik/")) {
605 logger
.log(TRACE
, "Skip " + entry
.getName());
607 } else if (entry
.getName().startsWith("META-INF/NOTICE")) {
608 logger
.log(WARNING
, "Skip " + entry
.getName() + " from " + artifact
);
612 throw new IllegalStateException("File " + target
+ " from " + artifact
+ " already exists");
615 logger
.log(TRACE
, () -> "Copied " + target
);
618 origin
.added
.add("binary content of " + artifact
);
621 downloadAndProcessM2Sources(mergeProps
, artifact
, bundleDir
, true, false);
624 // additional service files
625 Path servicesDir
= duDir
.resolve("services");
626 if (Files
.exists(servicesDir
)) {
627 for (Path p
: Files
.newDirectoryStream(servicesDir
)) {
628 Path target
= bundleDir
.resolve("META-INF/services/").resolve(p
.getFileName());
629 try (InputStream in
= Files
.newInputStream(p
);
630 OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
);) {
631 out
.write("\n".getBytes());
633 logger
.log(DEBUG
, "Appended " + p
);
635 origin
.added
.add(bundleDir
.relativize(target
).toString());
640 Map
<String
, String
> entries
= new TreeMap
<>();
641 try (Analyzer bndAnalyzer
= new Analyzer()) {
642 bndAnalyzer
.setProperties(mergeProps
);
643 Jar jar
= new Jar(bundleDir
.toFile());
644 bndAnalyzer
.setJar(jar
);
645 Manifest manifest
= bndAnalyzer
.calcManifest();
647 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
648 Object value
= manifest
.getMainAttributes().get(key
);
650 switch (key
.toString()) {
652 case "Bnd-LastModified":
656 if ("Require-Capability".equals(key
.toString())
657 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\"")) {
658 origin
.deleted
.add("MANIFEST header " + key
);
659 continue keys
;// hack for very old classes
661 entries
.put(key
.toString(), value
.toString());
663 } catch (Exception e
) {
664 throw new RuntimeException("Cannot process " + mergeBnd
, e
);
667 Manifest manifest
= new Manifest();
668 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
669 Files
.createDirectories(manifestPath
.getParent());
670 for (String key
: entries
.keySet()) {
671 String value
= entries
.get(key
);
672 manifest
.getMainAttributes().putValue(key
, value
);
674 manifest
.getMainAttributes().putValue(ARGEO_ORIGIN_M2
.toString(), originDesc
.toString());
676 processLicense(bundleDir
, manifest
);
679 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
682 createJar(bundleDir
, origin
);
685 /** Generates MANIFEST using BND. */
686 Path
processBndJar(Path downloaded
, Path targetCategoryBase
, Properties fileProps
, M2Artifact artifact
,
689 Map
<String
, String
> additionalEntries
= new TreeMap
<>();
690 boolean doNotModifyManifest
= Boolean
.parseBoolean(
691 fileProps
.getOrDefault(ARGEO_ORIGIN_NO_METADATA_GENERATION
.toString(), "false").toString());
693 // Note: we always force the symbolic name
694 if (doNotModifyManifest
) {
695 for (Object key
: fileProps
.keySet()) {
696 String value
= fileProps
.getProperty(key
.toString());
697 additionalEntries
.put(key
.toString(), value
);
700 if (artifact
!= null) {
701 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
702 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), artifact
.getName());
704 if (!fileProps
.containsKey(BUNDLE_VERSION
.toString())) {
705 fileProps
.put(BUNDLE_VERSION
.toString(), artifact
.getVersion());
709 if (!fileProps
.containsKey(EXPORT_PACKAGE
.toString())) {
710 fileProps
.put(EXPORT_PACKAGE
.toString(),
711 "*;version=\"" + fileProps
.getProperty(BUNDLE_VERSION
.toString()) + "\"");
715 try (Analyzer bndAnalyzer
= new Analyzer()) {
716 bndAnalyzer
.setProperties(fileProps
);
717 Jar jar
= new Jar(downloaded
.toFile());
718 bndAnalyzer
.setJar(jar
);
719 Manifest manifest
= bndAnalyzer
.calcManifest();
721 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
722 Object value
= manifest
.getMainAttributes().get(key
);
724 switch (key
.toString()) {
726 case "Bnd-LastModified":
730 if ("Require-Capability".equals(key
.toString())
731 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\"")) {
732 origin
.deleted
.add("MANIFEST header " + key
);
733 continue keys
;// !! hack for very old classes
735 additionalEntries
.put(key
.toString(), value
.toString());
739 Path targetBundleDir
= processBundleJar(downloaded
, targetCategoryBase
, additionalEntries
, origin
);
740 logger
.log(DEBUG
, () -> "Processed " + downloaded
);
741 return targetBundleDir
;
742 } catch (Exception e
) {
743 throw new RuntimeException("Cannot BND process " + downloaded
, e
);
748 /** Process an artifact that should not be modified. */
749 void processNotModified(Path targetCategoryBase
, Path downloaded
, Properties fileProps
, M2Artifact artifact
)
751 // Some proprietary or signed artifacts do not allow any modification
752 // When releasing (with separate sources), we just copy it
753 Path unmodifiedTarget
= targetCategoryBase
754 .resolve(fileProps
.getProperty(BUNDLE_SYMBOLICNAME
.toString()) + "." + artifact
.getBranch() + ".jar");
755 Files
.createDirectories(unmodifiedTarget
.getParent());
756 Files
.copy(downloaded
, unmodifiedTarget
, StandardCopyOption
.REPLACE_EXISTING
);
757 Path bundleDir
= targetCategoryBase
758 .resolve(fileProps
.getProperty(BUNDLE_SYMBOLICNAME
.toString()) + "." + artifact
.getBranch());
759 downloadAndProcessM2Sources(fileProps
, artifact
, bundleDir
, false, true);
761 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(unmodifiedTarget
))) {
762 manifest
= jarIn
.getManifest();
764 createSourceJar(bundleDir
, manifest
, fileProps
);
767 /** Download and integrates sources for a single Maven artifact. */
768 void downloadAndProcessM2Sources(Properties props
, M2Artifact artifact
, Path targetBundleDir
, boolean merging
,
769 boolean unmodified
) throws IOException
{
771 String repoStr
= props
.containsKey(ARGEO_ORIGIN_M2_REPO
.toString())
772 ? props
.getProperty(ARGEO_ORIGIN_M2_REPO
.toString())
774 String alternateUri
= props
.getProperty(ARGEO_ORIGIN_SOURCES_URI
.toString());
775 M2Artifact sourcesArtifact
= new M2Artifact(artifact
.toM2Coordinates(), "sources");
776 URI sourcesUrl
= alternateUri
!= null ?
new URI(alternateUri
)
777 : M2ConventionsUtils
.mavenRepoUrl(repoStr
, sourcesArtifact
);
778 Path sourcesDownloaded
= downloadMaven(sourcesUrl
, sourcesArtifact
);
779 processM2SourceJar(sourcesDownloaded
, targetBundleDir
, merging ? artifact
: null, unmodified
);
780 logger
.log(TRACE
, () -> "Processed source " + sourcesDownloaded
);
781 } catch (Exception e
) {
782 logger
.log(ERROR
, () -> "Cannot download source for " + artifact
);
787 /** Integrate sources from a downloaded jar file. */
788 void processM2SourceJar(Path file
, Path bundleDir
, M2Artifact mergingFrom
, boolean unmodified
) throws IOException
{
789 A2Origin origin
= new A2Origin();
790 Path sourceDir
= separateSources
|| unmodified ? bundleDir
.getParent().resolve(bundleDir
.toString() + ".src")
791 : bundleDir
.resolve("OSGI-OPT/src");
792 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
794 String mergingMsg
= "";
795 if (mergingFrom
!= null)
796 mergingMsg
= " of " + mergingFrom
;
798 Files
.createDirectories(sourceDir
);
800 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
801 String relPath
= entry
.getName();
802 if (entry
.isDirectory())
804 if (entry
.getName().equals("META-INF/MANIFEST.MF")) {// skip META-INF entries
805 origin
.deleted
.add("MANIFEST.MF from the sources" + mergingMsg
);
809 if (entry
.getName().startsWith("module-info.java")) {// skip Java module information
810 origin
.deleted
.add("Java module information from the sources (module-info.java)" + mergingMsg
);
813 if (entry
.getName().startsWith("/")) { // absolute paths
814 int metaInfIndex
= entry
.getName().indexOf("META-INF");
815 if (metaInfIndex
>= 0) {
816 relPath
= entry
.getName().substring(metaInfIndex
);
817 origin
.moved
.add(" to " + relPath
+ " entry with absolute path " + entry
.getName());
819 logger
.log(WARNING
, entry
.getName() + " has an absolute path");
820 origin
.deleted
.add(entry
.getName() + " from the sources" + mergingMsg
);
825 Path target
= sourceDir
.resolve(relPath
);
826 Files
.createDirectories(target
.getParent());
827 if (!Files
.exists(target
)) {
828 Files
.copy(jarIn
, target
);
829 logger
.log(TRACE
, () -> "Copied source " + target
);
831 logger
.log(TRACE
, () -> target
+ " already exists, skipping...");
836 if (separateSources
|| unmodified
) {
837 origin
.appendChanges(sourceDir
);
839 origin
.added
.add("source code under OSGI-OPT/src");
840 origin
.appendChanges(bundleDir
);
844 /** Download a Maven artifact. */
845 Path
downloadMaven(Properties props
, M2Artifact artifact
) throws IOException
{
846 String repoStr
= props
.containsKey(ARGEO_ORIGIN_M2_REPO
.toString())
847 ? props
.getProperty(ARGEO_ORIGIN_M2_REPO
.toString())
849 String alternateUri
= props
.getProperty(ARGEO_ORIGIN_URI
.toString());
851 URI uri
= alternateUri
!= null ?
new URI(alternateUri
) : M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
852 return downloadMaven(uri
, artifact
);
853 } catch (URISyntaxException e
) {
854 throw new IllegalArgumentException("Wrong aritfact URI", e
);
858 /** Download a Maven artifact. */
859 Path
downloadMaven(URI uri
, M2Artifact artifact
) throws IOException
{
860 return download(uri
, mavenBase
, M2ConventionsUtils
.artifactPath("", artifact
));
866 /** Process an archive in Eclipse format. */
867 void processEclipseArchive(Path duDir
) {
869 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
870 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
871 Files
.createDirectories(targetCategoryBase
);
872 // first delete all directories from previous builds
873 for (Path dir
: Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
)))
874 deleteDirectory(dir
);
876 Files
.createDirectories(originBase
);
878 Path commonBnd
= duDir
.resolve(COMMON_BND
);
879 Properties commonProps
= new Properties();
880 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
881 commonProps
.load(in
);
883 String url
= commonProps
.getProperty(ARGEO_ORIGIN_URI
.toString());
885 url
= uris
.getProperty(duDir
.getFileName().toString());
887 throw new IllegalStateException("No url available for " + duDir
);
888 commonProps
.put(ARGEO_ORIGIN_URI
.toString(), url
);
890 Path downloaded
= tryDownloadArchive(url
, originBase
);
892 FileSystem zipFs
= FileSystems
.newFileSystem(downloaded
, (ClassLoader
) null);
895 List
<PathMatcher
> includeMatchers
= new ArrayList
<>();
896 Properties includes
= new Properties();
897 try (InputStream in
= Files
.newInputStream(duDir
.resolve("includes.properties"))) {
900 for (Object pattern
: includes
.keySet()) {
901 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
902 includeMatchers
.add(pathMatcher
);
905 List
<PathMatcher
> excludeMatchers
= new ArrayList
<>();
906 Path excludeFile
= duDir
.resolve("excludes.properties");
907 if (Files
.exists(excludeFile
)) {
908 Properties excludes
= new Properties();
909 try (InputStream in
= Files
.newInputStream(excludeFile
)) {
912 for (Object pattern
: excludes
.keySet()) {
913 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
914 excludeMatchers
.add(pathMatcher
);
918 // keys are the bundle directories
919 Map
<Path
, A2Origin
> origins
= new HashMap
<>();
920 Files
.walkFileTree(zipFs
.getRootDirectories().iterator().next(), new SimpleFileVisitor
<Path
>() {
923 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
924 includeMatchers
: for (PathMatcher includeMatcher
: includeMatchers
) {
925 if (includeMatcher
.matches(file
)) {
926 for (PathMatcher excludeMatcher
: excludeMatchers
) {
927 if (excludeMatcher
.matches(file
)) {
928 logger
.log(TRACE
, "Skipping excluded " + file
);
929 return FileVisitResult
.CONTINUE
;
932 if (file
.getFileName().toString().contains(".source_")) {
933 processEclipseSourceJar(file
, targetCategoryBase
);
934 logger
.log(DEBUG
, () -> "Processed source " + file
);
936 Map
<String
, String
> map
= new HashMap
<>();
937 for (Object key
: commonProps
.keySet())
938 map
.put(key
.toString(), commonProps
.getProperty(key
.toString()));
939 A2Origin origin
= new A2Origin();
940 Path bundleDir
= processBundleJar(file
, targetCategoryBase
, map
, origin
);
941 if (bundleDir
== null) {
942 logger
.log(WARNING
, "No bundle dir created for " + file
+ ", skipping...");
943 return FileVisitResult
.CONTINUE
;
945 origins
.put(bundleDir
, origin
);
946 logger
.log(DEBUG
, () -> "Processed " + file
);
948 break includeMatchers
;
951 return FileVisitResult
.CONTINUE
;
955 DirectoryStream
<Path
> dirs
= Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
)
956 && p
.getFileName().toString().indexOf('.') >= 0 && !p
.getFileName().toString().endsWith(".src"));
957 for (Path bundleDir
: dirs
) {
958 A2Origin origin
= origins
.get(bundleDir
);
959 Objects
.requireNonNull(origin
, "No A2 origin found for " + bundleDir
);
960 createJar(bundleDir
, origin
);
962 } catch (IOException e
) {
963 throw new RuntimeException("Cannot process " + duDir
, e
);
968 /** Process sources in Eclipse format. */
969 void processEclipseSourceJar(Path file
, Path targetBase
) throws IOException
{
971 A2Origin origin
= new A2Origin();
973 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
974 Manifest manifest
= jarIn
.getManifest();
976 String
[] relatedBundle
= manifest
.getMainAttributes().getValue(ECLIPSE_SOURCE_BUNDLE
.toString())
978 String version
= relatedBundle
[1].substring("version=\"".length());
979 version
= version
.substring(0, version
.length() - 1);
980 NameVersion nameVersion
= new NameVersion(relatedBundle
[0], version
);
981 bundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
983 Path sourceDir
= separateSources ? bundleDir
.getParent().resolve(bundleDir
.toString() + ".src")
984 : bundleDir
.resolve("OSGI-OPT/src");
986 Files
.createDirectories(sourceDir
);
988 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
989 if (entry
.isDirectory())
991 if (entry
.getName().startsWith("META-INF"))// skip META-INF entries
993 Path target
= sourceDir
.resolve(entry
.getName());
994 Files
.createDirectories(target
.getParent());
995 Files
.copy(jarIn
, target
);
996 logger
.log(TRACE
, () -> "Copied source " + target
);
1000 if (separateSources
) {
1001 origin
.appendChanges(sourceDir
);
1003 origin
.added
.add("source code under OSGI-OPT/src");
1004 origin
.appendChanges(bundleDir
);
1007 } catch (IOException e
) {
1008 throw new IllegalStateException("Cannot process " + file
, e
);
1015 /** Normalise a single (that is, non-merged) bundle. */
1016 Path
processBundleJar(Path file
, Path targetBase
, Map
<String
, String
> entries
, A2Origin origin
) throws IOException
{
1017 // boolean embed = Boolean.parseBoolean(entries.getOrDefault(ARGEO_ORIGIN_EMBED.toString(), "false").toString());
1018 boolean doNotModify
= Boolean
1019 .parseBoolean(entries
.getOrDefault(ManifestHeader
.ARGEO_DO_NOT_MODIFY
.toString(), "false").toString());
1020 NameVersion nameVersion
;
1023 boolean isSingleton
= false;
1025 Manifest sourceManifest
;
1026 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
1027 sourceManifest
= jarIn
.getManifest();
1028 if (sourceManifest
== null)
1029 logger
.log(WARNING
, file
+ " has no manifest");
1030 manifest
= sourceManifest
!= null ?
new Manifest(sourceManifest
) : new Manifest();
1032 String rawSourceSymbolicName
= manifest
.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME
.toString());
1033 if (rawSourceSymbolicName
!= null) {
1034 // make sure there is no directive
1035 String
[] arr
= rawSourceSymbolicName
.split(";");
1036 for (int i
= 1; i
< arr
.length
; i
++) {
1037 if (arr
[i
].trim().equals("singleton:=true"))
1039 logger
.log(DEBUG
, file
.getFileName() + " is a singleton");
1042 // remove problematic entries in MANIFEST
1043 manifest
.getEntries().clear();
1045 String ourSymbolicName
= entries
.get(BUNDLE_SYMBOLICNAME
.toString());
1046 String ourVersion
= entries
.get(BUNDLE_VERSION
.toString());
1048 if (ourSymbolicName
!= null && ourVersion
!= null) {
1049 nameVersion
= new NameVersion(ourSymbolicName
, ourVersion
);
1051 nameVersion
= nameVersionFromManifest(manifest
);
1052 if (nameVersion
== null)
1053 throw new IllegalStateException("Could not compute name/version from Manifest");
1054 if (ourVersion
!= null && !nameVersion
.getVersion().equals(ourVersion
)) {
1056 "Original version is " + nameVersion
.getVersion() + " while new version is " + ourVersion
);
1057 entries
.put(BUNDLE_VERSION
.toString(), ourVersion
);
1059 if (ourSymbolicName
!= null) {
1060 // we always force our symbolic name
1061 nameVersion
.setName(ourSymbolicName
);
1065 bundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
1067 // copy original MANIFEST
1068 if (sourceManifest
!= null) {
1069 Path originalManifest
= bundleDir
.resolve(ARGEO_ORIGIN
).resolve("MANIFEST.MF");
1070 Files
.createDirectories(originalManifest
.getParent());
1071 try (OutputStream out
= Files
.newOutputStream(originalManifest
)) {
1072 sourceManifest
.write(out
);
1074 origin
.moved
.add("original MANIFEST to " + bundleDir
.relativize(originalManifest
));
1077 // force Java 9 module name
1078 entries
.put(ManifestHeader
.AUTOMATIC_MODULE_NAME
.toString(), nameVersion
.getName());
1080 boolean isNative
= false;
1083 if (bundleDir
.startsWith(a2LibBase
)) {
1085 Path libRelativePath
= a2LibBase
.relativize(bundleDir
);
1086 os
= libRelativePath
.getName(0).toString();
1087 arch
= libRelativePath
.getName(1).toString();
1092 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
1093 if (entry
.isDirectory())
1096 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".DSA")
1097 || entry
.getName().endsWith(".SF")) {
1098 origin
.deleted
.add("cryptographic signatures");
1101 if (entry
.getName().endsWith("module-info.class")) { // skip Java 9 module info
1102 origin
.deleted
.add("Java module information (module-info.class)");
1105 if (entry
.getName().startsWith("META-INF/versions/")) { // skip multi-version
1106 origin
.deleted
.add("additional Java versions (META-INF/versions)");
1109 if (entry
.getName().startsWith("META-INF/maven/")) {
1110 origin
.deleted
.add("Maven information (META-INF/maven)");
1113 // skip file system providers as they cause issues with native image
1114 if (entry
.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider")) {
1116 .add("file system providers (META-INF/services/java.nio.file.spi.FileSystemProvider)");
1120 if (entry
.getName().startsWith("OSGI-OPT/src/")) { // skip embedded sources
1121 origin
.deleted
.add("embedded sources");
1124 Path target
= bundleDir
.resolve(entry
.getName());
1125 Files
.createDirectories(target
.getParent());
1126 Files
.copy(jarIn
, target
);
1129 if (isNative
&& (entry
.getName().endsWith(".so") || entry
.getName().endsWith(".dll")
1130 || entry
.getName().endsWith(".jnilib"))) {
1131 Path categoryDir
= bundleDir
.getParent();
1132 boolean copyDll
= false;
1133 Path targetDll
= categoryDir
.resolve(bundleDir
.relativize(target
));
1134 if (nameVersion
.getName().equals("com.sun.jna")) {
1135 if (arch
.equals("x86_64"))
1137 if (os
.equals("macosx"))
1139 if (target
.getParent().getFileName().toString().equals(os
+ "-" + arch
)) {
1142 targetDll
= categoryDir
.resolve(target
.getFileName());
1147 Files
.createDirectories(targetDll
.getParent());
1148 if (Files
.exists(targetDll
))
1149 Files
.delete(targetDll
);
1150 Files
.copy(target
, targetDll
);
1152 Files
.delete(target
);
1153 origin
.deleted
.add(bundleDir
.relativize(target
).toString());
1155 logger
.log(TRACE
, () -> "Copied " + target
);
1160 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
1161 Files
.createDirectories(manifestPath
.getParent());
1163 if (isSingleton
&& entries
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
1164 entries
.put(BUNDLE_SYMBOLICNAME
.toString(),
1165 entries
.get(BUNDLE_SYMBOLICNAME
.toString()) + ";singleton:=true");
1168 // Final MANIFEST decisions
1169 // We also check the original OSGi metadata and compare with our changes
1170 for (String key
: entries
.keySet()) {
1171 String value
= entries
.get(key
);
1172 String previousValue
= manifest
.getMainAttributes().getValue(key
);
1173 boolean wasDifferent
= previousValue
!= null && !previousValue
.equals(value
);
1174 boolean keepPrevious
= false;
1176 if (SPDX_LICENSE_IDENTIFIER
.toString().equals(key
) && previousValue
!= null)
1177 keepPrevious
= true;
1178 else if (BUNDLE_VERSION
.toString().equals(key
) && wasDifferent
)
1179 if (previousValue
.equals(value
+ ".0")) // typically a Maven first release
1180 keepPrevious
= true;
1183 if (logger
.isLoggable(DEBUG
))
1184 logger
.log(DEBUG
, file
.getFileName() + ": " + key
+ " was NOT modified, value kept is "
1185 + previousValue
+ ", not overriden with " + value
);
1186 value
= previousValue
;
1190 manifest
.getMainAttributes().putValue(key
, value
);
1191 if (wasDifferent
&& !keepPrevious
) {
1192 if (IMPORT_PACKAGE
.toString().equals(key
) || EXPORT_PACKAGE
.toString().equals(key
))
1193 logger
.log(TRACE
, () -> file
.getFileName() + ": " + key
+ " was modified");
1194 else if (BUNDLE_SYMBOLICNAME
.toString().equals(key
) || AUTOMATIC_MODULE_NAME
.toString().equals(key
))
1196 file
.getFileName() + ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
1199 file
.getFileName() + ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
1200 origin
.modified
.add("MANIFEST header " + key
);
1203 // !! hack to remove unresolvable
1204 if (key
.equals("Provide-Capability") || key
.equals("Require-Capability"))
1205 if (nameVersion
.getName().equals("osgi.core") || nameVersion
.getName().equals("osgi.cmpn")) {
1206 manifest
.getMainAttributes().remove(key
);
1207 origin
.deleted
.add("MANIFEST header " + key
);
1211 // de-pollute MANIFEST
1212 for (Iterator
<Map
.Entry
<Object
, Object
>> manifestEntries
= manifest
.getMainAttributes().entrySet()
1213 .iterator(); manifestEntries
.hasNext();) {
1214 Map
.Entry
<Object
, Object
> manifestEntry
= manifestEntries
.next();
1215 switch (manifestEntry
.getKey().toString()) {
1216 case "Archiver-Version":
1219 case "Originally-Created-By":
1221 case "Bnd-LastModified":
1222 manifestEntries
.remove();
1223 origin
.deleted
.add("MANIFEST header " + manifestEntry
.getKey());
1226 if (sourceManifest
!= null && !sourceManifest
.getMainAttributes().containsKey(manifestEntry
.getKey()))
1227 origin
.added
.add("MANIFEST header " + manifestEntry
.getKey());
1231 processLicense(bundleDir
, manifest
);
1233 origin
.modified
.add("MANIFEST (META-INF/MANIFEST.MF)");
1234 // write the MANIFEST
1235 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
1236 manifest
.write(out
);
1241 /** Process SPDX license identifier. */
1242 void processLicense(Path bundleDir
, Manifest manifest
) {
1243 String spdxLicenceId
= manifest
.getMainAttributes().getValue(SPDX_LICENSE_IDENTIFIER
.toString());
1244 String bundleLicense
= manifest
.getMainAttributes().getValue(BUNDLE_LICENSE
.toString());
1245 if (spdxLicenceId
== null) {
1246 logger
.log(ERROR
, bundleDir
.getFileName() + ": " + SPDX_LICENSE_IDENTIFIER
+ " not available, "
1247 + BUNDLE_LICENSE
+ " is " + bundleLicense
);
1249 // only use the first licensing option
1250 int orIndex
= spdxLicenceId
.indexOf(" OR ");
1252 spdxLicenceId
= spdxLicenceId
.substring(0, orIndex
).trim();
1254 String bundleDirName
= bundleDir
.getFileName().toString();
1255 // force licenses of some well-known components
1256 // even if we say otherwise (typically because from an Eclipse archive)
1257 if (bundleDirName
.startsWith("org.apache."))
1258 spdxLicenceId
= "Apache-2.0";
1259 if (bundleDirName
.startsWith("com.sun.jna."))
1260 spdxLicenceId
= "Apache-2.0";
1261 if (bundleDirName
.startsWith("com.ibm.icu."))
1262 spdxLicenceId
= "ICU";
1263 if (bundleDirName
.startsWith("javax.annotation."))
1264 spdxLicenceId
= "GPL-2.0-only WITH Classpath-exception-2.0";
1265 if (bundleDirName
.startsWith("javax.inject."))
1266 spdxLicenceId
= "Apache-2.0";
1267 if (bundleDirName
.startsWith("org.osgi."))
1268 spdxLicenceId
= "Apache-2.0";
1270 manifest
.getMainAttributes().putValue(SPDX_LICENSE_IDENTIFIER
.toString(), spdxLicenceId
);
1271 if (!licensesUsed
.containsKey(spdxLicenceId
))
1272 licensesUsed
.put(spdxLicenceId
, new TreeSet
<>());
1273 licensesUsed
.get(spdxLicenceId
).add(bundleDir
.getParent().getFileName() + "/" + bundleDir
.getFileName());
1280 /** Recursively deletes a directory. */
1281 static void deleteDirectory(Path path
) throws IOException
{
1282 if (!Files
.exists(path
))
1284 Files
.walkFileTree(path
, new SimpleFileVisitor
<Path
>() {
1286 public FileVisitResult
postVisitDirectory(Path directory
, IOException e
) throws IOException
{
1289 Files
.delete(directory
);
1294 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1301 /** Extract name/version from a MANIFEST. */
1302 NameVersion
nameVersionFromManifest(Manifest manifest
) {
1303 Attributes attrs
= manifest
.getMainAttributes();
1305 String symbolicName
= attrs
.getValue(ManifestHeader
.BUNDLE_SYMBOLICNAME
.toString());
1306 if (symbolicName
== null)
1308 // make sure there is no directive
1309 symbolicName
= symbolicName
.split(";")[0];
1311 String version
= attrs
.getValue(ManifestHeader
.BUNDLE_VERSION
.toString());
1312 return new NameVersion(symbolicName
, version
);
1315 /** Try to download from an URI. */
1316 Path
tryDownloadArchive(String uriStr
, Path dir
) throws IOException
{
1318 List
<String
> urlBases
= null;
1319 String uriPrefix
= null;
1320 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
1321 if (uriStr
.startsWith(uriPref
)) {
1322 if (mirrors
.get(uriPref
).size() > 0) {
1323 urlBases
= mirrors
.get(uriPref
);
1324 uriPrefix
= uriPref
;
1329 if (urlBases
== null)
1331 return downloadArchive(new URI(uriStr
), dir
);
1332 } catch (FileNotFoundException
| URISyntaxException e
) {
1333 throw new FileNotFoundException("Cannot find " + uriStr
);
1337 for (String urlBase
: urlBases
) {
1338 String relativePath
= uriStr
.substring(uriPrefix
.length());
1339 String uStr
= urlBase
+ relativePath
;
1341 return downloadArchive(new URI(uStr
), dir
);
1342 } catch (FileNotFoundException
| URISyntaxException e
) {
1343 logger
.log(WARNING
, "Cannot download " + uStr
+ ", trying another mirror");
1346 throw new FileNotFoundException("Cannot find " + uriStr
);
1350 * Effectively download an archive.
1352 Path
downloadArchive(URI uri
, Path dir
) throws IOException
{
1353 return download(uri
, dir
, (String
) null);
1357 * Effectively download. Synchronised in order to avoid downloading twice in
1360 synchronized Path
download(URI uri
, Path dir
, String name
) throws IOException
{
1364 // We use also use parent directory in case the archive itself has a fixed name
1365 String
[] segments
= uri
.getPath().split("/");
1366 name
= segments
.length
> 1 ? segments
[segments
.length
- 2] + '-' + segments
[segments
.length
- 1]
1367 : segments
[segments
.length
- 1];
1370 dest
= dir
.resolve(name
);
1371 if (Files
.exists(dest
)) {
1372 logger
.log(TRACE
, () -> "File " + dest
+ " already exists for " + uri
+ ", not downloading again");
1375 Files
.createDirectories(dest
.getParent());
1378 try (InputStream in
= uri
.toURL().openStream()) {
1379 Files
.copy(in
, dest
);
1380 logger
.log(DEBUG
, () -> "Downloaded " + dest
+ " from " + uri
);
1385 /** Create a JAR file from a directory. */
1386 Path
createJar(Path bundleDir
, A2Origin origin
) throws IOException
{
1387 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
1389 try (InputStream in
= Files
.newInputStream(manifestPath
)) {
1390 manifest
= new Manifest(in
);
1392 // legal requirements
1393 origin
.appendChanges(bundleDir
);
1394 createReadMe(bundleDir
, manifest
);
1397 Path jarPath
= bundleDir
.getParent().resolve(bundleDir
.getFileName() + ".jar");
1398 try (JarOutputStream jarOut
= new JarOutputStream(Files
.newOutputStream(jarPath
), manifest
)) {
1399 jarOut
.setLevel(Deflater
.DEFAULT_COMPRESSION
);
1400 Files
.walkFileTree(bundleDir
, new SimpleFileVisitor
<Path
>() {
1403 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1404 if (file
.getFileName().toString().equals("MANIFEST.MF"))
1405 return super.visitFile(file
, attrs
);
1406 JarEntry entry
= new JarEntry(
1407 bundleDir
.relativize(file
).toString().replace(File
.separatorChar
, '/'));
1408 jarOut
.putNextEntry(entry
);
1409 Files
.copy(file
, jarOut
);
1410 return super.visitFile(file
, attrs
);
1415 deleteDirectory(bundleDir
);
1417 if (separateSources
)
1418 createSourceJar(bundleDir
, manifest
, null);
1423 /** Package sources separately, in the Eclipse-SourceBundle format. */
1424 void createSourceJar(Path bundleDir
, Manifest manifest
, Properties props
) throws IOException
{
1425 boolean unmodified
= props
!= null;
1426 Path bundleCategoryDir
= bundleDir
.getParent();
1427 Path sourceDir
= bundleCategoryDir
.resolve(bundleDir
.toString() + ".src");
1428 if (!Files
.exists(sourceDir
)) {
1429 logger
.log(WARNING
, sourceDir
+ " does not exist, skipping...");
1433 Path relPath
= a2Base
.relativize(bundleCategoryDir
);
1434 Path srcCategoryDir
= a2SrcBase
.resolve(relPath
);
1435 Path srcJarP
= srcCategoryDir
.resolve(sourceDir
.getFileName() + ".jar");
1436 Files
.createDirectories(srcJarP
.getParent());
1438 String bundleSymbolicName
= manifest
.getMainAttributes().getValue("Bundle-SymbolicName").toString();
1439 // in case there are additional directives
1440 bundleSymbolicName
= bundleSymbolicName
.split(";")[0];
1441 Manifest srcManifest
= new Manifest();
1442 srcManifest
.getMainAttributes().put(MANIFEST_VERSION
, "1.0");
1443 BUNDLE_SYMBOLICNAME
.put(srcManifest
, bundleSymbolicName
+ ".src");
1444 BUNDLE_VERSION
.put(srcManifest
, BUNDLE_VERSION
.get(manifest
));
1445 ECLIPSE_SOURCE_BUNDLE
.put(srcManifest
,
1446 bundleSymbolicName
+ ";version=\"" + BUNDLE_VERSION
.get(manifest
) + "\"");
1449 createReadMe(sourceDir
, unmodified ? props
: manifest
);
1451 try (JarOutputStream srcJarOut
= new JarOutputStream(Files
.newOutputStream(srcJarP
), srcManifest
)) {
1452 // srcJarOut.setLevel(Deflater.BEST_COMPRESSION);
1453 Files
.walkFileTree(sourceDir
, new SimpleFileVisitor
<Path
>() {
1456 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
1457 if (file
.getFileName().toString().equals("MANIFEST.MF"))
1458 return super.visitFile(file
, attrs
);
1459 JarEntry entry
= new JarEntry(
1460 sourceDir
.relativize(file
).toString().replace(File
.separatorChar
, '/'));
1461 srcJarOut
.putNextEntry(entry
);
1462 Files
.copy(file
, srcJarOut
);
1463 return super.visitFile(file
, attrs
);
1468 deleteDirectory(sourceDir
);
1472 * Generate a readme clarifying and prominently notifying of the repackaging and
1475 void createReadMe(Path jarDir
, Object mapping
) throws IOException
{
1476 // write repackaged README
1477 try (BufferedWriter writer
= Files
.newBufferedWriter(jarDir
.resolve(README_REPACKAGED
))) {
1478 boolean merged
= ARGEO_ORIGIN_M2_MERGE
.get(mapping
) != null;
1480 writer
.append("This component is a merging of third party components"
1481 + " in order to comply with A2 packaging standards.\n");
1483 writer
.append("This component is a repackaging of a third party component"
1484 + " in order to comply with A2 packaging standards.\n");
1487 String spdxLicenseId
= SPDX_LICENSE_IDENTIFIER
.get(mapping
);
1488 if (spdxLicenseId
== null)
1489 throw new IllegalStateException("An SPDX license id must have beend defined at this stage.");
1490 writer
.append("\nIt is redistributed under the following license:\n\n");
1491 writer
.append("SPDX-Identifier: " + spdxLicenseId
+ "\n\n");
1493 if (!spdxLicenseId
.startsWith("LicenseRef")) {// standard
1494 int withIndex
= spdxLicenseId
.indexOf(" WITH ");
1495 if (withIndex
>= 0) {
1496 String simpleId
= spdxLicenseId
.substring(0, withIndex
).trim();
1497 String exception
= spdxLicenseId
.substring(withIndex
+ " WITH ".length());
1498 writer
.append("which are available here: https://spdx.org/licenses/" + simpleId
1499 + "\nand here: https://spdx.org/licenses/" + exception
+ "\n");
1501 writer
.append("which is available here: https://spdx.org/licenses/" + spdxLicenseId
+ "\n");
1504 String url
= BUNDLE_LICENSE
.get(mapping
);
1506 writer
.write("which is available here: " + url
+ "\n");
1508 logger
.log(ERROR
, "No licence URL for " + jarDir
);
1513 String originDesc
= ARGEO_ORIGIN_URI
.get(mapping
);
1514 if (originDesc
!= null)
1515 writer
.append("\nThe original component comes from " + originDesc
+ ".\n");
1517 String m2Repo
= ARGEO_ORIGIN_M2_REPO
.get(mapping
);
1518 originDesc
= ARGEO_ORIGIN_M2
.get(mapping
);
1519 if (originDesc
!= null)
1520 writer
.append("\nThe original component has M2 coordinates:\n" + originDesc
.replace(',', '\n')
1521 + "\n" + (m2Repo
!= null ?
"\nin M2 repository " + m2Repo
+ "\n" : ""));
1523 logger
.log(ERROR
, "Cannot find origin information in " + jarDir
);
1525 String originSources
= ARGEO_ORIGIN_SOURCES_URI
.get(mapping
);
1526 if (originSources
!= null)
1527 writer
.append("\nThe original sources come from " + originSources
+ ".\n");
1529 if (Files
.exists(jarDir
.resolve(CHANGES
)))
1530 writer
.append("\nA detailed list of changes is available under " + CHANGES
+ ".\n");
1532 if (!jarDir
.getFileName().toString().endsWith(".src")) {// binary archive
1533 if (separateSources
)
1534 writer
.append("Corresponding sources are available in the related archive named "
1535 + jarDir
.toString() + ".src.jar.\n");
1537 writer
.append("Corresponding sources are available under OSGI-OPT/src.\n");
1543 * Gathers modifications performed on the original binaries and sources,
1544 * especially in order to comply with their license requirements.
1551 Set
<String
> modified
= new TreeSet
<>();
1552 Set
<String
> deleted
= new TreeSet
<>();
1553 Set
<String
> added
= new TreeSet
<>();
1554 Set
<String
> moved
= new TreeSet
<>();
1556 /** Append changes to the A2-ORIGIN/changes file. */
1557 void appendChanges(Path baseDirectory
) throws IOException
{
1558 if (modified
.isEmpty() && deleted
.isEmpty() && added
.isEmpty() && moved
.isEmpty())
1559 return; // no changes
1560 Path changesFile
= baseDirectory
.resolve(CHANGES
);
1561 Files
.createDirectories(changesFile
.getParent());
1562 try (BufferedWriter writer
= Files
.newBufferedWriter(changesFile
, APPEND
, CREATE
)) {
1563 for (String msg
: added
)
1564 writer
.write("- Added " + msg
+ ".\n");
1565 for (String msg
: modified
)
1566 writer
.write("- Modified " + msg
+ ".\n");
1567 for (String msg
: moved
)
1568 writer
.write("- Moved " + msg
+ ".\n");
1569 for (String msg
: deleted
)
1570 writer
.write("- Deleted " + msg
+ ".\n");
1576 /** Simple representation of an M2 artifact. */
1577 class M2Artifact
extends CategoryNameVersion
{
1578 private String classifier
;
1580 M2Artifact(String m2coordinates
) {
1581 this(m2coordinates
, null);
1584 M2Artifact(String m2coordinates
, String classifier
) {
1585 String
[] parts
= m2coordinates
.split(":");
1586 setCategory(parts
[0]);
1588 if (parts
.length
> 2) {
1589 setVersion(parts
[2]);
1591 this.classifier
= classifier
;
1594 String
getGroupId() {
1595 return super.getCategory();
1598 String
getArtifactId() {
1599 return super.getName();
1602 String
toM2Coordinates() {
1603 return getCategory() + ":" + getName() + (getVersion() != null ?
":" + getVersion() : "");
1606 String
getClassifier() {
1607 return classifier
!= null ? classifier
: "";
1610 String
getExtension() {
1615 /** Utilities around Maven (conventions based). */
1616 class M2ConventionsUtils
{
1617 final static String MAVEN_CENTRAL_BASE_URL
= "https://repo1.maven.org/maven2/";
1619 /** The file name of this artifact when stored */
1620 static String
artifactFileName(M2Artifact artifact
) {
1621 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
1622 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
1623 + artifact
.getExtension();
1626 /** Absolute path to the file */
1627 static String
artifactPath(String artifactBasePath
, M2Artifact artifact
) {
1628 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
1631 /** Absolute path to the file */
1632 static String
artifactUrl(String repoUrl
, M2Artifact artifact
) {
1633 if (repoUrl
.endsWith("/"))
1634 return repoUrl
+ artifactPath("/", artifact
).substring(1);
1636 return repoUrl
+ artifactPath("/", artifact
);
1639 /** Absolute path to the file */
1640 static URI
mavenRepoUrl(String repoBase
, M2Artifact artifact
) throws URISyntaxException
{
1641 String uri
= artifactUrl(repoBase
== null ? MAVEN_CENTRAL_BASE_URL
: repoBase
, artifact
);
1642 return new URI(uri
);
1645 /** Absolute path to the directories where the files will be stored */
1646 static String
artifactParentPath(String artifactBasePath
, M2Artifact artifact
) {
1647 return artifactBasePath
+ (artifactBasePath
.endsWith("/") || artifactBasePath
.equals("") ?
"" : "/")
1648 + artifactParentPath(artifact
);
1651 /** Relative path to the directories where the files will be stored */
1652 static String
artifactParentPath(M2Artifact artifact
) {
1653 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/' + artifact
.getVersion();
1657 private M2ConventionsUtils() {
1661 /** Combination of a category, a name and a version. */
1662 class CategoryNameVersion
extends NameVersion
{
1663 private String category
;
1665 CategoryNameVersion() {
1668 CategoryNameVersion(String category
, String name
, String version
) {
1669 super(name
, version
);
1670 this.category
= category
;
1673 CategoryNameVersion(String category
, NameVersion nameVersion
) {
1675 this.category
= category
;
1678 String
getCategory() {
1682 void setCategory(String category
) {
1683 this.category
= category
;
1687 public String
toString() {
1688 return category
+ ":" + super.toString();
1693 /** Combination of a name and a version. */
1694 class NameVersion
implements Comparable
<NameVersion
> {
1695 private String name
;
1696 private String version
;
1701 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1702 NameVersion(String nameVersion
) {
1703 int index
= nameVersion
.indexOf(";version=");
1705 setName(nameVersion
);
1708 setName(nameVersion
.substring(0, index
));
1709 setVersion(nameVersion
.substring(index
+ ";version=".length()));
1713 NameVersion(String name
, String version
) {
1715 this.version
= version
;
1718 NameVersion(NameVersion nameVersion
) {
1719 this.name
= nameVersion
.getName();
1720 this.version
= nameVersion
.getVersion();
1727 void setName(String name
) {
1731 String
getVersion() {
1735 void setVersion(String version
) {
1736 this.version
= version
;
1739 String
getBranch() {
1740 String
[] parts
= getVersion().split("\\.");
1741 if (parts
.length
< 2)
1742 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1743 return parts
[0] + "." + parts
[1];
1747 public boolean equals(Object obj
) {
1748 if (obj
instanceof NameVersion
) {
1749 NameVersion nameVersion
= (NameVersion
) obj
;
1750 return name
.equals(nameVersion
.getName()) && version
.equals(nameVersion
.getVersion());
1756 public int hashCode() {
1757 return name
.hashCode();
1761 public String
toString() {
1762 return name
+ ":" + version
;
1765 public int compareTo(NameVersion o
) {
1766 if (o
.getName().equals(name
))
1767 return version
.compareTo(o
.getVersion());
1769 return name
.compareTo(o
.getName());