Search in sources :

Example 11 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException 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 12 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class JSONManipulator method internalApplyChanges.

// Package accessible so tests can use it.
void internalApplyChanges(Project project, JSONState.JSONOperation operation) throws ManipulationException {
    File target = new File(project.getPom().getParentFile(), operation.getFile());
    logger.info("Attempting to start JSON update to file {} with xpath {} and replacement '{}' ", target, operation.getXPath(), operation.getUpdate());
    DocumentContext dc = null;
    try {
        if (!target.exists()) {
            logger.error("Unable to locate JSON file {} ", target);
            throw new ManipulationException("Unable to locate JSON file " + target);
        }
        dc = jsonIO.parseJSON(target);
        List o = dc.read(operation.getXPath());
        if (o.size() == 0) {
            if (project.isIncrementalPME()) {
                logger.warn("Did not locate JSON using XPath " + operation.getXPath());
                return;
            } else {
                logger.error("XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile());
                throw new ManipulationException("XPath did not resolve to a valid value");
            }
        }
        if (isEmpty(operation.getUpdate())) {
            // Delete
            logger.info("Deleting {} on {}", operation.getXPath(), dc.toString());
            dc.delete(operation.getXPath());
        } else {
            // Update
            logger.info("Updating {} on {}", operation.getXPath(), dc.toString());
            dc.set(operation.getXPath(), operation.getUpdate());
        }
        jsonIO.writeJSON(target, dc.jsonString());
    } catch (JsonPathException e) {
        logger.error("Caught JSON exception processing file {}, document context {} ", target, dc, e);
        throw new ManipulationException("Caught JsonPath", e);
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) JsonPathException(com.jayway.jsonpath.JsonPathException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) File(java.io.File)

Example 13 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException 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 14 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class PropertiesUtilsTest method testUpdateNestedProperties.

@Test
public void testUpdateNestedProperties() throws Exception {
    Project pP = getProject();
    ManipulationSession session = createUpdateSession();
    assertTrue(updateProperties(session, pP, false, "version.hibernate.core", "5.0.4.Final-redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    assertTrue(updateProperties(session, pP, false, "version.scala", "2.11.7.redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    try {
        updateProperties(session, pP, false, "version.scala", "3.11.7-redhat-1");
    } catch (ManipulationException e) {
    // Pass.
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Test(org.junit.Test)

Example 15 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class PropertiesUtilsTest method testCacheProperty.

@Test
public void testCacheProperty() throws Exception {
    Map<Project, Map<String, String>> propertyMap = new HashMap<>();
    CommonState state = new CommonState(new Properties());
    Project project = getProject();
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${foobar}${foobar2}", null, null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "suffix.${foobar}", null, null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, null, "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "1.0", "2.0", null, false));
    assertTrue(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.org.jboss}", "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${project.version}", "2.0", null, false));
    // DependencyManipulator does dependency.getVersion(). This could return e.g. ${version.scala} which can
    // refer to <version.scala>${version.scala.major}.7</version.scala>. If we are attempting to change version.scala
    // to e.g. 2.11.7.redhat-1 then in this case we need to ignore the version.scala.major property and append the .redhat-1.
    // If the property is ${...}.foobar then we only want to append suffix to foobar to change the version
    // However we don't need to change the value of the property. If the property is foobar.${....} then
    // we want to append suffix to the property ... but we need to handle that part of the property is hardcoded.
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.scala}.7", "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.foo}.${version.scala}.7", "2.0", null, false));
    try {
        PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.scala}.7.${version.scala2}", "2.0", null, false);
    } catch (ManipulationException e) {
    // Pass.
    }
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) Project(org.commonjava.maven.ext.common.model.Project) HashMap(java.util.HashMap) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) PropertiesUtils.updateProperties(org.commonjava.maven.ext.core.util.PropertiesUtils.updateProperties) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ManipulationException (org.commonjava.maven.ext.common.ManipulationException)42 IOException (java.io.IOException)13 File (java.io.File)11 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedHashMap (java.util.LinkedHashMap)7 Project (org.commonjava.maven.ext.common.model.Project)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)6 Properties (java.util.Properties)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)5 JsonPathException (com.jayway.jsonpath.JsonPathException)4 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)4 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)4 CommonState (org.commonjava.maven.ext.core.state.CommonState)4 Test (org.junit.Test)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 FileInputStream (java.io.FileInputStream)3 Dependency (org.apache.maven.model.Dependency)3