use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.
the class MavenConfigurationSupport method repositories.
/**
* Returns list of configured remote (<code>remote=true</code>) or default (<code>remote=false</code>)
* repositories.
* @param remote
* @return
*/
protected MavenRepositoryURL[] repositories(Dictionary<String, Object> config, boolean remote) throws Exception {
String property = remote ? PID + "." + PROPERTY_REPOSITORIES : PID + "." + PROPERTY_DEFAULT_REPOSITORIES;
String[] repositories = listOfValues((String) config.get(property));
if (remote) {
if (repositories.length == 0 || repositories[0].charAt(0) == '+') {
if (repositories.length > 0) {
repositories[0] = repositories[0].substring(1);
}
List<String> newRepositories = new LinkedList<>(Arrays.asList(repositories));
// append all repositories from all active profiles from available settings.xml
if (mavenSettings != null) {
// see org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.getRepositories()
Set<String> activeProfiles = new LinkedHashSet<>(mavenSettings.getActiveProfiles());
Map<String, Profile> profiles = mavenSettings.getProfilesAsMap();
profiles.values().stream().filter((profile) -> profile.getActivation() != null && profile.getActivation().isActiveByDefault()).map(Profile::getId).forEach(activeProfiles::add);
for (String activeProfile : activeProfiles) {
Profile profile = profiles.get(activeProfile);
if (profile == null) {
continue;
}
for (Repository repo : profile.getRepositories()) {
StringBuilder builder = new StringBuilder();
builder.append(repo.getUrl());
builder.append("@id=").append(repo.getId());
builder.append("@_from=").append(MavenRepositoryURL.FROM.SETTINGS);
if (repo.getReleases() != null) {
if (!repo.getReleases().isEnabled()) {
builder.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_DISALLOW_RELEASES);
}
SourceAnd<String> up = updatePolicy(repo.getReleases().getUpdatePolicy());
addPolicy(builder, "".equals(up.val()) ? "never" : up.val(), ServiceConstants.OPTION_RELEASES_UPDATE);
// not used in pax-url-aether
// addPolicy(builder, repo.getReleases().getChecksumPolicy(), "releasesChecksum");
}
if (repo.getSnapshots() != null) {
if (repo.getSnapshots().isEnabled()) {
builder.append(ServiceConstants.SEPARATOR_OPTIONS).append(ServiceConstants.OPTION_ALLOW_SNAPSHOTS);
}
SourceAnd<String> up = updatePolicy(repo.getSnapshots().getUpdatePolicy());
addPolicy(builder, "".equals(up.val()) ? "never" : up.val(), ServiceConstants.OPTION_SNAPSHOTS_UPDATE);
// not used in pax-url-aether
// addPolicy(builder, repo.getSnapshots().getChecksumPolicy(), "snapshotsChecksum");
}
newRepositories.add(builder.toString());
}
}
}
repositories = newRepositories.toArray(new String[newRepositories.size()]);
}
}
List<MavenRepositoryURL> result = new LinkedList<>();
for (String repo : repositories) {
result.add(new MavenRepositoryURL(repo));
}
return result.toArray(new MavenRepositoryURL[result.size()]);
}
use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.
the class RepositoryChangeCommand method edit.
@Override
protected void edit(String prefix, Dictionary<String, Object> config, MavenRepositoryURL[] allRepos, MavenRepositoryURL[] pidRepos, MavenRepositoryURL[] settingsRepos) throws Exception {
Optional<MavenRepositoryURL> first = Arrays.stream(allRepos).filter((repo) -> id.equals(repo.getId())).findFirst();
if (!first.isPresent()) {
System.err.printf("Can't find %s repository with ID \"%s\"\n", (defaultRepository ? "default" : "remote"), id);
return;
}
MavenRepositoryURL changedRepository = first.get();
changedRepository.setSnapshotsEnabled(snapshots);
changedRepository.setReleasesEnabled(!noReleases);
if (updatePolicy != null) {
SourceAnd<String> up = updatePolicy(updatePolicy);
if (!up.valid) {
System.err.println("Unknown value of update policy: \"" + updatePolicy + "\"");
return;
}
changedRepository.setReleasesUpdatePolicy(up.val());
changedRepository.setSnapshotsUpdatePolicy(up.val());
}
if (checksumPolicy != null) {
SourceAnd<String> cp = checksumPolicy(checksumPolicy);
if (!cp.valid) {
System.err.println("Unknown value of checksum policy: \"" + checksumPolicy + "\"");
return;
}
changedRepository.setReleasesChecksumPolicy(cp.val());
changedRepository.setSnapshotsChecksumPolicy(cp.val());
}
if (uri != null) {
SourceAnd<String> urlResolved = validateRepositoryURL(uri, defaultRepository);
if (!urlResolved.valid) {
return;
}
changedRepository.setURL(new URL(urlResolved.val()));
}
boolean hasUsername = username != null && !"".equals(username.trim());
boolean hasPassword = password != null && !"".equals(password.trim());
boolean hasCredentials = hasUsername && hasPassword;
if ((hasUsername && !hasPassword) || (!hasUsername && hasPassword)) {
System.err.println("Please specify both username and password");
return;
}
if (defaultRepository && hasCredentials) {
System.out.println("User credentials won't be used for default repository");
// no return
}
boolean credentialsUpdated = false;
// credentials for remote repository can be stored only in settings.xml
if (!defaultRepository && hasCredentials) {
if (!updateCredentials(force, id, username, password, prefix, config)) {
return;
}
credentialsUpdated = true;
}
if (!defaultRepository && changedRepository.getFrom() == MavenRepositoryURL.FROM.SETTINGS) {
// find <repository> in any active profile to change it
for (Profile profile : mavenSettings.getProfiles()) {
Optional<Repository> repository = profile.getRepositories().stream().filter((r) -> id.equals(r.getId())).findFirst();
if (repository.isPresent()) {
// I can't imagine it's not...
Repository r = repository.get();
r.setUrl(changedRepository.getURL().toString());
if (!changedRepository.isSnapshotsEnabled()) {
r.setSnapshots(new RepositoryPolicy());
r.getSnapshots().setEnabled(false);
} else {
RepositoryPolicy rp = r.getSnapshots() == null ? new RepositoryPolicy() : r.getSnapshots();
rp.setEnabled(true);
if (checksumPolicy != null) {
rp.setChecksumPolicy(changedRepository.getSnapshotsChecksumPolicy());
} else if (rp.getChecksumPolicy() == null) {
rp.setChecksumPolicy("warn");
}
if (updatePolicy != null) {
rp.setUpdatePolicy(changedRepository.getSnapshotsUpdatePolicy());
} else if (rp.getUpdatePolicy() == null) {
rp.setUpdatePolicy("daily");
}
r.setSnapshots(rp);
}
if (!changedRepository.isReleasesEnabled()) {
r.setReleases(new RepositoryPolicy());
r.getReleases().setEnabled(false);
} else {
RepositoryPolicy rp = r.getReleases() == null ? new RepositoryPolicy() : r.getReleases();
rp.setEnabled(true);
if (checksumPolicy != null) {
rp.setChecksumPolicy(changedRepository.getReleasesChecksumPolicy());
} else if (rp.getChecksumPolicy() == null) {
rp.setChecksumPolicy("warn");
}
if (updatePolicy != null) {
rp.setUpdatePolicy(changedRepository.getReleasesUpdatePolicy());
} else if (rp.getUpdatePolicy() == null) {
rp.setUpdatePolicy("daily");
}
r.setReleases(rp);
}
updateSettings(prefix, config);
break;
}
}
} else if (changedRepository.getFrom() == MavenRepositoryURL.FROM.PID) {
List<MavenRepositoryURL> newRepos = new LinkedList<>();
for (MavenRepositoryURL repo : pidRepos) {
MavenRepositoryURL _r = repo;
if (id.equals(repo.getId())) {
_r = changedRepository;
}
newRepos.add(_r);
}
updatePidRepositories(prefix, config, defaultRepository, newRepos, settingsRepos.length > 0);
if (credentialsUpdated) {
updateSettings(prefix, config);
}
}
Configuration cmConfig = cm.getConfiguration(PID);
cmConfig.update(config);
success = true;
}
use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.
the class RepositoryListCommand method doAction.
@Override
public void doAction(String prefix, Dictionary<String, Object> config) throws Exception {
ShellTable table = new ShellTable();
table.column("ID");
table.column("URL");
if (verbose) {
table.column("Releases");
table.column("Snapshots");
table.column("Defined in");
}
if (showPasswords) {
table.column("Username");
table.column("Password");
}
System.out.println();
System.out.println("== Remote repositories");
MavenRepositoryURL[] repositories = repositories(config, true);
for (MavenRepositoryURL repoURL : repositories) {
Row row = table.addRow();
row.addContent(repoURL.getId(), repoURL.getURL());
if (verbose) {
row.addContent(repositoryKindInfo(repoURL, false), repositoryKindInfo(repoURL, true), repoURL.getFrom());
}
if (showPasswords) {
if (servers.containsKey(repoURL.getId())) {
Server server = servers.get(repoURL.getId());
row.addContent(server.getUsername() == null ? "" : server.getUsername());
addPasswordInfo(row, serverPasswords, repoURL.getId(), server.getPassword());
} else {
row.addContent("", "");
}
}
}
table.print(System.out);
table = new ShellTable();
table.column("ID");
table.column("URL");
if (verbose) {
table.column("Releases");
table.column("Snapshots");
}
System.out.println();
System.out.println("== Default repositories");
repositories = repositories(config, false);
for (MavenRepositoryURL repoURL : repositories) {
Row row = table.addRow();
row.addContent(repoURL.getId(), repoURL.getURL());
if (verbose) {
row.addContent(repositoryKindInfo(repoURL, false), repositoryKindInfo(repoURL, true), repoURL.getFrom());
}
}
table.print(System.out);
System.out.println();
}
use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.
the class SummaryCommand method doAction.
@Override
protected void doAction(String prefix, Dictionary<String, Object> config) throws Exception {
ShellTable table = new ShellTable();
table.column("Option");
table.column("Value");
if (source) {
table.column("Source");
}
if (description) {
table.column("Description");
}
addRow(table, propertyIds ? PROPERTY_LOCAL_REPOSITORY : "Local repository", localRepository, "Maven repository to store artifacts resolved in *remote repositories*");
addRow(table, propertyIds ? PROPERTY_SETTINGS_FILE : "Settings file", settings, "Settings file that may contain configuration of additional repositories, http proxies and mirrors");
addRow(table, propertyIds ? PROPERTY_SECURITY_FILE : "Security settings file", securitySettings, "Settings file that contain (or relocates to) master Maven password");
if (showPasswords) {
addRow(table, propertyIds ? "<master>" : "Master password", new SourceAnd<>(securitySettings.source, masterPassword), "Master password used to decrypt proxy and server passwords");
}
// for default update/checksum policies specified at repository URI level, see
// org.ops4j.pax.url.mvn.internal.AetherBasedResolver.addRepo()
// see org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer#isUpdatedRequired()
SourceAnd<String> updatePolicy = updatePolicy((String) config.get(prefix + PROPERTY_GLOBAL_UPDATE_POLICY));
addRow(table, propertyIds ? PROPERTY_GLOBAL_UPDATE_POLICY : "Global update policy", updatePolicy, "Overrides update policy specified at repository level (if specified)");
// see org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider#newChecksumPolicy()
SourceAnd<String> checksumPolicy = checksumPolicy((String) config.get(prefix + PROPERTY_GLOBAL_CHECKSUM_POLICY));
addRow(table, propertyIds ? PROPERTY_GLOBAL_CHECKSUM_POLICY : "Global checksum policy", checksumPolicy, "Checksum policy for all repositories");
String updateReleasesProperty = (String) config.get(prefix + PROPERTY_UPDATE_RELEASES);
boolean updateReleases = false;
String sourceInfo = String.format(PATTERN_PID_PROPERTY, PID, prefix + PROPERTY_UPDATE_RELEASES);
if (updateReleasesProperty == null) {
sourceInfo = "Default \"false\"";
} else {
updateReleases = "true".equals(updateReleasesProperty);
}
addRow(table, propertyIds ? PROPERTY_UPDATE_RELEASES : "Update releases", new SourceAnd<>(sourceInfo, updateReleases), "Whether to download non-SNAPSHOT artifacts according to update policy");
// see org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.isValid()
// ANY non null value (even "false"!) means that we require configadmin
String requireConfigAdminProperty = context.getProperty(prefix + REQUIRE_CONFIG_ADMIN_CONFIG);
boolean requireConfigAdmin = requireConfigAdminProperty != null;
sourceInfo = "Default \"false\"";
if (requireConfigAdmin) {
sourceInfo = "BundleContext property (" + prefix + REQUIRE_CONFIG_ADMIN_CONFIG + ")";
}
addRow(table, propertyIds ? REQUIRE_CONFIG_ADMIN_CONFIG : "Require Config Admin", new SourceAnd<>(sourceInfo, requireConfigAdmin), "Whether MavenResolver service is registered ONLY with proper " + PID + " PID configuration");
// see org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.buildSettings()
String useFallbackRepositoriesProperty = (String) config.get(prefix + PROPERTY_USE_FALLBACK_REPOSITORIES);
boolean useFallbackRepositories = Boolean.parseBoolean(useFallbackRepositoriesProperty);
sourceInfo = "Default \"false\"";
if (useFallbackRepositoriesProperty != null) {
sourceInfo = String.format(PATTERN_PID_PROPERTY, PID, prefix + PROPERTY_USE_FALLBACK_REPOSITORIES);
}
addRow(table, propertyIds ? PROPERTY_USE_FALLBACK_REPOSITORIES : "Use fallback repository", new SourceAnd<>(sourceInfo, useFallbackRepositories), "Whether Maven Central is used as implicit, additional remote repository");
// see org.ops4j.pax.url.mvn.internal.config.MavenConfigurationImpl.enableProxy()
// "proxySupport" and "proxies" are not used in "new" MavenResolver
// see org.eclipse.aether.internal.impl.DefaultOfflineController#checkOffline()
String offlineProperty = (String) config.get(prefix + PROPERTY_OFFLINE);
boolean offline = Boolean.parseBoolean(offlineProperty);
sourceInfo = "Default \"false\"";
if (offlineProperty != null) {
sourceInfo = String.format(PATTERN_PID_PROPERTY, PID, prefix + PROPERTY_OFFLINE);
}
addRow(table, propertyIds ? PROPERTY_OFFLINE : "Offline mode", new SourceAnd<>(sourceInfo, offline), "Disables access to external remote repositories (file:// based ones are still used)");
// see org.ops4j.pax.url.mvn.internal.HttpClients.createConnManager()
String certificateCheckProperty = (String) config.get(prefix + PROPERTY_CERTIFICATE_CHECK);
boolean certificateCheck = Boolean.parseBoolean(certificateCheckProperty);
sourceInfo = "Default \"false\"";
if (certificateCheckProperty != null) {
sourceInfo = String.format(PATTERN_PID_PROPERTY, PID, prefix + PROPERTY_CERTIFICATE_CHECK);
}
addRow(table, propertyIds ? PROPERTY_CERTIFICATE_CHECK : "SSL/TLS certificate check", new SourceAnd<>(sourceInfo, certificateCheck), "Turns on server certificate validation for HTTPS remote repositories");
// repositories (short list)
MavenRepositoryURL[] remoteRepositories = repositories(config, true);
boolean first = true;
for (MavenRepositoryURL url : remoteRepositories) {
addRow(table, first ? (propertyIds ? PROPERTY_REPOSITORIES : "Remote repositories") : "", new SourceAnd<>(url.getFrom().getSource(), url.getURL().toString()), first ? "Remote repositories where artifacts are being resolved if not found locally" : "");
first = false;
}
// default repositories (short list)
MavenRepositoryURL[] defaultRepositories = repositories(config, false);
first = true;
for (MavenRepositoryURL url : defaultRepositories) {
addRow(table, first ? (propertyIds ? PROPERTY_DEFAULT_REPOSITORIES : "Default repositories") : "", new SourceAnd<>(url.getFrom().getSource(), url.getURL().toString()), first ? "Repositories where artifacts are looked up before trying remote resolution" : "");
first = false;
}
// proxies (short list)
if (mavenSettings != null && mavenSettings.getProxies() != null) {
first = true;
for (Proxy proxy : mavenSettings.getProxies()) {
String value = String.format("%s:%s", proxy.getHost(), proxy.getPort());
addRow(table, first ? (propertyIds ? "<proxies>" : "HTTP proxies") : "", new SourceAnd<>(MavenRepositoryURL.FROM.SETTINGS.getSource(), value), first ? "Maven HTTP proxies" : "");
first = false;
}
}
System.out.println();
table.print(System.out);
System.out.println();
}
use of org.apache.karaf.maven.core.MavenRepositoryURL in project karaf by apache.
the class RepositoryRemoveCommand method edit.
@Override
protected void edit(String prefix, Dictionary<String, Object> config, MavenRepositoryURL[] allRepos, MavenRepositoryURL[] pidRepos, MavenRepositoryURL[] settingsRepos) throws Exception {
Optional<MavenRepositoryURL> first = Arrays.stream(allRepos).filter((repo) -> id.equals(repo.getId())).findAny();
if (!first.isPresent()) {
System.err.printf("Can't find %s repository with ID \"%s\"\n", (defaultRepository ? "default" : "remote"), id);
return;
}
if (force || confirm(String.format("Are you sure to remove repository with ID \"%s\" for URL %s? (y/N) ", id, first.get().getURL()))) {
if (!defaultRepository && first.get().getFrom() == MavenRepositoryURL.FROM.SETTINGS) {
// remove <server> (credentials) if available
List<Server> newServers = mavenSettings.getServers().stream().filter((s) -> !id.equals(s.getId())).collect(Collectors.toList());
mavenSettings.setServers(newServers);
// find <repository> in any active profile and remove it
for (Profile profile : mavenSettings.getProfiles()) {
if (profile.getRepositories().stream().anyMatch((r) -> id.equals(r.getId()))) {
List<Repository> newRepos = profile.getRepositories().stream().filter((r) -> !id.equals(r.getId())).collect(Collectors.toList());
profile.setRepositories(newRepos);
System.out.printf("Repository with ID \"%s\" was removed from profile \"%s\"\n", id, profile.getId());
break;
}
}
updateSettings(prefix, config);
} else if (first.get().getFrom() == MavenRepositoryURL.FROM.PID) {
List<MavenRepositoryURL> newRepos = Arrays.stream(pidRepos).filter((r) -> !id.equals(r.getId())).collect(Collectors.toList());
updatePidRepositories(prefix, config, defaultRepository, newRepos, settingsRepos.length > 0);
// if there are credentials for this repository, we have to remove them from settings.xml
if (mavenSettings != null && mavenSettings.getServers().stream().anyMatch((s) -> id.equals(s.getId()))) {
// remove <server> (credentials) if available
List<Server> newServers = mavenSettings.getServers().stream().filter((s) -> !id.equals(s.getId())).collect(Collectors.toList());
mavenSettings.setServers(newServers);
updateSettings(prefix, config);
}
}
Configuration cmConfig = cm.getConfiguration(PID);
cmConfig.update(config);
success = true;
}
}
Aggregations