Skip to content

Commit

Permalink
fix #913 Apply BND convention to shadowJar as well
Browse files Browse the repository at this point in the history
The bnd plugin only applies to the default `jar` task. Since we're now
using the shadow plugin, `shadowJar` has no OSGI data in its MANIFEST.

This commit fixes the situation by applying the bnd plugin convention to
the shadowJar task. It also copies the bnd configuration since this is
not inherited.

The manifest content is tested for key OSGI and Java metadata (not
including the detail of the import/export, though).
  • Loading branch information
simonbasle committed Dec 3, 2019
1 parent daa6b54 commit 1d8782b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import aQute.bnd.gradle.BundleTaskConvention

buildscript {
repositories {
Expand Down Expand Up @@ -303,7 +304,6 @@ configure(rootProject) { project ->
}
}


jar {
classifier = 'original'

Expand All @@ -319,6 +319,14 @@ configure(rootProject) { project ->
check.dependsOn jacocoTestReport

shadowJar {
configure {
it.convention.plugins.bundle = new BundleTaskConvention(it)
doLast {
buildBundle()
}
}
bnd(bndOptions)

classifier = null

dependsOn(project.tasks.jar)
Expand Down
3 changes: 2 additions & 1 deletion src/jarFileTest/java/reactor/netty/AbstractJarFileTest.java
Expand Up @@ -28,11 +28,12 @@
*/
class AbstractJarFileTest {

static Path jarFilePath;
static Path root;

static {
try {
Path jarFilePath = Paths.get(System.getProperty("jarFile"));
jarFilePath = Paths.get(System.getProperty("jarFile"));
URI jarFileUri = new URI("jar", jarFilePath.toUri().toString(), null);
FileSystem fileSystem = FileSystems.newFileSystem(jarFileUri, emptyMap());
root = fileSystem.getPath("/");
Expand Down
65 changes: 63 additions & 2 deletions src/jarFileTest/java/reactor/netty/JarFileShadingTest.java
Expand Up @@ -15,17 +15,29 @@
*/
package reactor.netty;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.assertj.core.api.ListAssert;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.*;

/**
* This test must be executed with Gradle because it requires a shadow JAR
* This test must be executed with Gradle because it requires a shadow JAR.
* For example it can be run with {@code ./gradlew jarFileTest --tests *JarFileShadingTest}
*/
public class JarFileShadingTest extends AbstractJarFileTest {

Expand Down Expand Up @@ -59,6 +71,55 @@ public void testMetaInf() throws Exception {
}
}

@Test
public void testManifestContent() throws IOException {
ZipFile jar = new ZipFile(jarFilePath.toString());
ZipEntry manifest = jar
.stream()
.filter(ze -> ze.getName().equals("META-INF/MANIFEST.MF"))
.findFirst()
.get();

String version = jarFilePath.getFileName().toString()
.replace("reactor-netty-", "")
.replace("-original.jar", "")
.replace(".jar", "");
//for the case where there is a customVersion. OSGI only want 4 components to the version,
//so BND would simply remove the 4th dot between customVersion and RELEASE/BUILD-SNAPSHOT.
String osgiVersion = Arrays.stream(version.split("\\.", 4))
.map(comp -> comp.replace(".", ""))
.collect(Collectors.joining("."));

try (InputStream inputStream = jar.getInputStream(manifest);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) {
String lines = reader.lines()
//we eliminate the intermediate details of import-package & co
//since we won't assert them -> so that assertion error is more readable
.filter(s -> !s.startsWith(" "))
.collect(Collectors.joining("\n"));
assertThat(lines)
.as("base content")
.contains(
"Implementation-Title: reactor-netty",
"Implementation-Version: " + version,
"Automatic-Module-Name: reactor.netty"
);
assertThat(lines)
.as("OSGI content")
.contains(
"Bundle-Name: reactor-netty",
"Bundle-SymbolicName: io.projectreactor.netty.reactor-netty",
"Import-Package: ", //only assert the section is there
"Require-Capability:",
"Export-Package:", //only assert the section is there
"Bundle-Version: " + osgiVersion
);
}
catch (IOException ioe) {
fail("Coudn't inspect the manifest", ioe);
}
}

@SuppressWarnings("unchecked")
private ListAssert<String> assertThatFileList(Stream<Path> path) {
return (ListAssert) assertThat(path)
Expand Down

0 comments on commit 1d8782b

Please sign in to comment.