This is an automated email from the ASF dual-hosted git repository.
acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 57b0c31bddd CAMEL-19644 - camel-jbang - Add command to generate SBOM
report (#11601)
57b0c31bddd is described below
commit 57b0c31bddd75aa4a1d321c5030f048cdbef34b7
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Sep 28 17:07:43 2023 +0200
CAMEL-19644 - camel-jbang - Add command to generate SBOM report (#11601)
Signed-off-by: Andrea Cosentino <[email protected]>
---
.../dsl/jbang/core/commands/CamelJBangMain.java | 3 +-
.../dsl/jbang/core/commands/SBOMGenerator.java | 131 +++++++++++++++++++++
2 files changed, 133 insertions(+), 1 deletion(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 78310fa8e6a..7f0f22e1007 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -150,7 +150,8 @@ public class CamelJBangMain implements Callable<Integer> {
.addSubcommand("version", new CommandLine(new
VersionCommand(main))
.addSubcommand("get", new CommandLine(new
VersionGet(main)))
.addSubcommand("set", new CommandLine(new
VersionSet(main)))
- .addSubcommand("list", new CommandLine(new
VersionList(main))));
+ .addSubcommand("list", new CommandLine(new
VersionList(main))))
+ .addSubcommand("sbom", new CommandLine(new
SBOMGenerator(main)));
commandLine.getCommandSpec().versionProvider(() -> {
CamelCatalog catalog = new DefaultCamelCatalog();
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SBOMGenerator.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SBOMGenerator.java
new file mode 100644
index 00000000000..859bd7a4e94
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/SBOMGenerator.java
@@ -0,0 +1,131 @@
+/*
+ * 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.camel.dsl.jbang.core.commands;
+
+import java.io.File;
+import java.nio.file.Paths;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
+import org.apache.camel.util.CamelCaseOrderedProperties;
+import org.apache.camel.util.FileUtil;
+import picocli.CommandLine;
+
[email protected](name = "sbom",
+ description = "Generate a CycloneDX SBOM for a specific
project")
+public class SBOMGenerator extends Export {
+
+ protected static final String EXPORT_DIR = ".camel-jbang/export";
+
+ @CommandLine.Option(names = { "--output-directory" }, description =
"Directory where the SBOM will be saved",
+ defaultValue = ".")
+ protected String outputDirectory;
+
+ @CommandLine.Option(names = { "--output-name" }, description = "Output
name of the SBOM file",
+ defaultValue = "sbom")
+ protected String outputName;
+
+ @CommandLine.Option(names = { "--plugin-version" }, description = "The
CycloneDX Maven Plugin version",
+ defaultValue = "2.7.9")
+ protected String pluginVersion = "2.7.9";
+
+ public SBOMGenerator(CamelJBangMain main) {
+ super(main);
+ }
+
+ @Override
+ public Integer doCall() throws Exception {
+ this.quiet = true; // be quiet and generate from fresh data to ensure
the output is up-to-date
+ return super.doCall();
+ }
+
+ @Override
+ protected Integer export() throws Exception {
+ Integer answer = doExport();
+ if (answer == 0) {
+ File buildDir = new File(EXPORT_DIR);
+ String outputDirectoryParameter = "-DoutputDirectory=";
+ if (Paths.get(outputDirectory).isAbsolute()) {
+ outputDirectoryParameter += outputDirectory;
+ } else {
+ outputDirectoryParameter += "../../" + outputDirectory;
+ }
+ Process p = Runtime.getRuntime()
+ .exec("mvn org.cyclonedx:cyclonedx-maven-plugin:" +
pluginVersion + ":makeAggregateBom "
+ + outputDirectoryParameter
+ + " -DoutputName="
+ + outputName,
+ null,
+ buildDir);
+ boolean done = p.waitFor(60, TimeUnit.SECONDS);
+ if (!done) {
+ answer = 1;
+ }
+ if (p.exitValue() != 0) {
+ answer = p.exitValue();
+ }
+ // cleanup dir after complete
+ FileUtil.removeDir(buildDir);
+ }
+ return answer;
+ }
+
+ protected Integer doExport() throws Exception {
+ // read runtime and gav from profile if not configured
+ File profile = new File(getProfile() + ".properties");
+ if (profile.exists()) {
+ Properties prop = new CamelCaseOrderedProperties();
+ RuntimeUtil.loadProperties(prop, profile);
+ if (this.runtime == null) {
+ this.runtime = prop.getProperty("camel.jbang.runtime");
+ }
+ if (this.gav == null) {
+ this.gav = prop.getProperty("camel.jbang.gav");
+ }
+ // allow configuring versions from profile
+ this.javaVersion = prop.getProperty("camel.jbang.javaVersion",
this.javaVersion);
+ this.camelVersion = prop.getProperty("camel.jbang.camelVersion",
this.camelVersion);
+ this.kameletsVersion =
prop.getProperty("camel.jbang.kameletsVersion", this.kameletsVersion);
+ this.localKameletDir =
prop.getProperty("camel.jbang.localKameletDir", this.localKameletDir);
+ this.quarkusGroupId =
prop.getProperty("camel.jbang.quarkusGroupId", this.quarkusGroupId);
+ this.quarkusArtifactId =
prop.getProperty("camel.jbang.quarkusArtifactId", this.quarkusArtifactId);
+ this.quarkusVersion =
prop.getProperty("camel.jbang.quarkusVersion", this.quarkusVersion);
+ this.springBootVersion =
prop.getProperty("camel.jbang.springBootVersion", this.springBootVersion);
+ }
+
+ // use temporary export dir
+ exportDir = EXPORT_DIR;
+ if (gav == null) {
+ gav = "org.apache.camel:camel-jbang-export:1.0";
+ }
+ if (runtime == null) {
+ runtime = "camel-main";
+ }
+
+ if ("spring-boot".equals(runtime) ||
"camel-spring-boot".equals(runtime)) {
+ return export(new ExportSpringBoot(getMain()));
+ } else if ("quarkus".equals(runtime) ||
"camel-quarkus".equals(runtime)) {
+ return export(new ExportQuarkus(getMain()));
+ } else if ("main".equals(runtime) || "camel-main".equals(runtime)) {
+ return export(new ExportCamelMain(getMain()));
+ } else {
+ System.err.println("Unknown runtime: " + runtime);
+ return 1;
+ }
+ }
+}