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();
700 boolean isSingleton
= false;
701 String rawSourceSymbolicName
= manifest
.getMainAttributes()
702 .getValue(ManifestConstants
.BUNDLE_SYMBOLICNAME
.toString());
703 if (rawSourceSymbolicName
!= null) {
705 // make sure there is no directive
706 String
[] arr
= rawSourceSymbolicName
.split(";");
707 for (int i
= 1; i
< arr
.length
; i
++) {
708 if (arr
[i
].trim().equals("singleton:=true"))
710 logger
.log(DEBUG
, file
.getFileName() + " is a singleton");
714 // remove problematic entries in MANIFEST
715 manifest
.getEntries().clear();
717 String ourSymbolicName
= entries
.get(BUNDLE_SYMBOLICNAME
.toString());
718 String ourVersion
= entries
.get(BUNDLE_VERSION
.toString());
720 if (ourSymbolicName
!= null && ourVersion
!= null) {
721 nameVersion
= new NameVersion(ourSymbolicName
, ourVersion
);
723 nameVersion
= nameVersionFromManifest(manifest
);
724 if (ourVersion
!= null && !nameVersion
.getVersion().equals(ourVersion
)) {
725 logger
.log(Level
.WARNING
,
726 "Original version is " + nameVersion
.getVersion() + " while new version is " + ourVersion
);
727 entries
.put(BUNDLE_VERSION
.toString(), ourVersion
);
729 if (ourSymbolicName
!= null) {
730 // we always force our symbolic name
731 nameVersion
.setName(ourSymbolicName
);
734 targetBundleDir
= targetBase
.resolve(nameVersion
.getName() + "." + nameVersion
.getBranch());
736 // force Java 9 module name
737 entries
.put(ManifestConstants
.AUTOMATIC_MODULE_NAME
.toString(), nameVersion
.getName());
739 boolean isNative
= false;
742 if (targetBundleDir
.startsWith(a2LibBase
)) {
744 Path libRelativePath
= a2LibBase
.relativize(targetBundleDir
);
745 os
= libRelativePath
.getName(0).toString();
746 arch
= libRelativePath
.getName(1).toString();
751 entries
: while ((entry
= jarIn
.getNextJarEntry()) != null) {
752 if (entry
.isDirectory())
754 if (entry
.getName().endsWith(".RSA") || entry
.getName().endsWith(".SF"))
756 if (entry
.getName().endsWith("module-info.class")) // skip Java 9 module info
758 if (entry
.getName().startsWith("META-INF/versions/")) // skip multi-version
760 // skip file system providers as they cause issues with native image
761 if (entry
.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider"))
763 if (entry
.getName().startsWith("OSGI-OPT/src/")) // skip embedded sources
765 Path target
= targetBundleDir
.resolve(entry
.getName());
766 Files
.createDirectories(target
.getParent());
767 Files
.copy(jarIn
, target
);
770 if (isNative
&& (entry
.getName().endsWith(".so") || entry
.getName().endsWith(".dll")
771 || entry
.getName().endsWith(".jnilib"))) {
772 Path categoryDir
= targetBundleDir
.getParent();
773 // String[] segments = categoryDir.getFileName().toString().split("\\.");
774 // String arch = segments[segments.length - 1];
775 // String os = segments[segments.length - 2];
776 boolean copyDll
= false;
777 Path targetDll
= categoryDir
.resolve(targetBundleDir
.relativize(target
));
778 if (nameVersion
.getName().equals("com.sun.jna")) {
779 if (arch
.equals("x86_64"))
781 if (os
.equals("macosx"))
783 if (target
.getParent().getFileName().toString().equals(os
+ "-" + arch
)) {
786 targetDll
= categoryDir
.resolve(target
.getFileName());
791 Files
.createDirectories(targetDll
.getParent());
792 if (Files
.exists(targetDll
))
793 Files
.delete(targetDll
);
794 Files
.copy(target
, targetDll
);
796 Files
.delete(target
);
798 logger
.log(Level
.TRACE
, () -> "Copied " + target
);
802 Path manifestPath
= targetBundleDir
.resolve("META-INF/MANIFEST.MF");
803 Files
.createDirectories(manifestPath
.getParent());
806 entries
.put(BUNDLE_SYMBOLICNAME
.toString(), nameVersion
.getName() + ";singleton:=true");
809 for (String key
: entries
.keySet()) {
810 String value
= entries
.get(key
);
811 Object previousValue
= manifest
.getMainAttributes().putValue(key
, value
);
812 if (previousValue
!= null && !previousValue
.equals(value
)) {
813 if (ManifestConstants
.IMPORT_PACKAGE
.toString().equals(key
)
814 || ManifestConstants
.EXPORT_PACKAGE
.toString().equals(key
))
815 logger
.log(Level
.TRACE
, file
.getFileName() + ": " + key
+ " was modified");
817 logger
.log(Level
.WARNING
, file
.getFileName() + ": " + key
+ " was " + previousValue
818 + ", overridden with " + value
);
821 // hack to remove unresolvable
822 if (key
.equals("Provide-Capability") || key
.equals("Require-Capability"))
823 if (nameVersion
.getName().equals("osgi.core") || nameVersion
.getName().equals("osgi.cmpn")) {
824 manifest
.getMainAttributes().remove(key
);
827 try (OutputStream out
= Files
.newOutputStream(manifestPath
)) {
831 return targetBundleDir
;
838 /** Recursively deletes a directory. */
839 private static void deleteDirectory(Path path
) throws IOException
{
840 if (!Files
.exists(path
))
842 Files
.walkFileTree(path
, new SimpleFileVisitor
<Path
>() {
844 public FileVisitResult
postVisitDirectory(Path directory
, IOException e
) throws IOException
{
847 Files
.delete(directory
);
848 return FileVisitResult
.CONTINUE
;
852 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
854 return FileVisitResult
.CONTINUE
;
859 /** Extract name/version from a MANIFEST. */
860 protected NameVersion
nameVersionFromManifest(Manifest manifest
) {
861 Attributes attrs
= manifest
.getMainAttributes();
863 String symbolicName
= attrs
.getValue(ManifestConstants
.BUNDLE_SYMBOLICNAME
.toString());
864 if (symbolicName
== null)
866 // make sure there is no directive
867 symbolicName
= symbolicName
.split(";")[0];
869 String version
= attrs
.getValue(ManifestConstants
.BUNDLE_VERSION
.toString());
870 return new NameVersion(symbolicName
, version
);
873 /** Try to download from an URI. */
874 protected Path
tryDownload(String uri
, Path dir
) throws IOException
{
876 List
<String
> urlBases
= null;
877 String uriPrefix
= null;
878 uriPrefixes
: for (String uriPref
: mirrors
.keySet()) {
879 if (uri
.startsWith(uriPref
)) {
880 if (mirrors
.get(uriPref
).size() > 0) {
881 urlBases
= mirrors
.get(uriPref
);
887 if (urlBases
== null)
889 return download(new URL(uri
), dir
);
890 } catch (FileNotFoundException e
) {
891 throw new FileNotFoundException("Cannot find " + uri
);
895 for (String urlBase
: urlBases
) {
896 String relativePath
= uri
.substring(uriPrefix
.length());
897 URL url
= new URL(urlBase
+ relativePath
);
899 return download(url
, dir
);
900 } catch (FileNotFoundException e
) {
901 logger
.log(Level
.WARNING
, "Cannot download " + url
+ ", trying another mirror");
904 throw new FileNotFoundException("Cannot find " + uri
);
907 /** Effectively download. */
908 protected Path
download(URL url
, Path dir
) throws IOException
{
909 return download(url
, dir
, (String
) null);
912 /** Effectively download. */
913 protected Path
download(URL url
, Path dir
, String name
) throws IOException
{
917 name
= url
.getPath().substring(url
.getPath().lastIndexOf('/') + 1);
920 dest
= dir
.resolve(name
);
921 if (Files
.exists(dest
)) {
922 logger
.log(Level
.TRACE
, () -> "File " + dest
+ " already exists for " + url
+ ", not downloading again");
925 Files
.createDirectories(dest
.getParent());
928 try (InputStream in
= url
.openStream()) {
929 Files
.copy(in
, dest
);
930 logger
.log(Level
.DEBUG
, () -> "Downloaded " + dest
+ " from " + url
);
935 /** Create a JAR file from a directory. */
936 protected Path
createJar(Path bundleDir
) throws IOException
{
938 Path jarPath
= bundleDir
.getParent().resolve(bundleDir
.getFileName() + ".jar");
939 Path manifestPath
= bundleDir
.resolve("META-INF/MANIFEST.MF");
941 try (InputStream in
= Files
.newInputStream(manifestPath
)) {
942 manifest
= new Manifest(in
);
944 try (JarOutputStream jarOut
= new JarOutputStream(Files
.newOutputStream(jarPath
), manifest
)) {
945 jarOut
.setLevel(Deflater
.DEFAULT_COMPRESSION
);
946 Files
.walkFileTree(bundleDir
, new SimpleFileVisitor
<Path
>() {
949 public FileVisitResult
visitFile(Path file
, BasicFileAttributes attrs
) throws IOException
{
950 if (file
.getFileName().toString().equals("MANIFEST.MF"))
951 return super.visitFile(file
, attrs
);
952 JarEntry entry
= new JarEntry(bundleDir
.relativize(file
).toString());
953 jarOut
.putNextEntry(entry
);
954 Files
.copy(file
, jarOut
);
955 return super.visitFile(file
, attrs
);
960 deleteDirectory(bundleDir
);
964 enum ManifestConstants
{
966 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
967 BUNDLE_VERSION("Bundle-Version"), //
968 BUNDLE_LICENSE("Bundle-License"), //
969 EXPORT_PACKAGE("Export-Package"), //
970 IMPORT_PACKAGE("Import-Package"), //
972 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
974 SLC_CATEGORY("SLC-Category"), //
975 SLC_ORIGIN_M2("SLC-Origin-M2"), //
976 SLC_ORIGIN_M2_MERGE("SLC-Origin-M2-Merge"), //
977 SLC_ORIGIN_M2_REPO("SLC-Origin-M2-Repo"), //
978 SLC_ORIGIN_MANIFEST_NOT_MODIFIED("SLC-Origin-ManifestNotModified"), //
979 SLC_ORIGIN_URI("SLC-Origin-URI"),//
984 private ManifestConstants(String value
) {
989 public String
toString() {
997 /** Simple representation of an M2 artifact. */
998 class M2Artifact
extends CategoryNameVersion
{
999 private String classifier
;
1001 M2Artifact(String m2coordinates
) {
1002 this(m2coordinates
, null);
1005 M2Artifact(String m2coordinates
, String classifier
) {
1006 String
[] parts
= m2coordinates
.split(":");
1007 setCategory(parts
[0]);
1009 if (parts
.length
> 2) {
1010 setVersion(parts
[2]);
1012 this.classifier
= classifier
;
1015 String
getGroupId() {
1016 return super.getCategory();
1019 String
getArtifactId() {
1020 return super.getName();
1023 String
toM2Coordinates() {
1024 return getCategory() + ":" + getName() + (getVersion() != null ?
":" + getVersion() : "");
1027 String
getClassifier() {
1028 return classifier
!= null ? classifier
: "";
1031 String
getExtension() {
1036 /** Utilities around Maven (conventions based). */
1037 class M2ConventionsUtils
{
1038 final static String MAVEN_CENTRAL_BASE_URL
= "https://repo1.maven.org/maven2/";
1040 /** The file name of this artifact when stored */
1041 static String
artifactFileName(M2Artifact artifact
) {
1042 return artifact
.getArtifactId() + '-' + artifact
.getVersion()
1043 + (artifact
.getClassifier().equals("") ?
"" : '-' + artifact
.getClassifier()) + '.'
1044 + artifact
.getExtension();
1047 /** Absolute path to the file */
1048 static String
artifactPath(String artifactBasePath
, M2Artifact artifact
) {
1049 return artifactParentPath(artifactBasePath
, artifact
) + '/' + artifactFileName(artifact
);
1052 /** Absolute path to the file */
1053 static String
artifactUrl(String repoUrl
, M2Artifact artifact
) {
1054 if (repoUrl
.endsWith("/"))
1055 return repoUrl
+ artifactPath("/", artifact
).substring(1);
1057 return repoUrl
+ artifactPath("/", artifact
);
1060 /** Absolute path to the file */
1061 static URL
mavenRepoUrl(String repoBase
, M2Artifact artifact
) {
1062 String url
= artifactUrl(repoBase
== null ? MAVEN_CENTRAL_BASE_URL
: repoBase
, artifact
);
1064 return new URL(url
);
1065 } catch (MalformedURLException e
) {
1066 // it should not happen
1067 throw new IllegalStateException(e
);
1071 /** Absolute path to the directories where the files will be stored */
1072 static String
artifactParentPath(String artifactBasePath
, M2Artifact artifact
) {
1073 return artifactBasePath
+ (artifactBasePath
.endsWith("/") ?
"" : "/") + artifactParentPath(artifact
);
1076 /** Relative path to the directories where the files will be stored */
1077 static String
artifactParentPath(M2Artifact artifact
) {
1078 return artifact
.getGroupId().replace('.', '/') + '/' + artifact
.getArtifactId() + '/' + artifact
.getVersion();
1082 private M2ConventionsUtils() {
1086 class CategoryNameVersion
extends NameVersion
{
1087 private String category
;
1089 CategoryNameVersion() {
1092 CategoryNameVersion(String category
, String name
, String version
) {
1093 super(name
, version
);
1094 this.category
= category
;
1097 CategoryNameVersion(String category
, NameVersion nameVersion
) {
1099 this.category
= category
;
1102 String
getCategory() {
1106 void setCategory(String category
) {
1107 this.category
= category
;
1111 public String
toString() {
1112 return category
+ ":" + super.toString();
1117 class NameVersion
implements Comparable
<NameVersion
> {
1118 private String name
;
1119 private String version
;
1124 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1125 NameVersion(String nameVersion
) {
1126 int index
= nameVersion
.indexOf(";version=");
1128 setName(nameVersion
);
1131 setName(nameVersion
.substring(0, index
));
1132 setVersion(nameVersion
.substring(index
+ ";version=".length()));
1136 NameVersion(String name
, String version
) {
1138 this.version
= version
;
1141 NameVersion(NameVersion nameVersion
) {
1142 this.name
= nameVersion
.getName();
1143 this.version
= nameVersion
.getVersion();
1150 void setName(String name
) {
1154 String
getVersion() {
1158 void setVersion(String version
) {
1159 this.version
= version
;
1162 String
getBranch() {
1163 String
[] parts
= getVersion().split("\\.");
1164 if (parts
.length
< 2)
1165 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1166 return parts
[0] + "." + parts
[1];
1170 public boolean equals(Object obj
) {
1171 if (obj
instanceof NameVersion
) {
1172 NameVersion nameVersion
= (NameVersion
) obj
;
1173 return name
.equals(nameVersion
.getName()) && version
.equals(nameVersion
.getVersion());
1179 public int hashCode() {
1180 return name
.hashCode();
1184 public String
toString() {
1185 return name
+ ":" + version
;
1188 public int compareTo(NameVersion o
) {
1189 if (o
.getName().equals(name
))
1190 return version
.compareTo(o
.getVersion());
1192 return name
.compareTo(o
.getName());