Skip to content

Commit

Permalink
Add OSGi manifest headers for jars
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhoine committed Aug 28, 2024
1 parent 05cc138 commit 89c7929
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ dependencies {
implementation("me.champeau.gradle:japicmp-gradle-plugin:0.4.2")
// Needed for japicmp but not automatically brought in for some reason.
implementation("com.google.guava:guava:32.1.3-jre")
implementation("biz.aQute.bnd:biz.aQute.bnd.gradle:7.0.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import org.gradle.api.provider.Property

abstract class OtelJavaExtension {
abstract val moduleName: Property<String>
abstract val bundleName: Property<String>
}
14 changes: 12 additions & 2 deletions buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
eclipse
idea

id("biz.aQute.bnd.builder")
id("otel.spotless-conventions")
}

Expand Down Expand Up @@ -59,6 +60,9 @@ tasks {
)
}

systemProperty("felix.cache.dir", buildDir)
systemProperty("felix.bundle.path", "$buildDir/libs/${project.base.archivesName.get()}-${project.version}.jar")

testLogging {
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
Expand Down Expand Up @@ -88,11 +92,17 @@ tasks {

manifest {
attributes(
"Automatic-Module-Name" to otelJava.moduleName,
"Automatic-Module-Name" to otelJava.moduleName,
"Built-By" to System.getProperty("user.name"),
"Built-JDK" to System.getProperty("java.version"),
"Implementation-Title" to project.base.archivesName,
"Implementation-Version" to project.version)
"Implementation-Version" to project.version,
// Add OSGi manifest headers with bnd
"-exportcontents" to "${otelJava.moduleName.get()}.*",
"Bundle-Name" to otelJava.bundleName,
"Bundle-SymbolicName" to "${otelJava.moduleName.get()}.${project.base.archivesName.get()}",
"Import-Package" to "io.opentelemetry.api.*;resolution:=optional" // FIXME: should not be optional, dependency should be provided
)
}
}

Expand Down
1 change: 1 addition & 0 deletions semconv-incubating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ base {
archivesName.set("opentelemetry-semconv-incubating")
}
otelJava.moduleName.set("io.opentelemetry.semconv.incubating")
otelJava.bundleName.set("OpenTelemetry - Semantic Conventions Incubating")

dependencies {
api(project(":semconv"))
Expand Down
3 changes: 3 additions & 0 deletions semconv/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ base {
archivesName.set("opentelemetry-semconv")
}
otelJava.moduleName.set("io.opentelemetry.semconv")
otelJava.bundleName.set("OpenTelemetry - Semantic Conventions")

dependencies {
compileOnly("io.opentelemetry:opentelemetry-api")

testImplementation("io.opentelemetry:opentelemetry-api")
testImplementation("org.apache.felix:org.apache.felix.framework:7.0.5")
testImplementation("org.osgi:osgi.core:6.0.0")
}
34 changes: 34 additions & 0 deletions semconv/src/test/java/io/opentelemetry/semconv/OSGiBundleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.opentelemetry.semconv;

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

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.framework.Felix;
import org.junit.jupiter.api.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.launch.Framework;

class OSGiBundleTest {
@Test
void bundleIsActive() throws BundleException {
Map<String,String> params = new HashMap<String, String>();
params.put(Constants.FRAMEWORK_STORAGE, System.getProperty("felix.cache.dir"));

Framework framework = new Felix(params);
framework.init();
framework.start();

BundleContext context = framework.getBundleContext();
File bundleFile = new File(System.getProperty("felix.bundle.path"));

Bundle bundle = context.installBundle(bundleFile.toURI().toString());
bundle.start();

assertEquals(Bundle.ACTIVE, bundle.getState());
}
}

0 comments on commit 89c7929

Please sign in to comment.