Search in sources :

Example 6 with DependencyState

use of org.commonjava.maven.ext.core.state.DependencyState 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);
}
Also used : PluginState(org.commonjava.maven.ext.core.state.PluginState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleScopedArtifactRef(org.commonjava.maven.ext.common.model.SimpleScopedArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) Project(org.commonjava.maven.ext.common.model.Project) DependencyState(org.commonjava.maven.ext.core.state.DependencyState) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) RESTState(org.commonjava.maven.ext.core.state.RESTState)

Example 7 with DependencyState

use of org.commonjava.maven.ext.core.state.DependencyState in project pom-manipulation-ext by release-engineering.

the class PropertiesUtilsTest method createUpdateSession.

private ManipulationSession createUpdateSession() throws Exception {
    ManipulationSession session = new ManipulationSession();
    session.setState(new DependencyState(p));
    session.setState(new VersioningState(p));
    session.setState(new CommonState(p));
    final MavenExecutionRequest req = new DefaultMavenExecutionRequest().setUserProperties(p).setRemoteRepositories(Collections.<ArtifactRepository>emptyList());
    final PlexusContainer container = new DefaultPlexusContainer();
    final MavenSession mavenSession = new MavenSession(container, null, req, new DefaultMavenExecutionResult());
    session.setMavenSession(mavenSession);
    return session;
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) MavenSession(org.apache.maven.execution.MavenSession) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) DependencyState(org.commonjava.maven.ext.core.state.DependencyState) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) VersioningState(org.commonjava.maven.ext.core.state.VersioningState)

Aggregations

DependencyState (org.commonjava.maven.ext.core.state.DependencyState)7 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)3 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)2 Project (org.commonjava.maven.ext.common.model.Project)2 CommonState (org.commonjava.maven.ext.core.state.CommonState)2 RESTState (org.commonjava.maven.ext.core.state.RESTState)2 VersioningState (org.commonjava.maven.ext.core.state.VersioningState)2 WildcardMap (org.commonjava.maven.ext.core.util.WildcardMap)2 HashSet (java.util.HashSet)1 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)1 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)1 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)1 MavenSession (org.apache.maven.execution.MavenSession)1 Dependency (org.apache.maven.model.Dependency)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1