Skip to content

Commit

Permalink
Add multi-release processing support
Browse files Browse the repository at this point in the history
Currently bnd is not a capable of processing multi-release jars, this
adds support of multi-release processing by a new directive -release
where one can select what release version should be processed by bnd.

Fixes bndtools#5346
  • Loading branch information
laeubi committed Aug 19, 2022
1 parent 3bf69f8 commit fcffc95
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java
Expand Up @@ -1338,7 +1338,7 @@ public void removeSubDirs(String dir) {
}

private static final Predicate<String> pomXmlFilter = new PathSet("META-INF/maven/*/*/pom.xml").matches();
private int release;
private int release = -1;

public Stream<Resource> getPomXmlResources() {
return getResources(pomXmlFilter);
Expand Down
70 changes: 70 additions & 0 deletions biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java
@@ -0,0 +1,70 @@
package aQute.bnd.build;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.util.Collections;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;

public class JarTest {

private static final String TEST_CLASS_PATH = "a/test/package/Test.class";

private static final String VERSIONED_TEST_CLASS_PATH = "META-INF/versions/9/" + TEST_CLASS_PATH;

@TempDir
File tempDir;

@Test
public void testMultiReleaseJar() throws Exception {
File jarfile = new File(tempDir, "packed.jar");
try (Jar jar = new Jar("testme")) {
jar.ensureManifest();
Resource java8Class = resource();
Resource java9Class = resource();
jar.putResource(TEST_CLASS_PATH, java8Class);
jar.putResource(VERSIONED_TEST_CLASS_PATH, java9Class);
// without a release, content must be returned as-is
assertEquals(java8Class, jar.getResource(TEST_CLASS_PATH));
assertEquals(java9Class, jar.getResource(VERSIONED_TEST_CLASS_PATH));
// with a release set below 9 we should only see the default content
jar.setRelease(0);
assertEquals(java8Class, jar.getResource(TEST_CLASS_PATH));
assertNull(jar.getResource(VERSIONED_TEST_CLASS_PATH));
// with release 9 set, we now should see the java 9 content
jar.setRelease(9);
assertEquals(java9Class, jar.getResource(TEST_CLASS_PATH));
// if we write the jar out, all content should be present
jar.writeFolder(tempDir);
File defaultFile = new File(tempDir, TEST_CLASS_PATH);
assertTrue(defaultFile.isFile(), defaultFile.getAbsolutePath() + " is missing");
File versionedFile = new File(tempDir, VERSIONED_TEST_CLASS_PATH);
assertTrue(versionedFile.isFile(), versionedFile.getAbsolutePath() + " is missing");
jar.write(jarfile);
}
try (JarFile jar = new JarFile(jarfile)) {
Set<String> collect = Collections.list(jar.entries())
.stream()
.map(JarEntry::getName)
.collect(Collectors.toSet());
assertTrue(collect.contains(TEST_CLASS_PATH));
assertTrue(collect.contains(VERSIONED_TEST_CLASS_PATH));
}
}

private Resource resource() {
return new aQute.bnd.osgi.EmbeddedResource(new byte[0], System.currentTimeMillis());
}

}

0 comments on commit fcffc95

Please sign in to comment.