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 43bb531 make SpecVersion more useful
43bb531 is described below
commit 43bb531ed8a2d988486955c96f38bbe292305d04
Author: Stefan Bodewig <[email protected]>
AuthorDate: Thu May 14 12:45:48 2026 +0200
make SpecVersion more useful
---
.../org/apache/ant/cyclonedx/ComponentBomTask.java | 2 +-
src/main/org/apache/ant/cyclonedx/SpecVersion.java | 44 +++++++++++-----------
src/main/org/apache/ant/cyclonedx/ToolData.java | 4 +-
src/tests/antunit/componentbom-test.xml | 28 ++++++++++++++
4 files changed, 54 insertions(+), 24 deletions(-)
diff --git a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
index 1622be2..6117e5d 100644
--- a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
+++ b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
@@ -39,7 +39,7 @@ public class ComponentBomTask extends Task {
private File outputDirectory;
private String bomName = "bom";
- private SpecVersion specVersion = SpecVersion.VERSION_16;
+ private SpecVersion specVersion = SpecVersion.DEFAULT;
private OutputFormat format = OutputFormat.json;
private Component component;
private List<Component> additionalComponents = new ArrayList<>();
diff --git a/src/main/org/apache/ant/cyclonedx/SpecVersion.java
b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
index 1c5879b..94c8160 100644
--- a/src/main/org/apache/ant/cyclonedx/SpecVersion.java
+++ b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
@@ -1,45 +1,47 @@
package org.apache.ant.cyclonedx;
+import java.util.Arrays;
import java.util.Objects;
+import java.util.stream.Stream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.cyclonedx.Version;
+/**
+ * CycloneDX specification version to use for the SBOM.
+ *
+ * <p>Accepts the enum constants like {@code VERSION_16} as well as
+ * the human readable version {@code 1.6}. The values are directly
+ * provided by CycloneDX Core's enum.</p>
+ */
public class SpecVersion extends EnumeratedAttribute {
- public static final SpecVersion VERSION_16;
+ public static final SpecVersion DEFAULT;
static {
- VERSION_16 = new SpecVersion();
- VERSION_16.setValue("1.6");
+ DEFAULT = new SpecVersion();
+ DEFAULT.setValue(Version.VERSION_16.name());
}
@Override
public String[] getValues() {
- return new String[] { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6"
};
+ return Arrays.stream(Version.values())
+ .flatMap(v -> Stream.of(v.name(), v.getVersionString()))
+ .toArray(String[]::new);
}
public Version getVersion() {
- switch (getValue()) {
- case "1.0":
- return Version.VERSION_10;
- case "1.1":
- return Version.VERSION_11;
- case "1.2":
- return Version.VERSION_12;
- case "1.3":
- return Version.VERSION_13;
- case "1.4":
- return Version.VERSION_14;
- case "1.5":
- return Version.VERSION_15;
- case "1.6":
- return Version.VERSION_16;
- default:
- throw new BuildException("version '" + getValue() + "' is not
supported");
+ Version version = Version.fromVersionString(getValue());
+ if (version == null) {
+ try {
+ version = Version.valueOf(getValue());
+ } catch (IllegalArgumentException ex) {
+ throw new BuildException(getValue() + " is not a supported
version");
+ }
}
+ return version;
}
@Override
diff --git a/src/main/org/apache/ant/cyclonedx/ToolData.java
b/src/main/org/apache/ant/cyclonedx/ToolData.java
index f2a06c2..5b76d79 100644
--- a/src/main/org/apache/ant/cyclonedx/ToolData.java
+++ b/src/main/org/apache/ant/cyclonedx/ToolData.java
@@ -20,13 +20,13 @@ import org.cyclonedx.model.metadata.ToolInformation;
/**
* Provides tool information for BOM's metadata section.
*/
-public class ToolData {
+class ToolData {
private static Map<Version, ToolInformation> toolInformationCache = new
HashMap<>();
/**
* Tool Information needed for BOM's metadata section.
*/
- public static ToolInformation getToolInformation(Version specVersion)
throws IOException {
+ static ToolInformation getToolInformation(Version specVersion) throws
IOException {
ToolInformation cachedToolInformation =
toolInformationCache.get(specVersion);
if (cachedToolInformation == null) {
cachedToolInformation = cacheToolInformation(specVersion);
diff --git a/src/tests/antunit/componentbom-test.xml
b/src/tests/antunit/componentbom-test.xml
index 222327f..dc5261a 100644
--- a/src/tests/antunit/componentbom-test.xml
+++ b/src/tests/antunit/componentbom-test.xml
@@ -263,6 +263,34 @@
</au:expectfailure>
</target>
+ <target name="testSpecVersionAsVersionString">
+ <cdx:componentbom
+ outputdirectory="${output}"
+ format="json"
+ specVersion="1.6"
+ xmlns:cdx="antlib:org.apache.ant.cyclonedx">
+ <component name="testname"/>
+ </cdx:componentbom>
+ <au:assertResourceContains
+ xmlns:au="antlib:org.apache.ant.antunit"
+ resource="${output}/bom.json"
+ value='"specVersion" : "1.6"'/>
+ </target>
+
+ <target name="testSpecVersionAsEnumValue">
+ <cdx:componentbom
+ outputdirectory="${output}"
+ format="json"
+ specVersion="VERSION_16"
+ xmlns:cdx="antlib:org.apache.ant.cyclonedx">
+ <component name="testname"/>
+ </cdx:componentbom>
+ <au:assertResourceContains
+ xmlns:au="antlib:org.apache.ant.antunit"
+ resource="${output}/bom.json"
+ value='"specVersion" : "1.6"'/>
+ </target>
+
<target name="testMinimalComponentData">
<cdx:componentbom outputdirectory="${output}" format="xml"
xmlns:cdx="antlib:org.apache.ant.cyclonedx">