Skip to content

Commit

Permalink
CAMEL-19939: camel-jbang - dependency copy - Use camel-tooling-maven (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored Oct 4, 2023
1 parent cd33159 commit 5df9099
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 76 deletions.
1 change: 0 additions & 1 deletion docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,6 @@ org.apache.camel.kamelets:camel-kamelets:0.9.3
=== Copying dependency JARs to a specific directory

You can use the `camel dependency copy` command to copy the required JARs to a specific folder.
This command reuses Maven by invoking the `mvn dependency:copy-dependencies` command.

IMPORTANT: The `camel dependency copy` and `camel dependency list` uses Apache Maven,
This requires having Apache Maven installed, and `mvn` command in PATH environment, so Camel JBang
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@
*/
package org.apache.camel.dsl.jbang.core.commands;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;

import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
import org.apache.camel.util.CamelCaseOrderedProperties;
import org.apache.camel.tooling.maven.MavenArtifact;
import org.apache.camel.tooling.maven.MavenDownloader;
import org.apache.camel.tooling.maven.MavenDownloaderImpl;
import org.apache.camel.tooling.maven.MavenGav;
import org.apache.camel.tooling.maven.MavenResolutionException;
import org.apache.camel.util.FileUtil;
import picocli.CommandLine;

@CommandLine.Command(name = "copy",
description = "Copies all Camel dependencies required to run to a specific directory")
public class DependencyCopy extends Export {
public class DependencyCopy extends DependencyList {
private static final Set<String> EXCLUDED_GROUP_IDS = Set.of("org.fusesource.jansi", "org.apache.logging.log4j");

protected static final String EXPORT_DIR = ".camel-jbang/export";
private MavenDownloader downloader;

@CommandLine.Option(names = { "--output-directory" }, description = "Directory where dependencies should be copied",
defaultValue = "lib", required = true)
Expand All @@ -40,83 +47,53 @@ public DependencyCopy(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;
private void createOutputDirectory() {
Path outputDirectoryPath = Paths.get(outputDirectory);
if (Files.exists(outputDirectoryPath)) {
if (Files.isDirectory(outputDirectoryPath)) {
FileUtil.removeDir(outputDirectoryPath.toFile());
} else {
outputDirectoryParameter += "../../" + outputDirectory;
System.err.println("Error creating the output directory: " + outputDirectory
+ " is not a directory");
return;
}
Process p = Runtime.getRuntime()
.exec("mvn dependency:copy-dependencies -DincludeScope=compile -DexcludeGroupIds=org.fusesource.jansi,org.apache.logging.log4j "
+ outputDirectoryParameter,
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;
try {
Files.createDirectories(outputDirectoryPath);
} catch (IOException e) {
throw new UncheckedIOException("Error creating the output directory: " + outputDirectory, e);
}
}

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");
@Override
protected void outputGav(MavenGav gav) {
try {
List<MavenArtifact> artifacts = getDownloader().resolveArtifacts(
List.of(gav.toString()), Set.of(), true, gav.getVersion().contains("SNAPSHOT"));
for (MavenArtifact artifact : artifacts) {
Path target = Paths.get(outputDirectory, artifact.getFile().getName());
if (Files.exists(target) || EXCLUDED_GROUP_IDS.contains(artifact.getGav().getGroupId())) {
continue;
}
Files.copy(artifact.getFile().toPath(), target);
}
// 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-dummy:1.0";
}
if (runtime == null) {
runtime = "camel-main";
} catch (MavenResolutionException e) {
System.err.println("Error resolving the artifact: " + gav + " due to: " + e.getMessage());
} catch (IOException e) {
System.err.println("Error copying the artifact: " + gav + " due to: " + e.getMessage());
}
}

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;
private MavenDownloader getDownloader() {
if (downloader == null) {
init();
}
return downloader;
}

private void init() {
this.downloader = new MavenDownloaderImpl();
((MavenDownloaderImpl) downloader).build();
createOutputDirectory();
}
}

0 comments on commit 5df9099

Please sign in to comment.