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
The following commit(s) were added to refs/heads/main by this push: new bd79f14f815 CAMEL-18983: camel-jbang - Automatic detect atlassian maven repo when using camel-jira bd79f14f815 is described below commit bd79f14f815d109f8776f4f9f7ef98d4c88ebd1f Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Mar 6 08:57:50 2023 +0100 CAMEL-18983: camel-jbang - Automatic detect atlassian maven repo when using camel-jira --- .../java/org/apache/camel/main/KameletMain.java | 4 ++ .../camel/main/download/DependencyDownloader.java | 10 ++++ .../camel/main/download/KnownReposResolver.java | 64 ++++++++++++++++++++++ .../main/download/MavenDependencyDownloader.java | 59 ++++++++++++++++++-- .../resources/camel-main-known-repos.properties | 18 ++++++ 5 files changed, 151 insertions(+), 4 deletions(-) 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 bdd6aafdf68..0ec69e2d310 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 @@ -48,6 +48,7 @@ import org.apache.camel.main.download.DownloadListener; import org.apache.camel.main.download.DownloadModelineParser; import org.apache.camel.main.download.KameletMainInjector; import org.apache.camel.main.download.KnownDependenciesResolver; +import org.apache.camel.main.download.KnownReposResolver; import org.apache.camel.main.download.MavenDependencyDownloader; import org.apache.camel.main.download.TypeConverterLoaderDownloadListener; import org.apache.camel.main.http.VertxHttpServer; @@ -307,7 +308,10 @@ public class KameletMain extends MainCommandLineSupport { answer.getCamelContextExtension().getPackageScanClassResolver().addClassLoader(dynamicCL); answer.getCamelContextExtension().getPackageScanResourceResolver().addClassLoader(dynamicCL); + KnownReposResolver known = new KnownReposResolver(camelContext); + known.loadKnownDependencies(); MavenDependencyDownloader downloader = new MavenDependencyDownloader(); + downloader.setKnownReposResolver(known); downloader.setClassLoader(dynamicCL); downloader.setCamelContext(answer); downloader.setRepos(repos); diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloader.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloader.java index cce2e991f6e..1ebf173e5a3 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloader.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloader.java @@ -81,6 +81,16 @@ public interface DependencyDownloader extends CamelContextAware, StaticService { */ void downloadDependency(String groupId, String artifactId, String version); + /** + * Downloads the dependency incl transitive dependencies + * + * @param groupId maven group id + * @param artifactId maven artifact id + * @param version maven version + * @param extraRepos additional remote maven repositories to use when downloading + */ + void downloadDependency(String groupId, String artifactId, String version, String extraRepos); + /** * Downloads the dependency * diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/KnownReposResolver.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/KnownReposResolver.java new file mode 100644 index 00000000000..fc5626010f1 --- /dev/null +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/KnownReposResolver.java @@ -0,0 +1,64 @@ +/* + * 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.main.download; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import org.apache.camel.CamelContext; + +public final class KnownReposResolver { + + private final Map<String, String> repos = new HashMap<>(); + private final CamelContext camelContext; + + public KnownReposResolver(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public void loadKnownDependencies() { + doLoadKnownRepos("/camel-main-known-repos.properties"); + } + + private void doLoadKnownRepos(String name) { + try { + InputStream is = getClass().getResourceAsStream(name); + if (is != null) { + Properties prop = new Properties(); + prop.load(is); + Map<String, String> map = new HashMap<>(); + for (String key : prop.stringPropertyNames()) { + String value = prop.getProperty(key); + map.put(key, value); + } + addRepos(map); + } + } catch (Throwable e) { + // ignore + } + } + + public void addRepos(Map<String, String> repos) { + this.repos.putAll(repos); + } + + public String getRepo(String key) { + return repos.get(key); + } +} diff --git a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/MavenDependencyDownloader.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/MavenDependencyDownloader.java index eb2ab800213..d3941a51a02 100644 --- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/MavenDependencyDownloader.java +++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/MavenDependencyDownloader.java @@ -220,6 +220,7 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende private static final RepositoryPolicy POLICY_DISABLED = new RepositoryPolicy( false, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE); + int customCount = 1; private String[] bootClasspath; private DownloadThreadPool threadPool; private DIRegistry registry; @@ -227,6 +228,7 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende private CamelContext camelContext; private final Set<DownloadListener> downloadListeners = new LinkedHashSet<>(); private final Set<ArtifactDownloadListener> artifactDownloadListeners = new LinkedHashSet<>(); + private KnownReposResolver knownReposResolver; // repository URLs set from "camel.jbang.repos" property or --repos option. private String repos; @@ -259,6 +261,14 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende this.classLoader = classLoader; } + public KnownReposResolver getKnownReposResolver() { + return knownReposResolver; + } + + public void setKnownReposResolver(KnownReposResolver knownReposResolver) { + this.knownReposResolver = knownReposResolver; + } + @Override public void addDownloadListener(DownloadListener downloadListener) { CamelContextAware.trySetCamelContext(downloadListener, getCamelContext()); @@ -316,19 +326,24 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende downloadDependency(groupId, artifactId, version, true); } + @Override + public void downloadDependency(String groupId, String artifactId, String version, String extraRepos) { + doDownloadDependency(groupId, artifactId, version, true, false, extraRepos); + } + @Override public void downloadHiddenDependency(String groupId, String artifactId, String version) { - doDownloadDependency(groupId, artifactId, version, true, true); + doDownloadDependency(groupId, artifactId, version, true, true, null); } @Override public void downloadDependency(String groupId, String artifactId, String version, boolean transitively) { - doDownloadDependency(groupId, artifactId, version, transitively, false); + doDownloadDependency(groupId, artifactId, version, transitively, false, null); } protected void doDownloadDependency( String groupId, String artifactId, String version, boolean transitively, - boolean hidden) { + boolean hidden, String extraRepos) { if (!hidden) { // trigger listener @@ -367,6 +382,11 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende if (!apacheSnapshotsIncluded && "org.apache.camel".equals(groupId) && version.contains("SNAPSHOT")) { repositories.add(apacheSnapshots); } + // include extra repositories (if any) + repositories.addAll(resolveExtraRepositories(extraRepos)); + // and from known extra repositories (if any) + String known = knownReposResolver.getRepo(artifactId); + repositories.addAll(resolveExtraRepositories(known)); List<MavenArtifact> artifacts = resolveDependenciesViaAether(deps, repositories, transitively); List<File> files = new ArrayList<>(); @@ -493,6 +513,38 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende } } + private List<RemoteRepository> resolveExtraRepositories(String extraRepos) { + List<RemoteRepository> repositories = new ArrayList<>(); + if (extraRepos != null) { + Set<URL> repositoryURLs = new HashSet<>(); + for (String repo : extraRepos.split(",")) { + try { + URL url = new URL(repo); + if (url.getHost().equals("repo1.maven.org")) { + continue; + } + String id = "custom" + customCount++; + RepositoryPolicy releasePolicy = fresh ? POLICY_FRESH : POLICY_DEFAULT; + if (repositoryURLs.add(url)) { + if (url.getHost().equals("repository.apache.org") && url.getPath().contains("/snapshots")) { + apacheSnapshotsIncluded = true; + repositories.add(apacheSnapshots); + } else { + // both snapshots and releases allowed for custom repos + repositories.add(new RemoteRepository.Builder(id, "default", repo) + .setReleasePolicy(releasePolicy) + .setSnapshotPolicy(fresh ? POLICY_FRESH : POLICY_DEFAULT) + .build()); + } + } + } catch (MalformedURLException e) { + LOG.warn("Can't use {} URL: {}. Skipping.", repo, e.getMessage(), e); + } + } + } + return repositories; + } + @Override protected void doBuild() throws Exception { if (classLoader == null && camelContext != null) { @@ -1117,7 +1169,6 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende .build(); // and custom repos and remember URLs to not duplicate the repositories from the settings - int customCount = 1; if (repos != null) { for (String repo : repos.split(",")) { try { diff --git a/dsl/camel-kamelet-main/src/main/resources/camel-main-known-repos.properties b/dsl/camel-kamelet-main/src/main/resources/camel-main-known-repos.properties new file mode 100644 index 00000000000..3f3ed62a496 --- /dev/null +++ b/dsl/camel-kamelet-main/src/main/resources/camel-main-known-repos.properties @@ -0,0 +1,18 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +camel-jira = https://packages.atlassian.com/maven-external \ No newline at end of file