This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ant-antlibs-cyclonedx.git
The following commit(s) were added to refs/heads/main by this push:
new b00b3d9 make format configurable
b00b3d9 is described below
commit b00b3d9f9aa80326dea796189fc25df9b61c6abe
Author: Stefan Bodewig <[email protected]>
AuthorDate: Mon Apr 27 19:37:09 2026 +0200
make format configurable
this has the benefit of making tests easier to write thanks to xmlproperty
---
.../org/apache/ant/cyclonedx/ComponentBomTask.java | 26 ++++++++++++
src/tests/antunit/componentbom-test.xml | 49 +++++++++++++++++++++-
2 files changed, 74 insertions(+), 1 deletion(-)
diff --git a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
index 3fc9840..a081757 100644
--- a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
+++ b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
@@ -12,10 +12,12 @@ import java.util.UUID;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
+import org.cyclonedx.Format;
import org.cyclonedx.Version;
import org.cyclonedx.exception.GeneratorException;
import org.cyclonedx.generators.BomGeneratorFactory;
import org.cyclonedx.generators.json.BomJsonGenerator;
+import org.cyclonedx.generators.xml.BomXmlGenerator;
import org.cyclonedx.model.Bom;
import org.cyclonedx.model.LifecycleChoice;
import org.cyclonedx.model.Lifecycles;
@@ -27,11 +29,16 @@ import org.cyclonedx.model.Metadata;
public class ComponentBomTask extends Task {
private File bomFile;
+ private Format format = Format.JSON;
public void setBomFile(File f) {
bomFile = f;
}
+ public void setFormat(Format format) {
+ this.format = format;
+ }
+
public void execute() {
try {
Bom bom = createBom();
@@ -60,10 +67,29 @@ public class ComponentBomTask extends Task {
}
private void writeBom(Bom bom, File bomFile) throws IOException,
GeneratorException {
+ switch (format) {
+ case JSON:
+ writeJsonBom(bom, bomFile);
+ break;
+ case XML:
+ writeXmlBom(bom, bomFile);
+ break;
+ }
+ }
+
+ private void writeJsonBom(Bom bom, File bomFile) throws IOException,
GeneratorException {
BomJsonGenerator generator =
BomGeneratorFactory.createJson(Version.VERSION_16, bom);
try (FileOutputStream fos = new FileOutputStream(bomFile);
OutputStreamWriter writer = new OutputStreamWriter(fos,
StandardCharsets.UTF_8)) {
writer.write(generator.toJsonString(true));
}
}
+
+ private void writeXmlBom(Bom bom, File bomFile) throws IOException,
GeneratorException {
+ BomXmlGenerator generator =
BomGeneratorFactory.createXml(Version.VERSION_16, bom);
+ try (FileOutputStream fos = new FileOutputStream(bomFile);
+ OutputStreamWriter writer = new OutputStreamWriter(fos,
StandardCharsets.UTF_8)) {
+ writer.write(generator.toXmlString());
+ }
+ }
}
diff --git a/src/tests/antunit/componentbom-test.xml
b/src/tests/antunit/componentbom-test.xml
index 648e91c..c3c0ac4 100644
--- a/src/tests/antunit/componentbom-test.xml
+++ b/src/tests/antunit/componentbom-test.xml
@@ -19,7 +19,7 @@
<import file="shared.xml" />
- <target name="testToolMetadata">
+ <target name="testToolMetadataInJsonFormat">
<checksum property="sha256hash" file="${antlib.location}"
algorithm="SHA-256"/>
<mkdir dir="${output}"/>
<cdx:componentbom bomfile="${output}/bom.json"
@@ -46,4 +46,51 @@
value='"content" : "${sha256hash}"'/>
</target>
+ <target name="testToolMetadataInXmlFormat">
+ <checksum property="sha256hash" file="${antlib.location}"
algorithm="SHA-256"/>
+ <mkdir dir="${output}"/>
+ <cdx:componentbom bomfile="${output}/bom.xml" format="XML"
+ xmlns:cdx="antlib:org.apache.ant.cyclonedx"/>
+ <xmlproperty file="${output}/bom.xml"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.lifecycles.lifecycle.phase"
+ value="build"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component(type)"
+ value="library"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.name"
+ value="ant-cyclonedx"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.group"
+ value="org.apache.ant"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.version"
+ value="${artifact.version}"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.description"
+ value="Apache CycloneDX Antlib"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.manufacturer.name"
+ value="Apache Ant Development Team"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.manufacturer.url"
+ value="https://ant.apache.org/"/>
+ <au:assertPropertyEquals
+ xmlns:au="antlib:org.apache.ant.antunit"
+ name="bom.metadata.tools.components.component.licenses.license.id"
+ value="Apache-2.0"/>
+ <au:assertResourceContains
+ xmlns:au="antlib:org.apache.ant.antunit"
+ resource="${output}/bom.xml"
+ value='<hash alg="SHA-256">${sha256hash}</hash>'/>
+ </target>
</project>