gnodet-bot commented on code in PR #14093:
URL: https://github.com/apache/maven/pull/14093#discussion_r4657980558


##########
impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/SbomReportWriter.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.cling.invoker.mvn;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Instant;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * Writes a CycloneDX 1.6 JSON SBOM from the resolved artifacts of the build.
+ * Hand-rolled encoder: maven-cli has no JSON library dependency, and adding 
one
+ * for a single report is not justified.
+ *
+ * <p>Only fields Maven actually retains in the resolved artifact set are 
emitted:
+ * component group/name/version and a synthesised purl, plus dependency edges.
+ * Supplier, license and hash are omitted, never fabricated. Components and 
edges
+ * are sorted and the timestamp honours {@code SOURCE_DATE_EPOCH}, so the 
output
+ * is deterministic.</p>
+ */
+final class SbomReportWriter {
+    private SbomReportWriter() {}
+
+    static void write(Path file, MavenExecutionResult result) throws 
IOException {
+        Map<String, String[]> components = new TreeMap<>();
+        Map<String, Set<String>> dependencies = new TreeMap<>();
+        for (MavenProject project : result.getTopologicallySortedProjects()) {
+            String rootPurl = purl(project.getGroupId(), 
project.getArtifactId(), project.getVersion());
+            if (rootPurl == null) {
+                continue;
+            }
+            components.put(
+                    rootPurl,
+                    new String[] {project.getGroupId(), 
project.getArtifactId(), project.getVersion(), rootPurl});
+            for (Artifact artifact : project.getArtifacts()) {
+                String depPurl = purl(artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getVersion());
+                if (depPurl == null || depPurl.equals(rootPurl)) {
+                    continue;
+                }
+                components.put(
+                        depPurl,
+                        new String[] {artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getVersion(), depPurl});
+                dependencies.computeIfAbsent(rootPurl, k -> new 
TreeSet<>()).add(depPurl);
+            }
+        }
+
+        StringBuilder sb = new StringBuilder();
+        sb.append("{\n");
+        sb.append("  \"bomFormat\": \"CycloneDX\",\n");
+        sb.append("  \"specVersion\": \"1.6\",\n");
+        sb.append("  \"version\": 1,\n");
+        sb.append("  \"metadata\": {\n");
+        sb.append("    \"timestamp\": \"").append(timestamp()).append("\",\n");
+        sb.append("    \"tools\": [{\"name\": \"mvn\"}],\n");

Review Comment:
   ⚠️ **[LOW]** The `tools` field uses the deprecated array format. CycloneDX 
1.6 defines `tools` as a `oneOf`; the array form is `[Deprecated]` in the 
schema. Use the new object form instead:
   
   ```suggestion
           sb.append("    \"tools\": {\"components\": [{\"type\": 
\"application\", \"name\": \"mvn\"}]},\n");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to