forked from CycloneDX/cyclonedx-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for maven optionality, fixes CycloneDX#314
Signed-off-by: Kevin Conner <kev.conner@gmail.com>
- Loading branch information
Showing
16 changed files
with
402 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.cyclonedx.maven; | ||
|
||
import java.io.File; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.cyclonedx.maven.TestUtils.getComponentNode; | ||
import static org.cyclonedx.maven.TestUtils.getElement; | ||
import static org.cyclonedx.maven.TestUtils.readXML; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.cyclonedx.model.Component; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NodeList; | ||
|
||
import io.takari.maven.testing.executor.MavenRuntime.MavenRuntimeBuilder; | ||
import io.takari.maven.testing.executor.MavenVersions; | ||
import io.takari.maven.testing.executor.junit.MavenJUnitTestRunner; | ||
|
||
/** | ||
* Fix BOM handling of conflicting dependency tree graphs | ||
*/ | ||
@RunWith(MavenJUnitTestRunner.class) | ||
@MavenVersions({"3.6.3"}) | ||
public class Issue314Test extends BaseMavenVerifier { | ||
|
||
private static final String ISSUE_314_DEPENDENCY_B = "pkg:maven/com.example.issue_314/dependency_B@1.0.0?type=jar"; | ||
private static final String ISSUE_314_DEPENDENCY_C = "pkg:maven/com.example.issue_314/dependency_C@1.0.0?type=jar"; | ||
private static final String ISSUE_314_DEPENDENCY_D = "pkg:maven/com.example.issue_314/dependency_D@1.0.0?type=jar"; | ||
|
||
public Issue314Test(MavenRuntimeBuilder runtimeBuilder) throws Exception { | ||
super(runtimeBuilder); | ||
} | ||
|
||
/** | ||
* Validate the bytecode analysis components. | ||
* - No component should be marked as optional | ||
*/ | ||
@Test | ||
public void testBytecodeDependencyTree() throws Exception { | ||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put("useMavenOptionality", "false"); | ||
final File projDir = mvnBuild("issue-314", null, null, null, properties); | ||
|
||
final String requiredName = Component.Scope.REQUIRED.getScopeName(); | ||
|
||
final Document bom = readXML(new File(projDir, "dependency_A/target/bom.xml")); | ||
|
||
final NodeList componentsList = bom.getElementsByTagName("components"); | ||
assertEquals("Expected a single components element", 1, componentsList.getLength()); | ||
final Element components = (Element)componentsList.item(0); | ||
|
||
final Element componentBNode = getComponentNode(components, ISSUE_314_DEPENDENCY_B); | ||
final Element componentBScope = getElement(componentBNode, "scope"); | ||
if (componentBScope != null) { | ||
assertEquals("dependency_B scope should be " + requiredName, requiredName, componentBScope.getTextContent()); | ||
} | ||
|
||
final Element componentCNode = getComponentNode(components, ISSUE_314_DEPENDENCY_C); | ||
final Element componentCScope = getElement(componentCNode, "scope"); | ||
if (componentCScope != null) { | ||
assertEquals("dependency_C scope should be " + requiredName, requiredName, componentCScope.getTextContent()); | ||
} | ||
|
||
final Element componentDNode = getComponentNode(components, ISSUE_314_DEPENDENCY_D); | ||
final Element componentDScope = getElement(componentDNode, "scope"); | ||
if (componentDScope != null) { | ||
assertEquals("dependency_D scope should be " + requiredName, requiredName, componentDScope.getTextContent()); | ||
} | ||
} | ||
|
||
/** | ||
* Validate the bytecode analysis components. | ||
* - com.example.issue_314:dependency_C:1.0.0 and com.example.issue_314:dependency_D:1.0.0 *should* be marked as optional | ||
*/ | ||
@Test | ||
public void testMavenOptionalityDependencyTree() throws Exception { | ||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put("useMavenOptionality", "true"); | ||
final File projDir = mvnBuild("issue-314", null, null, null, properties); | ||
|
||
final String requiredName = Component.Scope.REQUIRED.getScopeName(); | ||
final String optionalName = Component.Scope.OPTIONAL.getScopeName(); | ||
|
||
final Document bom = readXML(new File(projDir, "dependency_A/target/bom.xml")); | ||
|
||
final NodeList componentsList = bom.getElementsByTagName("components"); | ||
assertEquals("Expected a single components element", 1, componentsList.getLength()); | ||
final Element components = (Element)componentsList.item(0); | ||
|
||
final Element componentBNode = getComponentNode(components, ISSUE_314_DEPENDENCY_B); | ||
final Element componentBScope = getElement(componentBNode, "scope"); | ||
if (componentBScope != null) { | ||
assertEquals("dependency_B scope should be " + requiredName, requiredName, componentBScope.getTextContent()); | ||
} | ||
|
||
final Element componentCNode = getComponentNode(components, ISSUE_314_DEPENDENCY_C); | ||
final Element componentCScope = getElement(componentCNode, "scope"); | ||
assertNotNull("dependency_C is missing its scope", componentCScope); | ||
assertEquals("dependency_C scope should be " + optionalName, optionalName, componentCScope.getTextContent()); | ||
|
||
final Element componentDNode = getComponentNode(components, ISSUE_314_DEPENDENCY_D); | ||
final Element componentDScope = getElement(componentDNode, "scope"); | ||
assertNotNull("dependency_D is missing its scope", componentDScope); | ||
assertEquals("dependency_D scope should be " + optionalName, optionalName, componentDScope.getTextContent()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>issue_314_parent</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
|
||
<artifactId>dependency_A</artifactId> | ||
|
||
<name>Dependency A</name> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>dependency_B</artifactId> | ||
<version>1.0.0</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.example.issue_314</groupId> | ||
<artifactId>dependency_C</artifactId> | ||
<version>1.0.0</version> | ||
<scope>provided</scope> | ||
<optional>true</optional> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.cyclonedx</groupId> | ||
<artifactId>cyclonedx-maven-plugin</artifactId> | ||
<version>${current.version}</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>makeBom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<projectType>library</projectType> | ||
<schemaVersion>1.4</schemaVersion> | ||
<includeBomSerialNumber>true</includeBomSerialNumber> | ||
<includeCompileScope>true</includeCompileScope> | ||
<includeProvidedScope>true</includeProvidedScope> | ||
<includeRuntimeScope>false</includeRuntimeScope> | ||
<includeSystemScope>false</includeSystemScope> | ||
<includeTestScope>false</includeTestScope> | ||
<includeLicenseText>false</includeLicenseText> | ||
<outputFormat>xml</outputFormat> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
src/test/resources/issue-314/dependency_A/src/main/java/com/example/issue_314/liba/LibA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.issue_314.liba; | ||
|
||
import com.example.issue_314.libb.LibB; | ||
import com.example.issue_314.libc.LibC; | ||
|
||
public class LibA { | ||
private LibA() {} | ||
|
||
public static void main(final String[] args) { | ||
System.out.println("In libA"); | ||
LibB.libBMethod(); | ||
try { | ||
LibC.libCMethod(); | ||
} catch (final NoClassDefFoundError ncdfe) { | ||
System.out.println("Optional library libC not present on classpath"); | ||
} | ||
} | ||
} |
Oops, something went wrong.