Search in sources :

Example 16 with ArtifactRef

use of org.commonjava.maven.atlas.ident.ref.ArtifactRef in project pom-manipulation-ext by release-engineering.

the class DependencyManipulator method removeReactorGAs.

/**
 * Remove version overrides which refer to projects in the current reactor.
 * Projects in the reactor include things like inter-module dependencies
 * which should never be overridden.
 * @param versionOverrides current set of ArtifactRef:newVersion overrides.
 * @return A new Map with the reactor GAs removed.
 */
private Map<ArtifactRef, String> removeReactorGAs(final Map<ArtifactRef, String> versionOverrides) throws ManipulationException {
    final Map<ArtifactRef, String> reducedVersionOverrides = new LinkedHashMap<>(versionOverrides);
    for (final Project project : session.getProjects()) {
        final String reactorGA = gav(project.getModel());
        reducedVersionOverrides.remove(SimpleArtifactRef.parse(reactorGA));
    }
    return reducedVersionOverrides;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with ArtifactRef

use of org.commonjava.maven.atlas.ident.ref.ArtifactRef in project pom-manipulation-ext by release-engineering.

the class DependencyManipulator method removeDuplicateArtifacts.

private void removeDuplicateArtifacts(Map<ArtifactRef, String> mergedOverrides, Map<ArtifactRef, String> targetOverrides) {
    Iterator<ArtifactRef> i = mergedOverrides.keySet().iterator();
    while (i.hasNext()) {
        ArtifactRef key = i.next();
        ProjectRef pRef = key.asProjectRef();
        for (ArtifactRef target : targetOverrides.keySet()) {
            if (pRef.equals(target.asProjectRef())) {
                logger.debug("From source overrides artifact {} clashes with target {}", key, target);
                i.remove();
                break;
            }
        }
    }
}
Also used : SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)

Example 18 with ArtifactRef

use of org.commonjava.maven.atlas.ident.ref.ArtifactRef in project pom-manipulation-ext by release-engineering.

the class DependencyManipulator method applyModuleVersionOverrides.

/**
 * Remove module overrides which do not apply to the current module. Searches the full list of version overrides
 * for any keys which contain the '@' symbol.  Removes these from the version overrides list, and add them back
 * without the '@' symbol only if they apply to the current module.
 *
 * @param projectGA the current project group : artifact
 * @param originalOverrides The full list of version overrides, both global and module specific
 * @param moduleOverrides are individual overrides e.g. group:artifact@groupId:artifactId :: value
 * @param explicitOverrides a custom map to handle wildcard overrides
 * @return The map of global and module specific overrides which apply to the given module
 * @throws ManipulationException if an error occurs
 */
