use of org.apache.maven.settings.RepositoryPolicy 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;
}
Aggregations