Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Central #13420

Merged
merged 3 commits into from Mar 8, 2024
Merged

Central #13420

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/user-manual/modules/ROOT/pages/camel-jbang.adoc
Expand Up @@ -3394,6 +3394,12 @@ The follow options related to _exporting_, can be configured in `application.pro
|`camel.jbang.mavenSettingsSecurity`
|Optional location of Maven settings-security.xml file to decrypt settings.xml

|`camel.jbang.mavenCentralEnabled`
|Whether downloading JARs from Maven Central repository is enabled

|`camel.jbang.mavenApacheSnapshotEnabled`
|Whether downloading JARs from ASF Maven Snapshot repository is enabled

|`camel.jbang.exportDir`
|Directory where the project will be exported

Expand Down
Expand Up @@ -66,6 +66,10 @@ protected Integer export() throws Exception {
this.repos = prop.getProperty("camel.jbang.repos", this.repos);
this.mavenSettings = prop.getProperty("camel.jbang.maven-settings", this.mavenSettings);
this.mavenSettingsSecurity = prop.getProperty("camel.jbang.maven-settings-security", this.mavenSettingsSecurity);
this.mavenCentralEnabled = "true"
.equals(prop.getProperty("camel.jbang.maven-central-enabled", mavenCentralEnabled ? "true" : "false"));
this.mavenApacheSnapshotEnabled = "true".equals(prop.getProperty("camel.jbang.maven-apache-snapshot-enabled",
mavenApacheSnapshotEnabled ? "true" : "false"));
this.exclude = prop.getProperty("camel.jbang.exclude", this.exclude);
}

Expand Down Expand Up @@ -100,6 +104,8 @@ protected Integer export(ExportBaseCommand cmd) throws Exception {
cmd.gav = this.gav;
cmd.mavenSettings = this.mavenSettings;
cmd.mavenSettingsSecurity = this.mavenSettingsSecurity;
cmd.mavenCentralEnabled = this.mavenCentralEnabled;
cmd.mavenApacheSnapshotEnabled = this.mavenApacheSnapshotEnabled;
cmd.exportDir = this.exportDir;
cmd.fresh = this.fresh;
cmd.download = this.download;
Expand Down
Expand Up @@ -119,6 +119,14 @@ abstract class ExportBaseCommand extends CamelCommand {
description = "Optional location of Maven settings-security.xml file to decrypt settings.xml")
String mavenSettingsSecurity;

@CommandLine.Option(names = { "--maven-central-enabled" },
description = "Whether downloading JARs from Maven Central repository is enabled")
boolean mavenCentralEnabled = true;

@CommandLine.Option(names = { "--maven-apache-snapshot-enabled" },
description = "Whether downloading JARs from ASF Maven Snapshot repository is enabled")
boolean mavenApacheSnapshotEnabled = true;

@CommandLine.Option(names = { "--main-classname" },
description = "The class name of the Camel Main application class",
defaultValue = "CamelApplication")
Expand Down
Expand Up @@ -167,6 +167,14 @@ public class Run extends CamelCommand {
description = "Optional location of Maven settings-security.xml file to decrypt settings.xml")
String mavenSettingsSecurity;

@Option(names = { "--maven-central-enabled" },
description = "Whether downloading JARs from Maven Central repository is enabled")
boolean mavenCentralEnabled = true;

@Option(names = { "--maven-apache-snapshot-enabled" },
description = "Whether downloading JARs from ASF Maven Snapshot repository is enabled")
boolean mavenApacheSnapshotEnabled = true;

@Option(names = { "--fresh" }, description = "Make sure we use fresh (i.e. non-cached) resources")
boolean fresh;

Expand Down Expand Up @@ -441,6 +449,8 @@ private int run() throws Exception {
main.setFresh(fresh);
main.setMavenSettings(mavenSettings);
main.setMavenSettingsSecurity(mavenSettingsSecurity);
main.setMavenCentralEnabled(mavenCentralEnabled);
main.setMavenApacheSnapshotEnabled(mavenApacheSnapshotEnabled);
main.setDownloadListener(new RunDownloadListener());
main.setAppName("Apache Camel (JBang)");

Expand Down Expand Up @@ -858,6 +868,10 @@ private Properties loadProfileProperties() throws Exception {
repos = answer.getProperty("camel.jbang.repos", repos);
mavenSettings = answer.getProperty("camel.jbang.maven-settings", mavenSettings);
mavenSettingsSecurity = answer.getProperty("camel.jbang.maven-settings-security", mavenSettingsSecurity);
mavenCentralEnabled = "true"
.equals(answer.getProperty("camel.jbang.maven-central-enabled", mavenCentralEnabled ? "true" : "false"));
mavenApacheSnapshotEnabled = "true".equals(answer.getProperty("camel.jbang.maven-apache-snapshot-enabled",
mavenApacheSnapshotEnabled ? "true" : "false"));
openapi = answer.getProperty("camel.jbang.open-api", openapi);
download = "true".equals(answer.getProperty("camel.jbang.download", download ? "true" : "false"));
background = "true".equals(answer.getProperty("camel.jbang.background", background ? "true" : "false"));
Expand Down
Expand Up @@ -110,6 +110,8 @@ public class KameletMain extends MainCommandLineSupport {
private boolean verbose;
private String mavenSettings;
private String mavenSettingsSecurity;
boolean mavenCentralEnabled = true;
boolean mavenApacheSnapshotEnabled = true;
private String stubPattern;
private boolean silent;
private DownloadListener downloadListener;
Expand Down Expand Up @@ -267,6 +269,28 @@ public DownloadListener getDownloadListener() {
return downloadListener;
}

public boolean isMavenCentralEnabled() {
return mavenCentralEnabled;
}

/**
* Whether downloading JARs from Maven Central repository is enabled
*/
public void setMavenCentralEnabled(boolean mavenCentralEnabled) {
this.mavenCentralEnabled = mavenCentralEnabled;
}

public boolean isMavenApacheSnapshotEnabled() {
return mavenApacheSnapshotEnabled;
}

/**
* Whether downloading JARs from ASF Maven Snapshot repository is enabled
*/
public void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled) {
this.mavenApacheSnapshotEnabled = mavenApacheSnapshotEnabled;
}

/**
* Sets a custom download listener
*/
Expand Down Expand Up @@ -384,6 +408,8 @@ protected CamelContext createCamelContext() {
downloader.setFresh(fresh);
downloader.setMavenSettings(mavenSettings);
downloader.setMavenSettingsSecurity(mavenSettingsSecurity);
downloader.setMavenCentralEnabled(mavenCentralEnabled);
downloader.setMavenApacheSnapshotEnabled(mavenApacheSnapshotEnabled);
if (downloadListener != null) {
downloader.addDownloadListener(downloadListener);
}
Expand Down
Expand Up @@ -87,6 +87,26 @@ public interface DependencyDownloader extends CamelContextAware, StaticService {
*/
void setMavenSettingsSecurity(String mavenSettingsSecurity);

/**
* Whether downloading JARs from Maven Central repository is enabled
*/
boolean isMavenCentralEnabled();

/**
* Whether downloading JARs from Maven Central repository is enabled
*/
void setMavenCentralEnabled(boolean mavenCentralEnabled);

/**
* Whether downloading JARs from ASF Maven Snapshot repository is enabled
*/
boolean isMavenApacheSnapshotEnabled();

/**
* Whether downloading JARs from ASF Maven Snapshot repository is enabled
*/
void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled);

/**
* Downloads the dependency incl transitive dependencies
*
Expand Down
Expand Up @@ -83,6 +83,9 @@ public class MavenDependencyDownloader extends ServiceSupport implements Depende
// settings.xml and settings-security.xml locations to be passed to MavenDownloader from camel-tooling-maven
private String mavenSettings;
private String mavenSettingsSecurity;
// to make it easy to turn off maven central/snapshot
boolean mavenCentralEnabled = true;
boolean mavenApacheSnapshotEnabled = true;

@Override
public CamelContext getCamelContext() {
Expand Down Expand Up @@ -187,6 +190,26 @@ public void setMavenSettingsSecurity(String mavenSettingsSecurity) {
this.mavenSettingsSecurity = mavenSettingsSecurity;
}

@Override
public boolean isMavenCentralEnabled() {
return mavenCentralEnabled;
}

@Override
public void setMavenCentralEnabled(boolean mavenCentralEnabled) {
this.mavenCentralEnabled = mavenCentralEnabled;
}

@Override
public boolean isMavenApacheSnapshotEnabled() {
return mavenApacheSnapshotEnabled;
}

@Override
public void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled) {
this.mavenApacheSnapshotEnabled = mavenApacheSnapshotEnabled;
}

@Override
public void downloadDependency(String groupId, String artifactId, String version) {
downloadDependency(groupId, artifactId, version, true);
Expand Down Expand Up @@ -243,7 +266,7 @@ protected void doDownloadDependency(
List<String> deps = List.of(gav);

// include Apache snapshot to make it easy to use upcoming releases
boolean useApacheSnaphots = "org.apache.camel".equals(groupId) && version.contains("SNAPSHOT");
boolean useApacheSnapshots = "org.apache.camel".equals(groupId) && version.contains("SNAPSHOT");

// include extra repositories (if any) - these will be used in addition
// to the ones detected from ~/.m2/settings.xml and configured in
Expand All @@ -256,7 +279,7 @@ protected void doDownloadDependency(
}

List<MavenArtifact> artifacts = resolveDependenciesViaAether(deps, extraRepositories,
transitively, useApacheSnaphots);
transitively, useApacheSnapshots);
List<File> files = new ArrayList<>();
if (verbose) {
LOG.info("Resolved: {} -> [{}]", gav, artifacts);
Expand Down Expand Up @@ -480,6 +503,8 @@ protected void doBuild() {
MavenDownloaderImpl mavenDownloaderImpl = new MavenDownloaderImpl();
mavenDownloaderImpl.setMavenSettingsLocation(mavenSettings);
mavenDownloaderImpl.setMavenSettingsSecurityLocation(mavenSettingsSecurity);
mavenDownloaderImpl.setMavenCentralEnabled(mavenCentralEnabled);
mavenDownloaderImpl.setMavenApacheSnapshotEnabled(mavenApacheSnapshotEnabled);
mavenDownloaderImpl.setRepos(repos);
mavenDownloaderImpl.setFresh(fresh);
mavenDownloaderImpl.setOffline(!download);
Expand Down Expand Up @@ -534,7 +559,7 @@ public List<MavenArtifact> resolveDependenciesViaAether(
try {
return mavenDownloader.resolveArtifacts(depIds, extraRepositories, transitively, useApacheSnapshots);
} catch (MavenResolutionException e) {
String repos = e.getRepositories() == null
String repos = (e.getRepositories() == null || e.getRepositories().isEmpty())
? "(empty URL list)"
: String.join(", ", e.getRepositories());
String msg = "Cannot resolve dependencies in " + repos;
Expand Down
Expand Up @@ -101,4 +101,28 @@ List<MavenGav> resolveAvailableVersions(String groupId, String artifactId, Strin
*/
void setRepositoryResolver(RepositoryResolver repositoryResolver);

/**
* Sets whether maven central repository should be included and as first in the list of repositories. This can be
* used to turn of maven central for users that may use their own maven proxy.
*/
void setMavenCentralEnabled(boolean mavenCentralEnabled);

/**
* Sets whether maven central repository should be included and as first in the list of repositories. This can be
* used to turn of maven central for users that may use their own maven proxy.
*/
boolean isMavenCentralEnabled();

/**
* Sets whether using SNAPSHOT versions of Apache Camel is enabled. If enabled then SNAPSHOT can be downloaded from
* the ASF SNAPSHOT maven repository.
*/
void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled);

/**
* Sets whether using SNAPSHOT versions of Apache Camel is enabled. If enabled then SNAPSHOT can be downloaded from
* the ASF SNAPSHOT maven repository.
*/
boolean isMavenApacheSnapshotEnabled();

}
Expand Up @@ -233,6 +233,9 @@ public class MavenDownloaderImpl extends ServiceSupport implements MavenDownload
private static final RepositoryPolicy POLICY_DISABLED = new RepositoryPolicy(
false, RepositoryPolicy.UPDATE_POLICY_NEVER, RepositoryPolicy.CHECKSUM_POLICY_IGNORE);

private boolean mavenCentralEnabled = true;
private boolean mavenApacheSnapshotEnabled = true;

private RepositoryResolver repositoryResolver;

private RepositorySystem repositorySystem;
Expand Down Expand Up @@ -326,9 +329,19 @@ protected void doBuild() {
originalRepositories));

// mirroring/proxying Maven Central
centralResolutionRepository = remoteRepositories.get(0);
if (centralRepository == null && !remoteRepositories.isEmpty()) {
for (RemoteRepository repo : remoteRepositories) {
if ("central".equals(repo.getId())) {
centralRepository = repo;
break;
} else if (repo.getHost().startsWith("repo1.maven.org") || repo.getHost().startsWith("repo2.maven.org")) {
centralRepository = repo;
break;
}
}
}

if (!apacheSnapshotsIncluded) {
if (mavenApacheSnapshotEnabled && !apacheSnapshotsIncluded) {
// process apache snapshots even if it's not present in remoteRepositories, because it
// may be used on demand for each download/resolution request
apacheSnapshotsResolutionRepository = repositorySystem.newResolutionRepositories(repositorySystemSession,
Expand Down Expand Up @@ -371,7 +384,7 @@ public List<MavenArtifact> resolveArtifacts(
repositories.addAll(repositorySystem.newResolutionRepositories(repositorySystemSession,
extraRemoteRepositories));
}
if (useApacheSnapshots && !apacheSnapshotsIncluded) {
if (mavenApacheSnapshotEnabled && useApacheSnapshots && !apacheSnapshotsIncluded) {
repositories.add(apacheSnapshotsResolutionRepository);
}

Expand Down Expand Up @@ -1171,18 +1184,22 @@ List<RemoteRepository> configureDefaultRepositories(Settings settings) {
// a set to prevent duplicates, but do not store URLs directly (hashCode() may lead to DNS resolution!)
Set<String> repositoryURLs = new HashSet<>();

// add maven central first - always
centralRepository = new RemoteRepository.Builder("central", "default", MAVEN_CENTRAL_REPO)
.setReleasePolicy(defaultPolicy)
.setSnapshotPolicy(POLICY_DISABLED)
.build();
repositories.add(centralRepository);
if (mavenCentralEnabled) {
// add maven central first - always
centralRepository = new RemoteRepository.Builder("central", "default", MAVEN_CENTRAL_REPO)
.setReleasePolicy(defaultPolicy)
.setSnapshotPolicy(POLICY_DISABLED)
.build();
repositories.add(centralRepository);
}

// configure Apache snapshots - to be used if needed
apacheSnapshotsRepository = new RemoteRepository.Builder("apache-snapshot", "default", APACHE_SNAPSHOT_REPO)
.setReleasePolicy(POLICY_DISABLED)
.setSnapshotPolicy(defaultPolicy)
.build();
if (mavenApacheSnapshotEnabled) {
// configure Apache snapshots - to be used if needed
apacheSnapshotsRepository = new RemoteRepository.Builder("apache-snapshot", "default", APACHE_SNAPSHOT_REPO)
.setReleasePolicy(POLICY_DISABLED)
.setSnapshotPolicy(defaultPolicy)
.build();
}

// and custom repos and remember URLs to not duplicate the repositories from the settings
if (repos != null) {
Expand All @@ -1206,7 +1223,8 @@ List<RemoteRepository> configureDefaultRepositories(Settings settings) {
try {
URL url = new URL(r.getUrl());
if (repositoryURLs.add(r.getUrl())) {
if (url.getHost().equals("repository.apache.org") && url.getPath().startsWith("/snapshots")) {
if (mavenApacheSnapshotEnabled && url.getHost().equals("repository.apache.org")
&& url.getPath().startsWith("/snapshots")) {
// record that Apache Snapshots repository is included in default (always used)
// repositories and used preconfigured instance of o.e.aether.repository.RemoteRepository
apacheSnapshotsIncluded = true;
Expand Down Expand Up @@ -1252,19 +1270,20 @@ List<RemoteRepository> configureDefaultRepositories(Settings settings) {

/**
* Helper method to translate a collection of Strings for remote repository URLs into actual instances of
* {@link RemoteRepository} added to the passed {@code repositories}. We don't detected duplicates here and we don't
* {@link RemoteRepository} added to the passed {@code repositories}. We don't detect duplicates here, and we don't
* do mirror/proxy processing of the repositories.
*/
private void configureRepositories(List<RemoteRepository> repositories, Set<String> urls) {
urls.forEach(repo -> {
try {
repo = repositoryResolver.resolveRepository(repo);
URL url = new URL(repo);
if (url.getHost().equals("repo1.maven.org")) {
if (mavenCentralEnabled && url.getHost().equals("repo1.maven.org")) {
// Maven Central is always used, so skip it
return;
}
if (url.getHost().equals("repository.apache.org") && url.getPath().contains("/snapshots")) {
if (mavenApacheSnapshotEnabled && url.getHost().equals("repository.apache.org")
&& url.getPath().contains("/snapshots")) {
// Apache Snapshots added, so we'll use our own definition of this repository
repositories.add(apacheSnapshotsRepository);
} else {
Expand Down Expand Up @@ -1316,6 +1335,26 @@ public void setOffline(boolean offline) {
this.offline = offline;
}

@Override
public boolean isMavenCentralEnabled() {
return mavenCentralEnabled;
}

@Override
public void setMavenCentralEnabled(boolean mavenCentralEnabled) {
this.mavenCentralEnabled = mavenCentralEnabled;
}

@Override
public boolean isMavenApacheSnapshotEnabled() {
return mavenApacheSnapshotEnabled;
}

@Override
public void setMavenApacheSnapshotEnabled(boolean mavenApacheSnapshotEnabled) {
this.mavenApacheSnapshotEnabled = mavenApacheSnapshotEnabled;
}

private static class AcceptAllDependencyFilter implements DependencyFilter {
@Override
public boolean accept(DependencyNode node, List<DependencyNode> parents) {
Expand Down