Search in sources :

Example 6 with CommonState

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

the class DependencyManipulator method apply.

/**
 * Applies dependency overrides to the project.
 */
private void apply(final Project project, final Model model, final Map<ArtifactRef, String> overrides) throws ManipulationException {
    // Map of Group : Map of artifactId [ may be wildcard ] : value
    final WildcardMap<String> explicitOverrides = new WildcardMap<>();
    final String projectGA = ga(project);
    final DependencyState dependencyState = session.getState(DependencyState.class);
    final CommonState commonState = session.getState(CommonState.class);
    logger.info("Processing project {} ", projectGA);
    Map<ArtifactRef, String> moduleOverrides = new LinkedHashMap<>(overrides);
    moduleOverrides = removeReactorGAs(moduleOverrides);
    try {
        moduleOverrides = applyModuleVersionOverrides(projectGA, dependencyState.getDependencyExclusions(), moduleOverrides, explicitOverrides);
        logger.debug("Module overrides are:\n{}", moduleOverrides);
        logger.debug("Explicit overrides are:\n{}", explicitOverrides);
    } catch (InvalidRefException e) {
        logger.error("Invalid module exclusion override {} : {} ", moduleOverrides, explicitOverrides);
        throw e;
    }
    if (project.isInheritanceRoot()) {
        // Handle the situation where the top level parent refers to a prior build that is in the BOM.
        if (project.getModelParent() != null) {
            for (Map.Entry<ArtifactRef, String> entry : moduleOverrides.entrySet()) {
                String oldValue = project.getModelParent().getVersion();
                String newValue = entry.getValue();
                if (entry.getKey().asProjectRef().equals(SimpleProjectRef.parse(ga(project.getModelParent())))) {
                    if (commonState.getStrict()) {
                        if (!PropertiesUtils.checkStrictValue(session, oldValue, newValue)) {
                            if (commonState.getFailOnStrictViolation()) {
                                throw new ManipulationException("Parent reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", ga(project.getModelParent()), newValue, oldValue);
                            } else {
                                logger.warn("Parent reference {} replacement: {} of original version: {} violates the strict version-alignment rule!", ga(project.getModelParent()), newValue, oldValue);
                                // a new property either.
                                continue;
                            }
                        }
                    }
                    logger.debug(" Modifying parent reference from {} to {} for {} ", model.getParent().getVersion(), newValue, ga(project.getModelParent()));
                    model.getParent().setVersion(newValue);
                    break;
                }
            }
            // Apply any explicit overrides to the top level parent. Convert it to a simulated
            // dependency so we can reuse applyExplicitOverrides.
            HashMap<ArtifactRef, Dependency> pDepMap = new HashMap<>();
            Dependency d = new Dependency();
            d.setGroupId(project.getModelParent().getGroupId());
            d.setArtifactId(project.getModelParent().getArtifactId());
            d.setVersion(project.getModelParent().getVersion());
            pDepMap.put(SimpleArtifactRef.parse(d.getManagementKey()), d);
            applyExplicitOverrides(project, pDepMap, explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
            project.getModelParent().setVersion(d.getVersion());
        }
        if (session.getState(DependencyState.class).getOverrideDependencies()) {
            // Apply overrides to project dependency management
            logger.debug("Applying overrides to managed dependencies for: {}", projectGA);
            final Map<ArtifactRef, String> nonMatchingVersionOverrides = applyOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, moduleOverrides);
            final Map<ArtifactRef, String> matchedOverrides = new LinkedHashMap<>(moduleOverrides);
            matchedOverrides.keySet().removeAll(nonMatchingVersionOverrides.keySet());
            applyExplicitOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
            if (commonState.getOverrideTransitive()) {
                final List<Dependency> extraDeps = new ArrayList<>();
                // Add dependencies to Dependency Management which did not match any existing dependency
                for (final ArtifactRef var : overrides.keySet()) {
                    if (!nonMatchingVersionOverrides.containsKey(var)) {
                        // This one in the remote pom was already dealt with ; continue.
                        continue;
                    }
                    final Dependency newDependency = new Dependency();
                    newDependency.setGroupId(var.getGroupId());
                    newDependency.setArtifactId(var.getArtifactId());
                    newDependency.setType(var.getType());
                    newDependency.setClassifier(var.getClassifier());
                    final String artifactVersion = moduleOverrides.get(var);
                    newDependency.setVersion(artifactVersion);
                    extraDeps.add(newDependency);
                    logger.debug("New entry added to <DependencyManagement/> - {} : {} ", var, artifactVersion);
                }
                // If the model doesn't have any Dependency Management set by default, create one for it
                DependencyManagement dependencyManagement = model.getDependencyManagement();
                if (extraDeps.size() > 0) {
                    if (dependencyManagement == null) {
                        dependencyManagement = new DependencyManagement();
                        model.setDependencyManagement(dependencyManagement);
                        logger.debug("Added <DependencyManagement/> for current project");
                    }
                    dependencyManagement.getDependencies().addAll(0, extraDeps);
                }
            } else {
                logger.debug("Non-matching dependencies ignored.");
            }
        } else {
            logger.debug("NOT applying overrides to managed dependencies for top-pom: {}", projectGA);
        }
    } else {
        // If a child module has a depMgmt section we'll change that as well.
        if (session.getState(DependencyState.class).getOverrideDependencies()) {
            logger.debug("Applying overrides to managed dependencies for: {}", projectGA);
            applyOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, moduleOverrides);
            applyExplicitOverrides(project, project.getResolvedManagedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
        } else {
            logger.debug("NOT applying overrides to managed dependencies for: {}", projectGA);
        }
    }
    if (session.getState(DependencyState.class).getOverrideDependencies()) {
        logger.debug("Applying overrides to concrete dependencies for: {}", projectGA);
        // Apply overrides to project direct dependencies
        applyOverrides(project, project.getResolvedDependencies(session), explicitOverrides, moduleOverrides);
        applyExplicitOverrides(project, project.getResolvedDependencies(session), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
        final HashMap<Profile, HashMap<ArtifactRef, Dependency>> pd = project.getResolvedProfileDependencies(session);
        final HashMap<Profile, HashMap<ArtifactRef, Dependency>> pmd = project.getResolvedProfileManagedDependencies(session);
        for (Profile p : pd.keySet()) {
            applyOverrides(project, pd.get(p), explicitOverrides, moduleOverrides);
            applyExplicitOverrides(project, pd.get(p), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
        }
        for (Profile p : pmd.keySet()) {
            applyOverrides(project, pmd.get(p), explicitOverrides, moduleOverrides);
            applyExplicitOverrides(project, pmd.get(p), explicitOverrides, commonState, explicitVersionPropertyUpdateMap);
        }
    } else {
        logger.debug("NOT applying overrides to concrete dependencies for: {}", projectGA);
    }
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InvalidRefException(org.commonjava.maven.atlas.ident.ref.InvalidRefException) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) Profile(org.apache.maven.model.Profile) ArtifactRef(org.commonjava.maven.atlas.ident.ref.ArtifactRef) SimpleArtifactRef(org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef) LinkedHashMap(java.util.LinkedHashMap) WildcardMap(org.commonjava.maven.ext.core.util.WildcardMap) DependencyState(org.commonjava.maven.ext.core.state.DependencyState) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WildcardMap(org.commonjava.maven.ext.core.util.WildcardMap) DependencyManagement(org.apache.maven.model.DependencyManagement)

Example 7 with CommonState

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

the class CheckStrictValueTest method beforeTest.

@Before
public void beforeTest() throws ManipulationException {
    Properties user = new Properties();
    user.setProperty(VersioningState.VERSION_SUFFIX_SYSPROP.getCurrent(), "redhat-5");
    final VersioningState vs = new VersioningState(user);
    session.setState(vs);
    if (!strictIgnoreSuffix) {
        user.setProperty(CommonState.STRICT_ALIGNMENT_IGNORE_SUFFIX, "false");
    }
    final CommonState cs = new CommonState(user);
    session.setState(cs);
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) Properties(java.util.Properties) Before(org.junit.Before)

Example 8 with CommonState

use of org.commonjava.maven.ext.core.state.CommonState 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)

Example 9 with CommonState

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

the class PropertiesUtils method checkStrictValue.

/**
 * Check the version change is valid in strict mode.
 *
 * @param session the manipulation session
 * @param oldValue the original version
 * @param newValue the new version
 * @return true if the version can be changed to the new version
 */
public static boolean checkStrictValue(ManipulationSession session, String oldValue, String newValue) {
    if (oldValue == null || newValue == null) {
        return false;
    } else if (oldValue.equals(newValue)) {
        // The old version and new version matches. So technically it can be changed (even if its a bit pointless).
        return true;
    }
    final CommonState cState = session.getState(CommonState.class);
    final VersioningState vState = session.getState(VersioningState.class);
    final boolean ignoreSuffix = cState.getStrictIgnoreSuffix();
    // New value might be e.g. 3.1-rebuild-1 or 3.1.0.rebuild-1 (i.e. it *might* be OSGi compliant).
    String newVersion = newValue;
    String suffix = getSuffix(session);
    String v = oldValue;
    if (!vState.preserveSnapshot()) {
        v = Version.removeSnapshot(v);
    }
    String osgiVersion = Version.getOsgiVersion(v);
    if (isNotEmpty(suffix)) {
        // the oldValue actually contains the suffix process it.
        if (ignoreSuffix && oldValue.contains(suffix)) {
            HashSet<String> s = new HashSet<>();
            s.add(oldValue);
            s.add(newValue);
            String x = String.valueOf(Version.findHighestMatchingBuildNumber(v, s));
            // matching.
            if (newValue.endsWith(x)) {
                String oldValueCache = oldValue;
                oldValue = oldValue.substring(0, oldValue.indexOf(suffix) - 1);
                v = oldValue;
                osgiVersion = Version.getOsgiVersion(v);
                logger.debug("Updating version to {} and for oldValue {} with newValue {} ", v, oldValueCache, newValue);
            } else {
                logger.warn("strictIgnoreSuffix set but unable to align from {} to {}", oldValue, newValue);
            }
        }
        // to work out the OSGi version.
        if (!Version.hasQualifier(v)) {
            v = Version.appendQualifierSuffix(v, suffix);
            osgiVersion = Version.getOsgiVersion(v);
            osgiVersion = osgiVersion.substring(0, osgiVersion.indexOf(suffix) - 1);
        }
        if (newValue.contains(suffix)) {
            newVersion = newValue.substring(0, newValue.indexOf(suffix) - 1);
        }
    }
    logger.debug("Comparing original version {} and OSGi variant {} with new version {} and suffix removed {} ", oldValue, osgiVersion, newValue, newVersion);
    // We compare both an OSGi'ied oldVersion and the non-OSGi version against the possible new version (which has
    // had its suffix stripped) in order to check whether its a valid change.
    boolean result = false;
    if (oldValue.equals(newVersion) || osgiVersion.equals(newVersion)) {
        result = true;
    }
    return result;
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) HashSet(java.util.HashSet)

Aggregations

CommonState (org.commonjava.maven.ext.core.state.CommonState)9 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)5 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 VersioningState (org.commonjava.maven.ext.core.state.VersioningState)3 Properties (java.util.Properties)2 Dependency (org.apache.maven.model.Dependency)2 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)2 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)2 DependencyState (org.commonjava.maven.ext.core.state.DependencyState)2 WildcardMap (org.commonjava.maven.ext.core.util.WildcardMap)2 ArrayList (java.util.ArrayList)1 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 DependencyManagement (org.apache.maven.model.DependencyManagement)1 Plugin (org.apache.maven.model.Plugin)1