use of org.commonjava.maven.ext.core.state.RESTState in project pom-manipulation-ext by release-engineering.
the class DependencyManipulator method loadRemoteOverrides.
/**
* This will load the remote overrides. It will first try to load any overrides that might have
* been prepopulated by the REST scanner, failing that it will load from a remote POM file.
*
* @return the loaded overrides
* @throws ManipulationException if an error occurs.
*/
private Map<ArtifactRef, String> loadRemoteOverrides() throws ManipulationException {
final DependencyState depState = session.getState(DependencyState.class);
final RESTState restState = session.getState(RESTState.class);
final List<ProjectVersionRef> gavs = depState.getRemoteBOMDepMgmt();
// While in theory we are only mapping ProjectRef -> NewVersion if we store key as ProjectRef we can't then have
// org.foo:foobar -> 1.2.0.redhat-2
// org.foo:foobar -> 2.0.0.redhat-2
// Which is useful for strictAlignment scenarios (although undefined for non-strict).
Map<ArtifactRef, String> restOverrides = depState.getRemoteRESTOverrides();
Map<ArtifactRef, String> bomOverrides = new LinkedHashMap<>();
Map<ArtifactRef, String> mergedOverrides = new LinkedHashMap<>();
if (gavs != null) {
final ListIterator<ProjectVersionRef> iter = gavs.listIterator(gavs.size());
// Iterate in reverse order so that the first GAV in the list overwrites the last
while (iter.hasPrevious()) {
final ProjectVersionRef ref = iter.previous();
Map<ArtifactRef, String> rBom = effectiveModelBuilder.getRemoteDependencyVersionOverrides(ref);
// We don't normalise the BOM list here as ::applyOverrides can handle multiple GA with different V
// for strict override. However, it is undefined if strict is not enabled.
bomOverrides.putAll(rBom);
}
}
if (depState.getPrecedence() == DependencyPrecedence.BOM) {
mergedOverrides = bomOverrides;
if (mergedOverrides.isEmpty()) {
String msg = restState.isEnabled() ? "dependencySource for restURL" : "dependencyManagement";
logger.warn("No dependencies found for dependencySource {}. Has {} been configured? ", depState.getPrecedence(), msg);
}
}
if (depState.getPrecedence() == DependencyPrecedence.REST) {
mergedOverrides = restOverrides;
if (mergedOverrides.isEmpty()) {
logger.warn("No dependencies found for dependencySource {}. Has restURL been configured? ", depState.getPrecedence());
}
} else if (depState.getPrecedence() == DependencyPrecedence.RESTBOM) {
mergedOverrides = bomOverrides;
removeDuplicateArtifacts(mergedOverrides, restOverrides);
mergedOverrides.putAll(restOverrides);
} else if (depState.getPrecedence() == DependencyPrecedence.BOMREST) {
mergedOverrides = restOverrides;
removeDuplicateArtifacts(mergedOverrides, bomOverrides);
mergedOverrides.putAll(bomOverrides);
}
logger.info("Remote precedence is {}", depState.getPrecedence());
logger.debug("Final remote override list is {}", mergedOverrides);
return mergedOverrides;
}
use of org.commonjava.maven.ext.core.state.RESTState in project pom-manipulation-ext by release-engineering.
the class RESTCollector method init.
@Override
public void init(final ManipulationSession session) throws ManipulationException {
this.session = session;
session.setState(new RESTState(session));
}
use of org.commonjava.maven.ext.core.state.RESTState in project pom-manipulation-ext by release-engineering.
the class PluginManipulator method loadRemoteBOM.
private Set<Plugin> loadRemoteBOM(PluginType type) throws ManipulationException {
final RESTState rState = session.getState(RESTState.class);
final PluginState pState = session.getState(PluginState.class);
final Set<Plugin> restOverrides = pState.getRemoteRESTOverrides();
final Set<Plugin> bomOverrides = new LinkedHashSet<>();
final List<ProjectVersionRef> gavs = pState.getRemotePluginMgmt();
Set<Plugin> mergedOverrides = new LinkedHashSet<>();
if (gavs != null) {
// We used to iterate in reverse order so that the first GAV in the list overwrites the last
// but due to the simplification moving to a single Set, as that doesn't support replace operation,
// we now iterate in normal order.
final Iterator<ProjectVersionRef> iter = gavs.iterator();
final Properties exclusions = (Properties) session.getUserProperties().clone();
exclusions.putAll(System.getProperties());
while (iter.hasNext()) {
final ProjectVersionRef ref = iter.next();
if (type == PluginType.RemotePM) {
bomOverrides.addAll(effectiveModelBuilder.getRemotePluginManagementVersionOverrides(ref, exclusions));
} else {
bomOverrides.addAll(effectiveModelBuilder.getRemotePluginVersionOverrides(ref, exclusions));
}
}
}
// TODO: Remote Plugin (as opposed to Remote PluginManagement) alignment is deprecated. Therefore we don't support combining it with REST.
if (type == PluginType.RemoteP) {
if (pState.getPrecedence() != PluginPrecedence.BOM) {
logger.warn("Remote plugin alignment is only supported with precedence type of BOM");
}
mergedOverrides = bomOverrides;
} else {
if (pState.getPrecedence() == PluginPrecedence.BOM) {
mergedOverrides = bomOverrides;
if (mergedOverrides.isEmpty()) {
String msg = rState.isEnabled() ? "pluginSource for restURL" : "pluginManagement";
logger.warn("No dependencies found for pluginSource {}. Has {} been configured? ", pState.getPrecedence(), msg);
}
}
if (pState.getPrecedence() == PluginPrecedence.REST) {
mergedOverrides = restOverrides;
if (mergedOverrides.isEmpty()) {
logger.warn("No dependencies found for pluginSource {}. Has restURL been configured? ", pState.getPrecedence());
}
} else if (pState.getPrecedence() == PluginPrecedence.RESTBOM) {
mergedOverrides = restOverrides;
mergedOverrides.addAll(bomOverrides);
} else if (pState.getPrecedence() == PluginPrecedence.BOMREST) {
mergedOverrides = bomOverrides;
mergedOverrides.addAll(restOverrides);
}
}
logger.debug("Final remote override list for type {} with precedence {} is {}", type.toString(), pState.getPrecedence(), mergedOverrides);
return mergedOverrides;
}
use of org.commonjava.maven.ext.core.state.RESTState in project pom-manipulation-ext by release-engineering.
the class RESTCollector method collect.
/**
* Prescans the Project to build up a list of Project GAs and also the various Dependencies.
*/
private void collect(final List<Project> projects) throws ManipulationException {
final RESTState state = session.getState(RESTState.class);
final VersioningState vs = session.getState(VersioningState.class);
final DependencyState ds = session.getState(DependencyState.class);
final PluginState ps = session.getState(PluginState.class);
if (!session.isEnabled() || !state.isEnabled()) {
logger.debug(getClass().getSimpleName() + ": Nothing to do!");
return;
}
final ArrayList<ProjectVersionRef> restParam = new ArrayList<>();
final ArrayList<ProjectVersionRef> newProjectKeys = new ArrayList<>();
final String override = vs.getOverride();
for (final Project project : projects) {
if (isEmpty(override)) {
// TODO: Check this : For the rest API I think we need to check every project GA not just inheritance root.
// Strip SNAPSHOT from the version for matching. DA will handle OSGi conversion.
ProjectVersionRef newKey = new SimpleProjectVersionRef(project.getKey());
if (project.getKey().getVersionString().endsWith("-SNAPSHOT")) {
if (!vs.preserveSnapshot()) {
newKey = new SimpleProjectVersionRef(project.getKey().asProjectRef(), project.getKey().getVersionString().substring(0, project.getKey().getVersionString().indexOf("-SNAPSHOT")));
} else {
logger.warn("SNAPSHOT detected for REST call but preserve-snapshots is enabled.");
}
}
newProjectKeys.add(newKey);
} else if (project.isExecutionRoot()) {
// We want to manually override the version ; therefore ignore what is in the project and calculate potential
// matches for that instead.
Project p = projects.get(0);
newProjectKeys.add(new SimpleProjectVersionRef(p.getGroupId(), p.getArtifactId(), override));
}
}
restParam.addAll(newProjectKeys);
// We only recognise dependencyManagement of the form g:a:version-rebuild not g:a:version-rebuild-<numeric>.
for (ProjectVersionRef bom : (ds.getRemoteBOMDepMgmt() == null ? Collections.<ProjectVersionRef>emptyList() : ds.getRemoteBOMDepMgmt())) {
if (!Version.hasBuildNumber(bom.getVersionString()) && bom.getVersionString().contains(PropertiesUtils.getSuffix(session))) {
// Create the dummy PVR to send to DA (which requires a numeric suffix).
ProjectVersionRef newBom = new SimpleProjectVersionRef(bom.asProjectRef(), bom.getVersionString() + "-0");
logger.debug("Adding dependencyManagement BOM {} into REST call.", newBom);
restParam.add(newBom);
}
}
Set<ArtifactRef> localDeps = establishAllDependencies(session, projects, null);
// Need to send that to the rest interface to get a translation.
for (ArtifactRef p : localDeps) {
restParam.add(p.asProjectVersionRef());
}
// Call the REST to populate the result.
logger.debug("Passing {} GAVs following into the REST client api {} ", restParam.size(), restParam);
logger.info("Calling REST client...");
long start = System.nanoTime();
Map<ProjectVersionRef, String> restResult = null;
try {
restResult = state.getVersionTranslator().translateVersions(restParam);
} finally {
printFinishTime(start, (restResult != null));
}
logger.debug("REST Client returned {} ", restResult);
// Process rest result for boms
ListIterator<ProjectVersionRef> iterator = (ds.getRemoteBOMDepMgmt() == null ? Collections.<ProjectVersionRef>emptyList().listIterator() : ds.getRemoteBOMDepMgmt().listIterator());
while (iterator.hasNext()) {
ProjectVersionRef pvr = iterator.next();
// As before, only process the BOMs if they are of the format <rebuild suffix> without a numeric portion.
if (!Version.hasBuildNumber(pvr.getVersionString()) && pvr.getVersionString().contains(PropertiesUtils.getSuffix(session))) {
// Create the dummy PVR to compare with results to...
ProjectVersionRef newBom = new SimpleProjectVersionRef(pvr.asProjectRef(), pvr.getVersionString() + "-0");
if (restResult.keySet().contains(newBom)) {
ProjectVersionRef replacementBOM = new SimpleProjectVersionRef(pvr.asProjectRef(), restResult.get(newBom));
logger.debug("Replacing BOM value of {} with {}.", pvr, replacementBOM);
iterator.remove();
iterator.add(replacementBOM);
}
}
}
vs.setRESTMetadata(parseVersions(session, projects, state, newProjectKeys, restResult));
final Map<ArtifactRef, String> overrides = new HashMap<>();
// Convert the loaded remote ProjectVersionRefs to the original ArtifactRefs
for (ArtifactRef a : localDeps) {
if (restResult.containsKey(a.asProjectVersionRef())) {
overrides.put(a, restResult.get(a.asProjectVersionRef()));
}
}
logger.debug("Setting REST Overrides {} ", overrides);
ds.setRemoteRESTOverrides(overrides);
// Unfortunately as everything is just GAVs we have to send everything to the PluginManipulator as well.
ps.setRemoteRESTOverrides(overrides);
}
Aggregations