private Map<ArtifactRef, String> applyModuleVersionOverrides(final String projectGA, final Map<String, String> moduleOverrides, Map<ArtifactRef, String> originalOverrides, final WildcardMap explicitOverrides) throws ManipulationException {
    final Map<ArtifactRef, String> remainingOverrides = new LinkedHashMap<>(originalOverrides);
    logger.debug("Calculating module-specific version overrides. Starting with:\n  {}", join(remainingOverrides.entrySet(), "\n  "));
    // These modes correspond to two different kinds of passes over the available override properties:
    // 1. Module-specific: Don't process wildcard overrides here, allow module-specific settings to take precedence.
    // 2. Wildcards: Add these IF there is no corresponding module-specific override.
    final boolean[] wildcardMode = { false, true };
    for (boolean aWildcardMode : wildcardMode) {
        for (final String currentKey : new HashSet<>(moduleOverrides.keySet())) {
            final String currentValue = moduleOverrides.get(currentKey);
            logger.debug("Processing key {} for override with value {}", currentKey, currentValue);
            if (!currentKey.contains("@")) {
                logger.debug("Not an override. Skip.");
                continue;
            }
            final boolean isModuleWildcard = currentKey.endsWith("@*");
            logger.debug("Is wildcard? {} and in module wildcard mode? {} ", isModuleWildcard, aWildcardMode);
            // process module-specific overrides (first)
            if (!aWildcardMode) {
                // skip wildcard overrides in this mode
                if (isModuleWildcard) {
                    logger.debug("Not currently in wildcard mode. Skip.");
                    continue;
                }
                final String[] artifactAndModule = currentKey.split("@");
                if (artifactAndModule.length != 2) {
                    throw new ManipulationException("Invalid format for exclusion key " + currentKey);
                }
                final String artifactGA = artifactAndModule[0];
                final ProjectRef moduleGA = SimpleProjectRef.parse(artifactAndModule[1]);
                logger.debug("For artifact override: {}, comparing parsed module: {} to current project: {}", artifactGA, moduleGA, projectGA);
                if (moduleGA.toString().equals(projectGA) || (moduleGA.getArtifactId().equals("*") && SimpleProjectRef.parse(projectGA).getGroupId().equals(moduleGA.getGroupId()))) {
                    if (currentValue != null && !currentValue.isEmpty()) {
                        explicitOverrides.put(SimpleProjectRef.parse(artifactGA), currentValue);
                        logger.debug("Overriding module dependency for {} with {} : {}", moduleGA, artifactGA, currentValue);
                    } else {
                        // Override prevention...
                        removeGA(remainingOverrides, SimpleProjectRef.parse(artifactGA));
                        logger.debug("For module {}, ignoring dependency override for {} ", moduleGA, artifactGA);
                    }
                }
            } else // process wildcard overrides (second)
            {
                // skip module-specific overrides in this mode i.e. format of groupdId:artifactId@*=
                if (!isModuleWildcard) {
                    logger.debug("Currently in wildcard mode. Skip.");
                    continue;
                }
                final String artifactGA = currentKey.substring(0, currentKey.length() - 2);
                logger.debug("For artifact override: {}, checking if current overrides already contain a module-specific version.", artifactGA);
                if (explicitOverrides.containsKey(SimpleProjectRef.parse(artifactGA))) {
                    logger.debug("For artifact override: {}, current overrides already contain a module-specific version. Skip.", artifactGA);
                    continue;
                }
                // I think this is only used for e.g. dependencyExclusion.groupId:artifactId@*=<explicitVersion>
                if (currentValue != null && !currentValue.isEmpty()) {
                    logger.debug("Overriding module dependency for {} with {} : {}", projectGA, artifactGA, currentValue);
                    explicitOverrides.put(SimpleProjectRef.parse(artifactGA), currentValue);
                } else {
                    // If we have a wildcard artifact we want to replace any prior explicit overrides
                    // with this one i.e. this takes precedence.
                    removeGA(remainingOverrides, SimpleProjectRef.parse(artifactGA));
                    logger.debug("Removing artifactGA " + artifactGA + " from overrides");
                }
            }
        }
    }
    return remainingOverrides;
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 19 with ArtifactRef

use of org.commonjava.maven.atlas.ident.ref.ArtifactRef 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 20 with ArtifactRef

use of org.commonjava.maven.atlas.ident.ref.ArtifactRef in project pom-manipulation-ext by release-engineering.

the class ResolveArtifactTest method testResolveArtifacts.

@Test
public void testResolveArtifacts() throws Exception {
    final ManipulationSession session = new ManipulationSession();
    // Locate the PME project pom file.
    final File projectroot = new File(TestUtils.resolveFileResource("properties/", "").getParentFile().getParentFile().getParentFile().getParentFile(), "pom.xml");
    PomIO pomIO = new PomIO();
    List<Project> projects = pomIO.parseProject(projectroot);
    Set<ArtifactRef> artifacts = RESTCollector.establishAllDependencies(session, projects, null);
    // NB If this test fails then check if PME deps/plugins have changed...
    assertTrue(artifacts.size() == 59);
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) File(java.io.File) PomIO(org.commonjava.maven.ext.io.PomIO) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) Test(org.junit.Test)

Aggregations

ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)24 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)17 Dependency (org.apache.maven.model.Dependency)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)6 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)6 Project (org.commonjava.maven.ext.common.model.Project)6 Profile (org.apache.maven.model.Profile)5 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)5 File (java.io.File)4 HashSet (java.util.HashSet)4 ProjectRef (org.commonjava.maven.atlas.ident.ref.ProjectRef)4 SimpleScopedArtifactRef (org.commonjava.maven.ext.common.model.SimpleScopedArtifactRef)4 ArrayList (java.util.ArrayList)3 InvalidRefException (org.commonjava.maven.atlas.ident.ref.InvalidRefException)3 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)3 ManipulationSession (org.commonjava.maven.ext.core.ManipulationSession)3 DependencyState (org.commonjava.maven.ext.core.state.DependencyState)3 Test (org.junit.Test)3 URI (java.net.URI)2