1 package org
.argeo
.build
;
3 import static java
.lang
.System
.Logger
.Level
.DEBUG
;
4 import static org
.argeo
.build
.Repackage
.ManifestConstants
.BUNDLE_SYMBOLICNAME
;
5 import static org
.argeo
.build
.Repackage
.ManifestConstants
.BUNDLE_VERSION
;
6 import static org
.argeo
.build
.Repackage
.ManifestConstants
.EXPORT_PACKAGE
;
7 import static org
.argeo
.build
.Repackage
.ManifestConstants
.SLC_ORIGIN_M2
;
8 import static org
.argeo
.build
.Repackage
.ManifestConstants
.SLC_ORIGIN_M2_REPO
;
10 import java
.io
.FileNotFoundException
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
13 import java
.io
.OutputStream
;
14 import java
.lang
.System
.Logger
;
15 import java
.lang
.System
.Logger
.Level
;
16 import java
.net
.MalformedURLException
;
18 import java
.nio
.file
.DirectoryStream
;
19 import java
.nio
.file
.FileSystem
;
20 import java
.nio
.file
.FileSystems
;
21 import java
.nio
.file
.FileVisitResult
;
22 import java
.nio
.file
.Files
;
23 import java
.nio
.file
.Path
;
24 import java
.nio
.file
.PathMatcher
;
25 import java
.nio
.file
.Paths
;
26 import java
.nio
.file
.SimpleFileVisitor
;
27 import java
.nio
.file
.StandardOpenOption
;
28 import java
.nio
.file
.attribute
.BasicFileAttributes
;
29 import java
.util
.ArrayList
;
30 import java
.util
.HashMap
;
31 import java
.util
.List
;
33 import java
.util
.Objects
;
34 import java
.util
.Properties
;
35 import java
.util
.TreeMap
;
36 import java
.util
.jar
.Attributes
;
37 import java
.util
.jar
.JarEntry
;
38 import java
.util
.jar
.JarInputStream
;
39 import java
.util
.jar
.JarOutputStream
;
40 import java
.util
.jar
.Manifest
;
41 import java
.util
.zip
.Deflater
;
43 import aQute
.bnd
.osgi
.Analyzer
;
44 import aQute
.bnd
.osgi
.Jar
;
46 /** The central class for A2 packaging. */
47 public class Repackage
{
48 private final static Logger logger
= System
.getLogger(Repackage
.class.getName());
50 /** Main entry point. */
51 public static void main(String
[] args
) {
52 if (args
.length
< 2) {
53 System
.err
.println("Usage: <path to a2 output dir> <category1> <category2> ...");
56 Path a2Base
= Paths
.get(args
[0]).toAbsolutePath().normalize();
57 Path descriptorsBase
= Paths
.get(".").toAbsolutePath().normalize();
58 Repackage factory
= new Repackage(a2Base
, descriptorsBase
, true);
60 for (int i
= 1; i
< args
.length
; i
++) {
61 Path p
= Paths
.get(args
[i
]);
62 factory
.processCategory(p
);
66 private final static String COMMON_BND
= "common.bnd";
67 private final static String MERGE_BND
= "merge.bnd";
69 private Path originBase
;
71 private Path a2LibBase
;
72 private Path descriptorsBase
;
74 private Properties uris
= new Properties();
76 private boolean includeSources
= true;
78 /** key is URI prefix, value list of base URLs */
79 private Map
<String
, List
<String
>> mirrors
= new HashMap
<String
, List
<String
>>();
81 public Repackage(Path a2Base
, Path descriptorsBase
, boolean includeSources
) {
82 Objects
.requireNonNull(a2Base
);
83 Objects
.requireNonNull(descriptorsBase
);
84 this.originBase
= Paths
.get(System
.getProperty("user.home"), ".cache", "argeo/build/origin");
85 // TODO define and use a build base
87 this.a2LibBase
= a2Base
.resolve("lib");
88 this.descriptorsBase
= descriptorsBase
;
89 if (!Files
.exists(this.descriptorsBase
))
90 throw new IllegalArgumentException(this.descriptorsBase
+ " does not exist");
91 this.includeSources
= includeSources
;
93 Path urisPath
= this.descriptorsBase
.resolve("uris.properties");
94 if (Files
.exists(urisPath
)) {
95 try (InputStream in
= Files
.newInputStream(urisPath
)) {
97 } catch (IOException e
) {
98 throw new IllegalStateException("Cannot load " + urisPath
, e
);
102 // TODO make it configurable
103 List
<String
> eclipseMirrors
= new ArrayList
<>();
104 eclipseMirrors
.add("https://archive.eclipse.org/");
105 eclipseMirrors
.add("http://ftp-stud.hs-esslingen.de/Mirrors/eclipse/");
106 eclipseMirrors
.add("http://ftp.fau.de/eclipse/");
108 mirrors
.put("http://www.eclipse.org/downloads", eclipseMirrors
);
115 /** Process a whole category/group id. */
116 public void processCategory(Path categoryRelativePath
) {
118 Path targetCategoryBase
= descriptorsBase
.resolve(categoryRelativePath
);
119 DirectoryStream
<Path
> bnds
= Files
.newDirectoryStream(targetCategoryBase
,
120 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
121 && !p
.getFileName().toString().equals(MERGE_BND
));
122 for (Path p
: bnds
) {
123 processSingleM2ArtifactDistributionUnit(p
);
126 DirectoryStream
<Path
> dus
= Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
));
127 for (Path duDir
: dus
) {
128 if (duDir
.getFileName().toString().startsWith("eclipse-")) {
129 processEclipseArchive(duDir
);
131 processM2BasedDistributionUnit(duDir
);
134 } catch (IOException e
) {
135 throw new RuntimeException("Cannot process category " + categoryRelativePath
, e
);
139 /** Process a standalone Maven artifact. */
140 public void processSingleM2ArtifactDistributionUnit(Path bndFile
) {
142 // String category = bndFile.getParent().getFileName().toString();
143 Path categoryRelativePath
= descriptorsBase
.relativize(bndFile
.getParent());
144 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
146 Properties fileProps
= new Properties();
147 try (InputStream in
= Files
.newInputStream(bndFile
)) {
150 String repoStr
= fileProps
.containsKey(SLC_ORIGIN_M2_REPO
.toString())
151 ? fileProps
.getProperty(SLC_ORIGIN_M2_REPO
.toString())
154 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
155 // use file name as symbolic name
156 String symbolicName
= bndFile
.getFileName().toString();
157 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
158 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
161 String m2Coordinates
= fileProps
.getProperty(SLC_ORIGIN_M2
.toString());
162 if (m2Coordinates
== null)
163 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile
);
164 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
165 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
166 Path downloaded
= download(url
, originBase
, artifact
);
168 Path targetBundleDir
= processBndJar(downloaded
, targetCategoryBase
, fileProps
, artifact
);
170 downloadAndProcessM2Sources(repoStr
, artifact
, targetBundleDir
);
172 createJar(targetBundleDir
);
173 } catch (Exception e
) {
174 throw new RuntimeException("Cannot process " + bndFile
, e
);
178 /** Process multiple Maven artifacts. */
179 public void processM2BasedDistributionUnit(Path duDir
) {
181 // String category = duDir.getParent().getFileName().toString();
182 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
183 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
186 Path mergeBnd
= duDir
.resolve(MERGE_BND
);
187 if (Files
.exists(mergeBnd
)) {
188 mergeM2Artifacts(mergeBnd
);
192 Path commonBnd
= duDir
.resolve(COMMON_BND
);
193 if (!Files
.exists(commonBnd
)) {
196 Properties commonProps
= new Properties();
197 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
198 commonProps
.load(in
);
201 String m2Version
= commonProps
.getProperty(SLC_ORIGIN_M2
.toString());
202 if (m2Version
== null) {
203 logger
.log(Level
.WARNING
, "Ignoring " + duDir
+ " as it is not an M2-based distribution unit");
204 return;// ignore, this is probably an Eclipse archive
206 if (!m2Version
.startsWith(":")) {
207 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
209 m2Version
= m2Version
.substring(1);
211 DirectoryStream
<Path
> ds
= Files
.newDirectoryStream(duDir
,
212 (p
) -> p
.getFileName().toString().endsWith(".bnd") && !p
.getFileName().toString().equals(COMMON_BND
)
213 && !p
.getFileName().toString().equals(MERGE_BND
));
215 Properties fileProps
= new Properties();
216 try (InputStream in
= Files
.newInputStream(p
)) {
219 String m2Coordinates
= fileProps
.getProperty(SLC_ORIGIN_M2
.toString());
220 M2Artifact artifact
= new M2Artifact(m2Coordinates
);
222 artifact
.setVersion(m2Version
);
224 // prepare manifest entries
225 Properties mergeProps
= new Properties();
226 mergeProps
.putAll(commonProps
);
228 fileEntries
: for (Object key
: fileProps
.keySet()) {
229 if (ManifestConstants
.SLC_ORIGIN_M2
.toString().equals(key
))
230 continue fileEntries
;
231 String value
= fileProps
.getProperty(key
.toString());
232 Object previousValue
= mergeProps
.put(key
.toString(), value
);
233 if (previousValue
!= null) {
234 logger
.log(Level
.WARNING
,
235 commonBnd
+ ": " + key
+ " was " + previousValue
+ ", overridden with " + value
);
238 mergeProps
.put(ManifestConstants
.SLC_ORIGIN_M2
.toString(), artifact
.toM2Coordinates());
239 if (!mergeProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
240 // use file name as symbolic name
241 String symbolicName
= p
.getFileName().toString();
242 symbolicName
= symbolicName
.substring(0, symbolicName
.length() - ".bnd".length());
243 mergeProps
.put(BUNDLE_SYMBOLICNAME
.toString(), symbolicName
);
246 String repoStr
= mergeProps
.containsKey(SLC_ORIGIN_M2_REPO
.toString())
247 ? mergeProps
.getProperty(SLC_ORIGIN_M2_REPO
.toString())
251 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
252 Path downloaded
= download(url
, originBase
, artifact
);
254 Path targetBundleDir
= processBndJar(downloaded
, targetCategoryBase
, mergeProps
, artifact
);
255 // logger.log(Level.DEBUG, () -> "Processed " + downloaded);
258 downloadAndProcessM2Sources(repoStr
, artifact
, targetBundleDir
);
260 createJar(targetBundleDir
);
262 } catch (IOException e
) {
263 throw new RuntimeException("Cannot process " + duDir
, e
);
268 /** Merge multiple Maven artifacts. */
269 protected void mergeM2Artifacts(Path mergeBnd
) throws IOException
{
270 Path duDir
= mergeBnd
.getParent();
271 String category
= duDir
.getParent().getFileName().toString();
272 Path targetCategoryBase
= a2Base
.resolve(category
);
274 Properties mergeProps
= new Properties();
275 try (InputStream in
= Files
.newInputStream(mergeBnd
)) {
280 String m2Version
= mergeProps
.getProperty(SLC_ORIGIN_M2
.toString());
281 if (m2Version
== null) {
282 logger
.log(Level
.WARNING
, "Ignoring " + duDir
+ " as it is not an M2-based distribution unit");
283 return;// ignore, this is probably an Eclipse archive
285 if (!m2Version
.startsWith(":")) {
286 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version
);
288 m2Version
= m2Version
.substring(1);
289 mergeProps
.put(ManifestConstants
.BUNDLE_VERSION
.toString(), m2Version
);
291 String artifactsStr
= mergeProps
.getProperty(ManifestConstants
.SLC_ORIGIN_M2_MERGE
.toString());
292 String repoStr
= mergeProps
.containsKey(SLC_ORIGIN_M2_REPO
.toString())
293 ? mergeProps
.getProperty(SLC_ORIGIN_M2_REPO
.toString())
296 String bundleSymbolicName
= mergeProps
.getProperty(ManifestConstants
.BUNDLE_SYMBOLICNAME
.toString());
297 if (bundleSymbolicName
== null)
298 throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd
);
299 CategoryNameVersion nameVersion
= new M2Artifact(category
+ ":" + bundleSymbolicName
+ ":" + m2Version
);
300 Path targetBundleDir
= targetCategoryBase
.resolve(bundleSymbolicName
+ "." + nameVersion
.getBranch());
302 String
[] artifacts
= artifactsStr
.split(",");
303 artifacts
: for (String str
: artifacts
) {
304 String m2Coordinates
= str
.trim();
305 if ("".equals(m2Coordinates
))
307 M2Artifact artifact
= new M2Artifact(m2Coordinates
.trim());
308 if (artifact
.getVersion() == null)
309 artifact
.setVersion(m2Version
);
310 URL url
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, artifact
);
311 Path downloaded
= download(url
, originBase
, artifact
);
313 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(downloaded
), false)) {
314 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
315 if (entry
.isDirectory())
317 else if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".SF"))
319 else if (entry
.getName().startsWith("META-INF/versions/"))
321 else if (entry
.getName().startsWith("META-INF/maven/"))
323 else if (entry
.getName().equals("module-info.class"))
325 else if (entry
.getName().equals("META-INF/NOTICE"))
327 else if (entry
.getName().equals("META-INF/NOTICE.txt"))
329 else if (entry
.getName().equals("META-INF/LICENSE"))
331 else if (entry
.getName().equals("META-INF/LICENSE.md"))
333 else if (entry
.getName().equals("META-INF/LICENSE-notice.md"))
335 else if (entry
.getName().equals("META-INF/DEPENDENCIES"))
337 if (entry
.getName().startsWith(".cache/")) // Apache SSHD
339 Path target
= targetBundleDir
.resolve(entry
.getName());
340 Files
.createDirectories(target
.getParent());
341 if (!Files
.exists(target
)) {
342 Files
.copy(jarIn
, target
);
344 if (entry
.getName().startsWith("META-INF/services/")) {
345 try (OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
)) {
346 out
.write("\n".getBytes());
347 jarIn
.transferTo(out
);
348 if (logger
.isLoggable(DEBUG
))
349 logger
.log(DEBUG
, artifact
.getArtifactId() + " - Appended " + entry
.getName());
351 } else if (entry
.getName().startsWith("org/apache/batik/")) {
352 logger
.log(Level
.WARNING
, "Skip " + entry
.getName());
355 throw new IllegalStateException("File " + target
+ " from " + artifact
+ " already exists");
358 logger
.log(Level
.TRACE
, () -> "Copied " + target
);
362 downloadAndProcessM2Sources(repoStr
, artifact
, targetBundleDir
);
365 // additional service files
366 Path servicesDir
= duDir
.resolve("services");
367 if (Files
.exists(servicesDir
)) {
368 for (Path p
: Files
.newDirectoryStream(servicesDir
)) {
369 Path target
= targetBundleDir
.resolve("META-INF/services/").resolve(p
.getFileName());
370 try (InputStream in
= Files
.newInputStream(p
);
371 OutputStream out
= Files
.newOutputStream(target
, StandardOpenOption
.APPEND
);) {
372 out
.write("\n".getBytes());
374 if (logger
.isLoggable(DEBUG
))
375 logger
.log(DEBUG
, "Appended " + p
);
380 Map
<String
, String
> entries
= new TreeMap
<>();
381 try (Analyzer bndAnalyzer
= new Analyzer()) {
382 bndAnalyzer
.setProperties(mergeProps
);
383 Jar jar
= new Jar(targetBundleDir
.toFile());
384 bndAnalyzer
.setJar(jar
);
385 Manifest manifest
= bndAnalyzer
.calcManifest();
387 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
388 Object value
= manifest
.getMainAttributes().get(key
);
390 switch (key
.toString()) {
392 case "Bnd-LastModified":
396 if ("Require-Capability".equals(key
.toString())
397 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
398 continue keys
;// hack for very old classes
399 entries
.put(key
.toString(), value
.toString());
400 // logger.log(DEBUG, () -> key + "=" + value);
403 } catch (Exception e
) {
404 throw new RuntimeException("Cannot process " + mergeBnd
, e
);
407 Manifest manifest
= new Manifest();
408 Path manifestPath
= targetBundleDir
.resolve("META-INF/MANIFEST.MF");
409 Files
.createDirectories(manifestPath
.getParent());
410 for (String key
: entries
.keySet()) {
411 String value
= entries
.get(key
);
412 manifest
.getMainAttributes().putValue(key
, value
);
415 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
418 createJar(targetBundleDir
);
421 /** Generate MANIFEST using BND. */
422 protected Path
processBndJar(Path downloaded
, Path targetCategoryBase
, Properties fileProps
, M2Artifact artifact
) {
425 Map
<String
, String
> additionalEntries
= new TreeMap
<>();
426 boolean doNotModify
= Boolean
.parseBoolean(fileProps
427 .getOrDefault(ManifestConstants
.SLC_ORIGIN_MANIFEST_NOT_MODIFIED
.toString(), "false").toString());
429 // we always force the symbolic name
432 fileEntries
: for (Object key
: fileProps
.keySet()) {
433 if (ManifestConstants
.SLC_ORIGIN_M2
.toString().equals(key
))
434 continue fileEntries
;
435 String value
= fileProps
.getProperty(key
.toString());
436 additionalEntries
.put(key
.toString(), value
);
439 if (artifact
!= null) {
440 if (!fileProps
.containsKey(BUNDLE_SYMBOLICNAME
.toString())) {
441 fileProps
.put(BUNDLE_SYMBOLICNAME
.toString(), artifact
.getName());
443 if (!fileProps
.containsKey(BUNDLE_VERSION
.toString())) {
444 fileProps
.put(BUNDLE_VERSION
.toString(), artifact
.getVersion());
448 if (!fileProps
.containsKey(EXPORT_PACKAGE
.toString())) {
449 fileProps
.put(EXPORT_PACKAGE
.toString(),
450 "*;version=\"" + fileProps
.getProperty(BUNDLE_VERSION
.toString()) + "\"");
453 try (Analyzer bndAnalyzer
= new Analyzer()) {
454 bndAnalyzer
.setProperties(fileProps
);
455 Jar jar
= new Jar(downloaded
.toFile());
456 bndAnalyzer
.setJar(jar
);
457 Manifest manifest
= bndAnalyzer
.calcManifest();
459 keys
: for (Object key
: manifest
.getMainAttributes().keySet()) {
460 Object value
= manifest
.getMainAttributes().get(key
);
462 switch (key
.toString()) {
464 case "Bnd-LastModified":
468 if ("Require-Capability".equals(key
.toString())
469 && value
.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
470 continue keys
;// hack for very old classes
471 additionalEntries
.put(key
.toString(), value
.toString());
472 // logger.log(DEBUG, () -> key + "=" + value);
477 Path targetBundleDir
= processBundleJar(downloaded
, targetCategoryBase
, additionalEntries
);
478 logger
.log(Level
.DEBUG
, () -> "Processed " + downloaded
);
479 return targetBundleDir
;
480 } catch (Exception e
) {
481 throw new RuntimeException("Cannot BND process " + downloaded
, e
);
486 /** Download and integrates sources for a single Maven artifact. */
487 protected void downloadAndProcessM2Sources(String repoStr
, M2Artifact artifact
, Path targetBundleDir
)
491 M2Artifact sourcesArtifact
= new M2Artifact(artifact
.toM2Coordinates(), "sources");
492 URL sourcesUrl
= M2ConventionsUtils
.mavenRepoUrl(repoStr
, sourcesArtifact
);
493 Path sourcesDownloaded
= download(sourcesUrl
, originBase
, artifact
, true);
494 processM2SourceJar(sourcesDownloaded
, targetBundleDir
);
495 logger
.log(Level
.TRACE
, () -> "Processed source " + sourcesDownloaded
);
499 /** Integrate sources from a downloaded jar file. */
500 protected void processM2SourceJar(Path file
, Path targetBundleDir
) throws IOException
{
501 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
502 Path targetSourceDir
= targetBundleDir
.resolve("OSGI-OPT/src");
504 // TODO make it less dangerous?
505 if (Files
.exists(targetSourceDir
)) {
506 // deleteDirectory(targetSourceDir);
508 Files
.createDirectories(targetSourceDir
);
513 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
514 if (entry
.isDirectory())
516 if (entry
.getName().startsWith("META-INF"))// skip META-INF entries
518 if (entry
.getName().startsWith("module-info.java"))// skip META-INF entries
520 if (entry
.getName().startsWith("/")) // absolute paths
522 Path target
= targetSourceDir
.resolve(entry
.getName());
523 Files
.createDirectories(target
.getParent());
524 if (!Files
.exists(target
)) {
525 Files
.copy(jarIn
, target
);
526 logger
.log(Level
.TRACE
, () -> "Copied source " + target
);
528 logger
.log(Level
.WARNING
, () -> target
+ " already exists, skipping...");
535 /** Download a Maven artifact. */
536 protected Path
download(URL url
, Path dir
, M2Artifact artifact
) throws IOException
{
537 return download(url
, dir
, artifact
, false);
540 /** Download a Maven artifact. */
541 protected Path
download(URL url
, Path dir
, M2Artifact artifact
, boolean sources
) throws IOException
{
542 return download(url
, dir
, artifact
.getGroupId() + '/' + artifact
.getArtifactId() + "-" + artifact
.getVersion()
543 + (sources ?
"-sources" : "") + ".jar");
550 /** Process an archive in Eclipse format. */
551 public void processEclipseArchive(Path duDir
) {
553 Path categoryRelativePath
= descriptorsBase
.relativize(duDir
.getParent());
554 // String category = categoryRelativePath.getFileName().toString();
555 Path targetCategoryBase
= a2Base
.resolve(categoryRelativePath
);
556 Files
.createDirectories(targetCategoryBase
);
557 // first delete all directories from previous builds
558 for (Path dir
: Files
.newDirectoryStream(targetCategoryBase
, (p
) -> Files
.isDirectory(p
))) {
559 deleteDirectory(dir
);
562 Files
.createDirectories(originBase
);
564 Path commonBnd
= duDir
.resolve(COMMON_BND
);
565 Properties commonProps
= new Properties();
566 try (InputStream in
= Files
.newInputStream(commonBnd
)) {
567 commonProps
.load(in
);
569 String url
= commonProps
.getProperty(ManifestConstants
.SLC_ORIGIN_URI
.toString());
571 url
= uris
.getProperty(duDir
.getFileName().toString());
573 throw new IllegalStateException("No url available for " + duDir
);
574 commonProps
.put(ManifestConstants
.SLC_ORIGIN_URI
.toString(), url
);
576 Path downloaded
= tryDownload(url
, originBase
);
578 FileSystem zipFs
= FileSystems
.newFileSystem(downloaded
, (ClassLoader
) null);
581 List
<PathMatcher
> includeMatchers
= new ArrayList
<>();
582 Properties includes
= new Properties();
583 try (InputStream in
= Files
.newInputStream(duDir
.resolve("includes.properties"))) {
586 for (Object pattern
: includes
.keySet()) {
587 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
588 includeMatchers
.add(pathMatcher
);
591 List
<PathMatcher
> excludeMatchers
= new ArrayList
<>();
592 Path excludeFile
= duDir
.resolve("excludes.properties");
593 if (Files
.exists(excludeFile
)) {
594 Properties excludes
= new Properties();
595 try (InputStream in
= Files
.newInputStream(excludeFile
)) {
598 for (Object pattern
: excludes
.keySet()) {
599 PathMatcher pathMatcher
= zipFs
.getPathMatcher("glob:/" + pattern
);
600 excludeMatchers
.add(pathMatcher
);
604 Files
.walkFileTree(zipFs
.getRootDirectories().iterator().next(), new SimpleFileVisitor
<Path
>() {
607 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
608 includeMatchers
: for (PathMatcher includeMatcher
: includeMatchers
) {
609 if (includeMatcher
.matches(file
)) {
610 for (PathMatcher excludeMatcher
: excludeMatchers
) {
611 if (excludeMatcher
.matches(file
)) {
612 logger
.log(Level
.WARNING
, "Skipping excluded " + file
);
613 return FileVisitResult
.CONTINUE
;
616 if (includeSources
&& file
.getFileName().toString().contains(".source_")) {
617 processEclipseSourceJar(file
, targetCategoryBase
);
618 logger
.log(Level
.DEBUG
, () -> "Processed source " + file
);
621 Map
<String
, String
> map
= new HashMap
<>();
622 for (Object key
: commonProps
.keySet())
623 map
.put(key
.toString(), commonProps
.getProperty(key
.toString()));
624 processBundleJar(file
, targetCategoryBase
, map
);
625 logger
.log(Level
.DEBUG
, () -> "Processed " + file
);
627 break includeMatchers
;
630 return FileVisitResult
.CONTINUE
;
634 DirectoryStream
<Path
> dirs
= Files
.newDirectoryStream(targetCategoryBase
,
635 (p
) -> Files
.isDirectory(p
) && p
.getFileName().toString().indexOf('.') >= 0);
636 for (Path dir
: dirs
) {
639 } catch (IOException e
) {
640 throw new RuntimeException("Cannot process " + duDir
, e
);
645 /** Process sources in Eclipse format. */
646 protected void processEclipseSourceJar(Path file
, Path targetBase
) throws IOException
{
648 Path targetBundleDir
;
649 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
650 Manifest manifest
= jarIn
.getManifest();
652 String
[] relatedBundle
= manifest
.getMainAttributes().getValue("Eclipse-SourceBundle").split(";");
653 String version
= relatedBundle
[1].substring("version=\"".length());
654 version
= version
.substring(0, version
.length() - 1);
655 NameVersion nameVersion
= new NameVersion(relatedBundle
[0], version
);
656 targetBundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
658 Path targetSourceDir
= targetBundleDir
.resolve("OSGI-OPT/src");
660 // TODO make it less dangerous?
661 if (Files
.exists(targetSourceDir
)) {
662 // deleteDirectory(targetSourceDir);
664 Files
.createDirectories(targetSourceDir
);
669 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
670 if (entry
.isDirectory())
672 if (entry
.getName().startsWith("META-INF"))// skip META-INF entries
674 Path target
= targetSourceDir
.resolve(entry
.getName());
675 Files
.createDirectories(target
.getParent());
676 Files
.copy(jarIn
, target
);
677 logger
.log(Level
.TRACE
, () -> "Copied source " + target
);
682 } catch (IOException e
) {
683 throw new IllegalStateException("Cannot process " + file
, e
);
691 /** Normalise a bundle. */
692 protected Path
processBundleJar(Path file
, Path targetBase
, Map
<String
, String
> entries
) throws IOException
{
693 NameVersion nameVersion
;
694 Path targetBundleDir
;
695 try (JarInputStream jarIn
= new JarInputStream(Files
.newInputStream(file
), false)) {
696 Manifest sourceManifest
= jarIn
.getManifest();
697 Manifest manifest
= sourceManifest
!= null ?
new Manifest(sourceManifest
) : new Manifest();
699 // remove problematic entries in MANIFEST
700 manifest
.getEntries().clear();
702 String ourSymbolicName
= entries
.get(BUNDLE_SYMBOLICNAME
.toString());
703 String ourVersion
= entries
.get(BUNDLE_VERSION
.toString());
705 if (ourSymbolicName
!= null && ourVersion
!= null) {
706 nameVersion
= new NameVersion(ourSymbolicName
, ourVersion
);
708 nameVersion
= nameVersionFromManifest(manifest
);
709 if (ourVersion
!= null && !nameVersion
.getVersion().equals(ourVersion
)) {
710 logger
.log(Level
.WARNING
,
711 "Original version is " + nameVersion
.getVersion() + " while new version is " + ourVersion
);
712 entries
.put(BUNDLE_VERSION
.toString(), ourVersion
);
714 if (ourSymbolicName
!= null) {
715 // we always force our symbolic name
716 nameVersion
.setName(ourSymbolicName
);
719 targetBundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
721 // force Java 9 module name
722 entries
.put(ManifestConstants
.AUTOMATIC_MODULE_NAME
.toString(), nameVersion
.getName());
724 boolean isNative
= false;
727 if (targetBundleDir
.startsWith(a2LibBase
)) {
729 Path libRelativePath
= a2LibBase
.relativize(targetBundleDir
);
730 os
= libRelativePath
.getName(0).toString();
731 arch
= libRelativePath
.getName(1).toString();
736 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
737 if (entry
.isDirectory())
739 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".SF"))
741 if (entry
.getName().endsWith("module-info.class")) // skip Java 9 module info
743 if (entry
.getName().startsWith("META-INF/versions/")) // skip multi-version
745 // skip file system providers as they cause issues with native image
746 if (entry
.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider"))
748 if (entry
.getName().startsWith("OSGI-OPT/src/")) // skip embedded sources
750 Path target
= targetBundleDir
.resolve(entry
.getName());
751 Files
.createDirectories(target
.getParent());
752 Files
.copy(jarIn
, target
);
755 if (isNative
&& (entry
.getName().endsWith(".so") || entry
.getName().endsWith(".dll")
756 || entry
.getName().endsWith(".jnilib"))) {
757 Path categoryDir
= targetBundleDir
.getParent();
758 // String[] segments = categoryDir.getFileName().toString().split("\\.");
759 // String arch = segments[segments.length - 1];
760 // String os = segments[segments.length - 2];
761 boolean copyDll
= false;
762 Path targetDll
= categoryDir
.resolve(targetBundleDir
.relativize(target
));
763 if (nameVersion
.getName().equals("com.sun.jna")) {
764 if (arch
.equals("x86_64"))
766 if (os
.equals("macosx"))
768 if (target
.getParent().getFileName().toString().equals(os
+ "-" + arch
)) {
771 targetDll
= categoryDir
.resolve(target
.getFileName());
776 Files
.createDirectories(targetDll
.getParent());
777 if (Files
.exists(targetDll
))
778 Files
.delete(targetDll
);
779 Files
.copy(target
, targetDll
);
781 Files
.delete(target
);
783 logger
.log(Level
.TRACE
, () -> "Copied " + target
);
787 Path manifestPath
= targetBundleDir
.resolve("META-INF/MANIFEST.MF");
788 Files
.createDirectories(manifestPath
.getParent());
789 for (String key
: entries
.keySet()) {
790 String value
= entries
.get(key
);
791 Object previousValue
= manifest
.getMainAttributes().putValue(key
, value
);
792 if (previousValue
!= null && !previousValue
.equals(value
)) {
793 if (ManifestConstants
.IMPORT_PACKAGE
.toString().equals(key
)
794 || ManifestConstants
.EXPORT_PACKAGE
.toString().equals(key
))
795 logger
.log(Level
.TRACE
, file
.getFileName() + ": " + key
+ " was modified");
797 logger
.log(Level
.WARNING
, file
.getFileName() + ": " + key
+ " was " + previousValue
798 + ", overridden with " + value
);
801 // hack to remove unresolvable
802 if (key
.equals("Provide-Capability") || key
.equals("Require-Capability"))
803 if (nameVersion
.getName().equals("osgi.core") || nameVersion
.getName().equals("osgi.cmpn")) {
804 manifest
.getMainAttributes().remove(key
);
807 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
811 return targetBundleDir
;
818 /** Recursively deletes a directory. */
819 private static void deleteDirectory(Path path
) throws IOException
{
820 if (!Files
.exists(path
))
822 Files
.walkFileTree(path
, new SimpleFileVisitor
<Path
>() {
824 public FileVisitResult
postVisitDirectory(Path directory
, IOException e
) throws IOException
{
827 Files
.delete(directory
);
828 return FileVisitResult
.CONTINUE
;
832 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
834 return FileVisitResult
.CONTINUE
;
839 /** Extract name/version from a MANIFEST. */
840 protected NameVersion
nameVersionFromManifest(Manifest manifest
) {
841 Attributes attrs
= manifest
.getMainAttributes();
843 String symbolicName
= attrs
.getValue(ManifestConstants
.BUNDLE_SYMBOLICNAME
.toString());
844 if (symbolicName
== null)
846 // make sure there is no directive
847 symbolicName
= symbolicName
.split(";")[0];
849 String version
= attrs
.getValue(ManifestConstants
.BUNDLE_VERSION
.toString());
850 return new NameVersion(symbolicName
, version
);
853 /** Try to download from an URI. */
854 protected Path
tryDownload(String uri
, Path dir
) throws IOException
{
856 List
<String
> urlBases
= null;
857 String uriPrefix
= null;
858 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
859 if (uri
.startsWith(uriPref
)) {
860 if (mirrors
.get(uriPref
).size() > 0) {
861 urlBases
= mirrors
.get(uriPref
);
867 if (urlBases
== null)
869 return download(new URL(uri
), dir
);
870 } catch (FileNotFoundException e
) {
871 throw new FileNotFoundException("Cannot find " + uri
);
875 for (String urlBase
: urlBases
) {
876 String relativePath
= uri
.substring(uriPrefix
.length());
877 URL url
= new URL(urlBase
+ relativePath
);
879 return download(url
, dir
);
880 } catch (FileNotFoundException e
) {
881 logger
.log(Level
.WARNING
, "Cannot download " + url
+ ", trying another mirror");
884 throw new FileNotFoundException("Cannot find " + uri
);
887 /** Effectively download. */
888 protected Path
download(URL url
, Path dir
) throws IOException
{
889 return download(url
, dir
, (String
) null);
892 /** Effectively download. */
893 protected Path
download(URL url
, Path dir
, String name
) throws IOException
{
897 name
= url
.getPath().substring(url
.getPath().lastIndexOf('/') + 1);
900 dest
= dir
.resolve(name
);
901 if (Files
.exists(dest
)) {
902 logger
.log(Level
.TRACE
, () -> "File " + dest
+ " already exists for " + url
+ ", not downloading again");
905 Files
.createDirectories(dest
.getParent());
908 try (InputStream in
= url
.openStream()) {
909 Files
.copy(in
, dest
);
910 logger
.log(Level
.DEBUG
, () -> "Downloaded " + dest
+ " from " + url
);
915 /** Create a JAR file from a directory. */
916 protected Path
createJar(Path bundleDir
) throws IOException
{
918 Path jarPath
= bundleDir
.getParent().resolve(bundleDir
.getFileName() + ".jar");
919 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
921 try (InputStream in
= Files
.newInputStream(manifestPath
)) {
922 manifest
= new Manifest(in
);
924 try (JarOutputStream jarOut
= new JarOutputStream(Files
.newOutputStream(jarPath
), manifest
)) {
925 jarOut
.setLevel(Deflater
.DEFAULT_COMPRESSION
);
926 Files
.walkFileTree(bundleDir
, new SimpleFileVisitor
<Path
>() {
929 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
930 if (file
.getFileName().toString().equals("MANIFEST.MF"))
931 return super.visitFile(file
, attrs
);
932 JarEntry entry
= new JarEntry(bundleDir
.relativize(file
).toString());
933 jarOut
.putNextEntry(entry
);
934 Files
.copy(file
, jarOut
);
935 return super.visitFile(file
, attrs
);
940 deleteDirectory(bundleDir
);
944 enum ManifestConstants
{
946 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
947 BUNDLE_VERSION("Bundle-Version"), //
948 BUNDLE_LICENSE("Bundle-License"), //
949 EXPORT_PACKAGE("Export-Package"), //
950 IMPORT_PACKAGE("Import-Package"), //
952 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
954 SLC_CATEGORY("SLC-Category"), //
955 SLC_ORIGIN_M2("SLC-Origin-M2"), //
956 SLC_ORIGIN_M2_MERGE("SLC-Origin-M2-Merge"), //
957 SLC_ORIGIN_M2_REPO("SLC-Origin-M2-Repo"), //
958 SLC_ORIGIN_MANIFEST_NOT_MODIFIED("SLC-Origin-ManifestNotModified"), //
959 SLC_ORIGIN_URI("SLC-Origin-URI"),//
964 private ManifestConstants(String value
) {
969 public String
toString() {
977 /** Simple representation of an M2 artifact. */
978 class M2Artifact
extends CategoryNameVersion
{
979 private String classifier
;
981 M2Artifact(String m2coordinates
) {
982 this(m2coordinates
, null);
985 M2Artifact(String m2coordinates
, String classifier
) {
986 String
[] parts
= m2coordinates
.split(":");
987 setCategory(parts
[0]);
989 if (parts
.length
> 2) {
990 setVersion(parts
[2]);
992 this.classifier
= classifier
;
995 String
getGroupId() {
996 return super.getCategory();
999 String
getArtifactId() {
1000 return super.getName();
1003 String
toM2Coordinates() {
1004 return getCategory() + ":" + getName() + (getVersion() != null ?
":" + getVersion() : "");
1007 String
getClassifier() {
1008 return classifier
!= null ? classifier
: "";
1011 String
getExtension() {
1016 /** Utilities around Maven (conventions based). */
1017 class M2ConventionsUtils
{
1018 final static String MAVEN_CENTRAL_BASE_URL
= "https://repo1.maven.org/maven2/";
1020 /** The file name of this artifact when stored */
1021 static String
artifactFileName(M2Artifact artifact
) {
1022 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
1023 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
1024 + artifact
.getExtension();
1027 /** Absolute path to the file */
1028 static String
artifactPath(String artifactBasePath
, M2Artifact artifact
) {
1029 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
1032 /** Absolute path to the file */
1033 static String
artifactUrl(String repoUrl
, M2Artifact artifact
) {
1034 if (repoUrl
.endsWith("/"))
1035 return repoUrl
+ artifactPath("/", artifact
).substring(1);
1037 return repoUrl
+ artifactPath("/", artifact
);
1040 /** Absolute path to the file */
1041 static URL
mavenRepoUrl(String repoBase
, M2Artifact artifact
) {
1042 String url
= artifactUrl(repoBase
== null ? MAVEN_CENTRAL_BASE_URL
: repoBase
, artifact
);
1044 return new URL(url
);
1045 } catch (MalformedURLException e
) {
1046 // it should not happen
1047 throw new IllegalStateException(e
);
1051 /** Absolute path to the directories where the files will be stored */
1052 static String
artifactParentPath(String artifactBasePath
, M2Artifact artifact
) {
1053 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
1056 /** Relative path to the directories where the files will be stored */
1057 static String
artifactParentPath(M2Artifact artifact
) {
1058 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/' + artifact
.getVersion();
1062 private M2ConventionsUtils() {
1066 class CategoryNameVersion
extends NameVersion
{
1067 private String category
;
1069 CategoryNameVersion() {
1072 CategoryNameVersion(String category
, String name
, String version
) {
1073 super(name
, version
);
1074 this.category
= category
;
1077 CategoryNameVersion(String category
, NameVersion nameVersion
) {
1079 this.category
= category
;
1082 String
getCategory() {
1086 void setCategory(String category
) {
1087 this.category
= category
;
1091 public String
toString() {
1092 return category
+ ":" + super.toString();
1097 class NameVersion
implements Comparable
<NameVersion
> {
1098 private String name
;
1099 private String version
;
1104 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1105 NameVersion(String nameVersion
) {
1106 int index
= nameVersion
.indexOf(";version=");
1108 setName(nameVersion
);
1111 setName(nameVersion
.substring(0, index
));
1112 setVersion(nameVersion
.substring(index
+ ";version=".length()));
1116 NameVersion(String name
, String version
) {
1118 this.version
= version
;
1121 NameVersion(NameVersion nameVersion
) {
1122 this.name
= nameVersion
.getName();
1123 this.version
= nameVersion
.getVersion();
1130 void setName(String name
) {
1134 String
getVersion() {
1138 void setVersion(String version
) {
1139 this.version
= version
;
1142 String
getBranch() {
1143 String
[] parts
= getVersion().split("\\.");
1144 if (parts
.length
< 2)
1145 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1146 return parts
[0] + "." + parts
[1];
1150 public boolean equals(Object obj
) {
1151 if (obj
instanceof NameVersion
) {
1152 NameVersion nameVersion
= (NameVersion
) obj
;
1153 return name
.equals(nameVersion
.getName()) && version
.equals(nameVersion
.getVersion());
1159 public int hashCode() {
1160 return name
.hashCode();
1164 public String
toString() {
1165 return name
+ ":" + version
;
1168 public int compareTo(NameVersion o
) {
1169 if (o
.getName().equals(name
))
1170 return version
.compareTo(o
.getVersion());
1172 return name
.compareTo(o
.getName());