This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 748cc89b3d9cc4d631ff987e217b4064e411c454 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 7 10:10:29 2022 +0200 CAMEL-18079: camel-jbang - Add support for 3rd party maven repositories --- .../apache/camel/dsl/jbang/core/commands/Run.java | 8 ++++- .../camel/dsl/jbang/core/commands/UberJar.java | 6 ++++ .../org/apache/camel/main/DownloaderHelper.java | 33 ++++++++++++++++++ .../java/org/apache/camel/main/KameletMain.java | 25 ++++++++++++++ .../src/main/resources/camelGrapeConfig.xml | 40 ++++++++++++++++++++++ 5 files changed, 111 insertions(+), 1 deletion(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java index 6c144f5ca51..3fb0f8f7e18 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java @@ -99,9 +99,12 @@ class Run extends CamelCommand { String[] files; @Option(names = { - "--dep", "--deps" }, description = "Add additional dependencies (Use commas to separate them).") + "--dep", "--deps" }, description = "Add additional dependencies (Use commas to separate multiple dependencies).") String dependencies; + @Option(names = {"--repos"}, description = "Additional maven repositories for download on-demand (Use commas to separate multiple repositories).") + String repos; + @Option(names = { "--name" }, defaultValue = "CamelJBang", description = "The name of the Camel application") String name; @@ -286,6 +289,7 @@ class Run extends CamelCommand { } else { propertiesFiles = propertiesFiles + ",file:" + profilePropertiesFile.getName(); } + repos = profileProperties.getProperty("camel.jbang.repos", repos); } // if no specific file to run then try to auto-detect @@ -308,6 +312,7 @@ class Run extends CamelCommand { final KameletMain main = createMainInstance(); final Set<String> downloaded = new HashSet<>(); + main.setRepos(repos); main.setDownloadListener(new DownloadListener() { @Override public void onDownloadDependency(String groupId, String artifactId, String version) { @@ -338,6 +343,7 @@ class Run extends CamelCommand { // allow java-dsl to compile to .class which we need in uber-jar mode writeSetting(main, profileProperties, "camel.main.routesCompileDirectory", WORK_DIR); writeSetting(main, profileProperties, "camel.jbang.dependencies", dependencies); + writeSetting(main, profileProperties, "camel.jbang.repos", repos); writeSetting(main, profileProperties, "camel.jbang.health", health ? "true" : "false"); writeSetting(main, profileProperties, "camel.jbang.console", console ? "true" : "false"); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java index 81e555285d5..4e8616bdf90 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/UberJar.java @@ -141,6 +141,12 @@ class UberJar extends CamelCommand { buildDir = new File(LIB_DIR); buildDir.mkdirs(); for (String l : lines) { + // support 3rd party maven repositories + File grapeConfig = new File(".camel-jbang/camelGrapeConfig.xml"); + if (grapeConfig.exists()) { + System.setProperty("grape.config", grapeConfig.getAbsolutePath()); + } + if (l.startsWith("dependency=")) { l = StringHelper.after(l, "dependency="); MavenGav gav = MavenGav.parseGav(null, l); diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java index 62ef40adeb6..1398909bab1 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/DownloaderHelper.java @@ -16,6 +16,8 @@ */ package org.apache.camel.main; +import java.io.File; +import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.util.HashMap; @@ -23,6 +25,7 @@ import java.util.Map; import groovy.grape.Grape; import org.apache.camel.CamelContext; +import org.apache.camel.util.IOHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -108,4 +111,34 @@ public final class DownloaderHelper { return false; } + public static void prepareDownloader(CamelContext camelContext, String repos) throws Exception { + InputStream is = DownloaderHelper.class.getResourceAsStream("/camelGrapeConfig.xml"); + if (is != null) { + String xml = IOHelper.loadText(is); + if (repos != null) { + StringBuilder sb = new StringBuilder(); + sb.append(" <!-- custom repositories -->"); + int i = 0; + for (String repo : repos.split(",")) { + i++; + sb.append(String.format("\n <url name=\"repo%s\" m2compatible=\"true\">", i)); + sb.append(String.format( + "\n <artifact pattern=\"%s/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]\"/>", + repo)); + sb.append(String.format("\n </url>")); + } + xml = xml.replace(" <!-- @repos@ -->", sb.toString()); + } + + // save file to local disk and point grape to use this + File out = new File(".camel-jbang/camelGrapeConfig.xml"); + IOHelper.writeText(xml, out); + + // Grape should use our custom configuration file + System.setProperty("grape.config", out.getAbsolutePath()); + + IOHelper.close(is); + } + } + } diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java index b695a58c7f4..0da80cffeba 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java @@ -39,6 +39,7 @@ public class KameletMain extends MainCommandLineSupport { protected final MainRegistry registry = new MainRegistry(); private boolean download = true; + private String repos; private boolean stub; private DownloadListener downloadListener; private GroovyClassLoader groovyClassLoader; @@ -117,6 +118,17 @@ public class KameletMain extends MainCommandLineSupport { this.download = download; } + public String getRepos() { + return repos; + } + + /** + * Additional maven repositories for download on-demand (Use commas to separate multiple repositories). + */ + public void setRepos(String repos) { + this.repos = repos; + } + public boolean isStub() { return stub; } @@ -167,6 +179,16 @@ public class KameletMain extends MainCommandLineSupport { } } }); + addOption(new ParameterOption( + "repos", "repositories", "Additional maven repositories for download on-demand.", + "repos") { + @Override + protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) { + if (arg.equals("-repos")) { + setRepos(parameter); + } + } + }); } @Override @@ -278,6 +300,9 @@ public class KameletMain extends MainCommandLineSupport { if (download) { // use resolvers that can auto downloaded try { + // prepare grape config with custom repositories + DownloaderHelper.prepareDownloader(camelContext, repos); + // dependencies from CLI Object dependencies = getInitialProperties().get("camel.jbang.dependencies"); if (dependencies != null) { diff --git a/dsl/camel-kamelet-main/src/main/resources/camelGrapeConfig.xml b/dsl/camel-kamelet-main/src/main/resources/camelGrapeConfig.xml new file mode 100644 index 00000000000..2aa4400ed84 --- /dev/null +++ b/dsl/camel-kamelet-main/src/main/resources/camelGrapeConfig.xml @@ -0,0 +1,40 @@ +<!-- + + 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. + +--> +<ivysettings> + <!-- disable download of source and javadoc to be faster --> + <property name="ivy.maven.lookup.sources" value="false"/> + <property name="ivy.maven.lookup.javadoc" value="false"/> + <settings defaultResolver="downloadGrapes"/> + <resolvers> + <chain name="downloadGrapes" returnFirst="true"> + <!-- grapes --> + <filesystem name="cachedGrapes"> + <ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/> + <artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision](-[classifier]).[ext]"/> + </filesystem> + <!-- local m2 --> + <ibiblio name="localm2" root="${user.home.url}/.m2/repository/" checkmodified="true" changingPattern=".*" changingMatcher="regexp" m2compatible="true"/> + <!-- maven central --> + <ibiblio name="maven2" m2compatible="true"/> + <!-- @repos@ --> + </chain> + </resolvers> +</ivysettings>