Search in sources :

Example 6 with ProjectRef

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

the class WildcardMapTest method testGet.

@Test
public void testGet() throws Exception {
    final String value = "1.2";
    ProjectRef key1 = SimpleProjectRef.parse("org.group:new-artifact");
    ProjectRef key2 = SimpleProjectRef.parse("org.group:new-new-artifact");
    map.put(key1, value);
    map.put(key2, value);
    assertTrue(value.equals(map.get(key1)));
    assertTrue(value.equals(map.get(key2)));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) Test(org.junit.Test)

Example 7 with ProjectRef

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

the class DependencyManipulator method applyOverrides.

/**
 * Apply a set of version overrides to a list of dependencies. Return a set of the overrides which were not applied.
 *
 * @param project The current Project
 * @param dependencies The list of dependencies
 * @param explicitOverrides Any explicitOverrides to track for ignoring
 * @param overrides The map of dependency version overrides
 * @return The map of overrides that were not matched in the dependencies
 * @throws ManipulationException if an error occurs
 */
private Map<ArtifactRef, String> applyOverrides(final Project project, final HashMap<ArtifactRef, Dependency> dependencies, final WildcardMap<String> explicitOverrides, final Map<ArtifactRef, String> overrides) throws ManipulationException {
    // Duplicate the override map so unused overrides can be easily recorded
    final Map<ArtifactRef, String> unmatchedVersionOverrides = new LinkedHashMap<>();
    unmatchedVersionOverrides.putAll(overrides);
    if (dependencies == null || dependencies.size() == 0) {
        return unmatchedVersionOverrides;
    }
    final CommonState commonState = session.getState(CommonState.class);
    final boolean strict = commonState.getStrict();
    // Apply matching overrides to dependencies
    for (final ArtifactRef dependency : dependencies.keySet()) {
        ProjectRef depPr = new SimpleProjectRef(dependency.getGroupId(), dependency.getArtifactId());
        // will most likely happen is last-wins.
        for (final Map.Entry<ArtifactRef, String> entry : overrides.entrySet()) {
            ProjectRef groupIdArtifactId = entry.getKey().asProjectRef();
            if (depPr.equals(groupIdArtifactId)) {
                final String oldVersion = dependencies.get(dependency).getVersion();
                final String overrideVersion = entry.getValue();
                final String resolvedValue = dependency.getVersionString();
                if (isEmpty(overrideVersion)) {
                    logger.warn("Unable to align with an empty override version for " + groupIdArtifactId + "; ignoring");
                } else if (isEmpty(oldVersion)) {
                    logger.debug("Dependency is a managed version for " + groupIdArtifactId + "; ignoring");
                } else // By avoiding the potential duplicate work it also avoids a possible property clash problem.
                if (explicitOverrides.containsKey(depPr)) {
                    logger.debug("Dependency {} matches known explicit override so not performing initial override pass.", depPr);
                    unmatchedVersionOverrides.remove(entry.getKey());
                } else // have immediate access to the original property so the closest that is feasible is verify strict matching.
                if (strict && oldVersion.contains("$") && !PropertiesUtils.checkStrictValue(session, resolvedValue, overrideVersion)) {
                    logger.debug("Original fully resolved version {} of {} does not match override version {} -> {} so ignoring", resolvedValue, dependency, entry.getKey(), overrideVersion);
                    if (commonState.getFailOnStrictViolation()) {
                        throw new ManipulationException("For {} replacing original property version {} (fully resolved: {} ) with new version {} for {} violates the strict version-alignment rule!", depPr.toString(), dependencies.get(dependency).getVersion(), resolvedValue, entry.getKey().getVersionString(), entry.getKey().asProjectRef().toString());
                    } else {
                        logger.warn("Replacing original property version {} with new version {} for {} violates the strict version-alignment rule!", resolvedValue, overrideVersion, dependencies.get(dependency).getVersion());
                    }
                } else {
                    // Too much spurious logging with project.version.
                    if (!oldVersion.equals("${project.version}")) {
                        logger.info("Updating version {} for dependency {} from {}.", overrideVersion, dependency, project.getPom());
                    }
                    if (!PropertiesUtils.cacheProperty(project, commonState, versionPropertyUpdateMap, oldVersion, overrideVersion, entry.getKey(), false)) {
                        if (oldVersion.equals("${project.version}")) {
                            logger.debug("For dependency {} ; version is built in {} so skipping inlining {}", groupIdArtifactId, oldVersion, overrideVersion);
                        } else if (strict && !PropertiesUtils.checkStrictValue(session, resolvedValue, overrideVersion)) {
                            if (commonState.getFailOnStrictViolation()) {
                                throw new ManipulationException("Replacing original version {} in dependency {} with new version {} violates the strict version-alignment rule!", oldVersion, groupIdArtifactId.toString(), overrideVersion);
                            } else {
                                logger.warn("Replacing original version {} in dependency {} with new version {} violates the strict version-alignment rule!", oldVersion, groupIdArtifactId, overrideVersion);
                            }
                        } else {
                            logger.debug("Altered dependency {} : {} -> {}", groupIdArtifactId, oldVersion, overrideVersion);
                            if (oldVersion.contains("${")) {
                                String suffix = PropertiesUtils.getSuffix(session);
                                String replaceVersion;
                                if (commonState.getStrictIgnoreSuffix() && oldVersion.contains(suffix)) {
                                    replaceVersion = StringUtils.substringBefore(oldVersion, suffix);
                                    replaceVersion += suffix + StringUtils.substringAfter(overrideVersion, suffix);
                                } else {
                                    replaceVersion = oldVersion + StringUtils.removeStart(overrideVersion, resolvedValue);
                                }
                                logger.debug("Resolved value is {} and replacement version is {} ", resolvedValue, replaceVersion);
                                // In this case the previous value couldn't be cached even though it contained a property
                                // as it was either multiple properties or a property combined with a hardcoded value. Therefore
                                // just append the suffix.
                                dependencies.get(dependency).setVersion(replaceVersion);
                            } else {
                                dependencies.get(dependency).setVersion(overrideVersion);
                            }
                        }
                    }
                    unmatchedVersionOverrides.remove(entry.getKey());
                }
            }
        }
    }
    return unmatchedVersionOverrides;
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WildcardMap(org.commonjava.maven.ext.core.util.WildcardMap) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with ProjectRef

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

the class RESTCollector method parseVersions.

/**
 * Parse the rest result for the project GAs and store them in versioning state for use
 * there by incremental suffix calculation.
 */
private Map<ProjectRef, Set<String>> parseVersions(ManipulationSession session, List<Project> projects, RESTState state, ArrayList<ProjectVersionRef> newProjectKeys, Map<ProjectVersionRef, String> restResult) throws ManipulationException {
    Map<ProjectRef, Set<String>> versionStates = new HashMap<>();
    for (final ProjectVersionRef p : newProjectKeys) {
        if (restResult.containsKey(p)) {
            // Found part of the current project to store in Versioning State
            Set<String> versions = versionStates.get(p.asProjectRef());
            if (versions == null) {
                versions = new HashSet<>();
                versionStates.put(p.asProjectRef(), versions);
            }
            versions.add(restResult.get(p));
        }
    }
    logger.debug("Added the following ProjectRef:Version from REST call into VersionState {}", versionStates);
    // We know we have ProjectVersionRef(s) of the current project(s). We need to establish potential
    // blacklist by calling
    // GET /listings/blacklist/ga?groupid=GROUP_ID&artifactid=ARTIFACT_ID
    // passing in the groupId and artifactId.
    // From the results we then need to establish whether the community version occurs in the blacklist
    // causing a total abort and whether any redhat versions occur in the blacklist. If they do, that will
    // affect the incremental potential options. The simplest option is simply to add those results to versionStates
    // list. This will cause the incremental build number to be set to greater than those.
    List<ProjectVersionRef> blacklist;
    for (Project p : projects) {
        if (p.isExecutionRoot()) {
            logger.debug("Calling REST client for blacklist with {}...", p.getKey().asProjectRef());
            blacklist = state.getVersionTranslator().findBlacklisted(p.getKey().asProjectRef());
            if (blacklist.size() > 0) {
                String suffix = PropertiesUtils.getSuffix(session);
                String bVersion = blacklist.get(0).getVersionString();
                String pVersion = p.getVersion();
                logger.debug("REST Client returned for blacklist {} ", blacklist);
                if (isEmpty(suffix)) {
                    logger.warn("No version suffix found ; unable to verify community blacklisting.");
                } else if (blacklist.size() == 1 && !bVersion.contains(suffix)) {
                    if (pVersion.contains(suffix)) {
                        pVersion = pVersion.substring(0, pVersion.indexOf(suffix) - 1);
                    }
                    if (pVersion.equals(bVersion)) {
                        throw new ManipulationException("community artifact '" + blacklist.get(0) + "' has been blacklisted. Unable to build project version " + p.getVersion());
                    }
                }
                // Found part of the current project to store in Versioning State
                Set<String> versions = versionStates.get(p.getKey().asProjectRef());
                if (versions == null) {
                    versions = new HashSet<>();
                    versionStates.put(p.getKey().asProjectRef(), versions);
                }
                for (ProjectVersionRef b : blacklist) {
                    versions.add(b.getVersionString());
                }
            }
            break;
        }
    }
    return versionStates;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef)

Example 9 with ProjectRef

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

the class VersioningCalculatorTest method setupSession.

private VersioningState setupSession(final Properties properties, final Map<ProjectRef, String[]> versionMap) throws Exception {
    final ArtifactRepository ar = new MavenArtifactRepository("test", "http://repo.maven.apache.org/maven2", new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy());
    final MavenExecutionRequest req = new DefaultMavenExecutionRequest().setUserProperties(properties).setRemoteRepositories(Arrays.asList(ar));
    final PlexusContainer container = new DefaultPlexusContainer();
    final MavenSession mavenSession = new MavenSession(container, null, req, new DefaultMavenExecutionResult());
    session = new ManipulationSession();
    session.setMavenSession(mavenSession);
    final VersioningState state = new VersioningState(properties);
    session.setState(state);
    final Map<String, byte[]> dataMap = new HashMap<>();
    if (versionMap != null && !versionMap.isEmpty()) {
        for (final Map.Entry<ProjectRef, String[]> entry : versionMap.entrySet()) {
            final String path = toMetadataPath(entry.getKey());
            final byte[] data = setupMetadataVersions(entry.getValue());
            dataMap.put(path, data);
        }
    }
    final Location mdLoc = MavenLocationExpander.EXPANSION_TARGET;
    final Transport mdTrans = new StubTransport(dataMap);
    modder = new TestVersionCalculator(new ManipulationSession(), mdLoc, mdTrans, temp.newFolder("galley-cache"));
    return state;
}
Also used : DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) HashMap(java.util.HashMap) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) StubTransport(org.commonjava.maven.ext.core.fixture.StubTransport) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenSession(org.apache.maven.execution.MavenSession) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) Transport(org.commonjava.maven.galley.spi.transport.Transport) StubTransport(org.commonjava.maven.ext.core.fixture.StubTransport) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.commonjava.maven.galley.model.Location)

