Skip to content

Commit 249e6d0

Browse files
committed
Add support for using conditional packages directive.
bndtools has a feature named "conditional package"[1] that allows to slurp-in certain code into the final jar for example to prevent having a dependency on a full module when maybe only a single class is used, Currently when using maven targets this can not be used because it is a feature only supported in the builder, this now adds support for using this as well as a test-case to demonstrate it. [1] https://bnd.bndtools.org/instructions/conditionalpackage.html
1 parent dee4a9d commit 249e6d0

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

org.eclipse.m2e.pde.target.tests/src/org/eclipse/m2e/pde/target/tests/OSGiMetadataGenerationTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertNull;
2020
import static org.junit.Assert.assertTrue;
2121

22+
import java.io.File;
2223
import java.net.URI;
2324
import java.util.Arrays;
2425
import java.util.List;
@@ -28,6 +29,7 @@
2829
import java.util.jar.Attributes;
2930

3031
import org.eclipse.core.runtime.IStatus;
32+
import org.eclipse.equinox.frameworkadmin.BundleInfo;
3133
import org.eclipse.osgi.util.ManifestElement;
3234
import org.eclipse.pde.core.target.ITargetLocation;
3335
import org.eclipse.pde.core.target.TargetBundle;
@@ -37,6 +39,8 @@
3739
import org.osgi.framework.Version;
3840
import org.osgi.framework.VersionRange;
3941

42+
import aQute.bnd.osgi.Jar;
43+
4044
public class OSGiMetadataGenerationTest extends AbstractMavenTargetTest {
4145

4246
@Test
@@ -286,6 +290,44 @@ public void testNonOSGiArtifact_missingArtifactGenerate_defaultInstructions() th
286290
assertNull(sourceAttributes.getValue(Constants.DYNAMICIMPORT_PACKAGE));
287291
}
288292

293+
@Test
294+
public void testConditionalPackage() throws Exception {
295+
ITargetLocation target = resolveMavenTarget(
296+
"""
297+
<location includeDependencyDepth="none" includeDependencyScopes="compile" includeSource="false" label="Maven-archetype" missingManifest="generate" type="Maven">
298+
<dependencies>
299+
<dependency>
300+
<groupId>org.apache.maven.archetype</groupId>
301+
<artifactId>archetype-common</artifactId>
302+
<version>3.3.1</version>
303+
<type>jar</type>
304+
</dependency>
305+
</dependencies>
306+
<instructions><![CDATA[
307+
Bundle-Name: Bundle in Test from artifact ${mvnGroupId}:${mvnArtifactId}:${mvnVersion}:${mvnClassifier}
308+
version: ${version_cleanup;${mvnVersion}}
309+
Bundle-SymbolicName: m2e.custom.test.wrapped.${mvnArtifactId}
310+
Bundle-Version: ${version}
311+
Import-Package: *
312+
Export-Package: org.apache.maven.archetype;version="${version}";-noimport:=true
313+
-conditionalpackage: org.apache.commons.*
314+
]]></instructions>
315+
</location>
316+
""");
317+
assertStatusOk(getTargetStatus(target));
318+
TargetBundle[] bundles = target.getBundles();
319+
for (TargetBundle bundle : bundles) {
320+
BundleInfo bundleInfo = bundle.getBundleInfo();
321+
File file = new File(bundleInfo.getLocation());
322+
try (Jar jar = new Jar(file)) {
323+
Set<String> resources = jar.getResources().keySet();
324+
assertTrue("Conditional package org.apache.commons.* not included.",
325+
resources.stream().anyMatch(s -> s.startsWith("org/apache/commons/")));
326+
}
327+
;
328+
}
329+
}
330+
289331
@Test
290332
public void testNonOSGiArtifact_missingArtifactGenerate_customInstructions() throws Exception {
291333
ITargetLocation target = resolveMavenTarget(

org.eclipse.m2e.pde.target/src/org/eclipse/m2e/pde/target/shared/MavenBundleWrapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@
6060
import org.eclipse.m2e.pde.target.shared.ProcessingMessage.Type;
6161
import org.osgi.framework.Constants;
6262

63-
import aQute.bnd.osgi.Analyzer;
63+
import aQute.bnd.osgi.Builder;
6464
import aQute.bnd.osgi.Jar;
65+
import aQute.bnd.osgi.Processor;
6566
import aQute.bnd.version.Version;
6667

6768
/**
@@ -204,7 +205,8 @@ private static WrappedBundle getWrappedNode(DependencyNode node,
204205
List<ProcessingMessage> messages = new ArrayList<>();
205206
wrapArtifactFile.getParentFile().mkdirs();
206207
boolean hasErrors = false;
207-
try (Analyzer analyzer = new Analyzer(analyzerJar);) {
208+
try (Builder analyzer = new Builder(new Processor());) {
209+
analyzer.setJar(analyzerJar);
208210
analyzer.setProperty("mvnGroupId", artifact.getGroupId());
209211
analyzer.setProperty("mvnArtifactId", artifact.getArtifactId());
210212
analyzer.setProperty("mvnVersion", artifact.getBaseVersion());

0 commit comments

Comments
 (0)