Example 10 with ProjectRef

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

the class IdUtils method parseGAs.

/**
 * Splits the value on ',', then wraps each value in {@link SimpleProjectRef#parse(String)} and prints a warning / skips in the event of a
 * parsing error. Returns null if the input value is null.
 * @param value a comma separated list of GA to parse
 * @return a collection of parsed ProjectRef.
 */
public static List<ProjectRef> parseGAs(final String value) {
    if (isEmpty(value)) {
        return null;
    } else {
        final String[] gavs = value.split(",");
        final List<ProjectRef> refs = new ArrayList<>();
        for (final String gav : gavs) {
            try {
                final ProjectRef ref = SimpleProjectRef.parse(gav);
                refs.add(ref);
            } catch (final InvalidRefException e) {
                logger.error("Skipping invalid remote management GAV: " + gav);
                throw e;
            }
        }
        return refs;
    }
}
Also used : InvalidRefException(org.commonjava.maven.atlas.ident.ref.InvalidRefException) ArrayList(java.util.ArrayList) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef)

Aggregations

ProjectRef (org.commonjava.maven.atlas.ident.ref.ProjectRef)22 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)15 HashMap (java.util.HashMap)6 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)4 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)4 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 GalleyMavenException (org.commonjava.maven.galley.maven.GalleyMavenException)3 Set (java.util.Set)2 Model (org.apache.maven.model.Model)2 Profile (org.apache.maven.model.Profile)2 InvalidRefException (org.commonjava.maven.atlas.ident.ref.InvalidRefException)2 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)2 JoinString (org.commonjava.maven.atlas.ident.util.JoinString)2 Project (org.commonjava.maven.ext.common.model.Project)2