Search in sources :

Example 31 with Profile

use of org.apache.maven.model.Profile in project pom-manipulation-ext by release-engineering.

the class Project method getResolvedProfileManagedPlugins.

/**
 * This method will scan the plugins in the pluginManagement section in the potentially active Profiles
 * in this project and return a fully resolved list. Note that while updating the {@link Plugin}
 * reference returned will be reflected in the Model as it is the same object, if you wish to remove
 * or add items to the Model then you must use {@link #getModel()}
 *
 * @param session MavenSessionHandler, used by {@link PropertyResolver}
 * @return a list of fully resolved {@link ProjectVersionRef} to the original {@link Plugin}
 * @throws ManipulationException if an error occurs
 */
public HashMap<Profile, HashMap<ProjectVersionRef, Plugin>> getResolvedProfileManagedPlugins(MavenSessionHandler session) throws ManipulationException {
    if (resolvedProfileManagedPlugins == null) {
        resolvedProfileManagedPlugins = new HashMap<>();
        for (final Profile profile : ProfileUtils.getProfiles(session, model)) {
            HashMap<ProjectVersionRef, Plugin> profileDeps = new HashMap<>();
            if (profile.getBuild() != null) {
                final PluginManagement pm = profile.getBuild().getPluginManagement();
                if (!(pm == null || pm.getPlugins() == null)) {
                    resolvePlugins(session, pm.getPlugins(), profileDeps);
                }
            }
            resolvedProfileManagedPlugins.put(profile, profileDeps);
        }
    }
    return resolvedProfileManagedPlugins;
}
Also used : PluginManagement(org.apache.maven.model.PluginManagement) HashMap(java.util.HashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) Profile(org.apache.maven.model.Profile) Plugin(org.apache.maven.model.Plugin)

Example 32 with Profile

use of org.apache.maven.model.Profile in project pom-manipulation-ext by release-engineering.

the class Project method getResolvedProfileDependencies.

/**
 * This method will scan the dependencies in the potentially active Profiles in this project and
 * return a fully resolved list.  Note that this will only return full dependencies not managed
 * i.e. those with a group, artifact and version.
 *
 * Note that while updating the {@link Dependency} reference returned will be reflected in the
 * Model as it is the same object, if you wish to remove or add items to the Model then you
 * must use {@link #getModel()}
 *
 * @param session MavenSessionHandler, used by {@link PropertyResolver}
 * @return a list of fully resolved {@link ArtifactRef} to the original {@link Dependency}
 * @throws ManipulationException if an error occurs
 */
public HashMap<Profile, HashMap<ArtifactRef, Dependency>> getResolvedProfileDependencies(MavenSessionHandler session) throws ManipulationException {
    if (resolvedProfileDependencies == null) {
        resolvedProfileDependencies = new HashMap<>();
        for (final Profile profile : ProfileUtils.getProfiles(session, model)) {
            HashMap<ArtifactRef, Dependency> profileDeps = new HashMap<>();
            resolveDeps(session, profile.getDependencies(), false, profileDeps);
            resolvedProfileDependencies.put(profile, profileDeps);
        }
    }
    return resolvedProfileDependencies;
}
Also used : HashMap(java.util.HashMap) 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)

Example 33 with Profile

use of org.apache.maven.model.Profile 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 34 with Profile

use of org.apache.maven.model.Profile in project pom-manipulation-ext by release-engineering.

the class PluginRemovalManipulator method apply.

private boolean apply(final Project project, final Model model) {
    final PluginRemovalState state = session.getState(PluginRemovalState.class);
    logger.debug("Applying plugin changes to: " + ga(project));
    boolean result = false;
    List<ProjectRef> pluginsToRemove = state.getPluginRemoval();
    if (model.getBuild() != null) {
        result = scanPlugins(pluginsToRemove, model.getBuild().getPlugins());
    }
    for (final Profile profile : ProfileUtils.getProfiles(session, model)) {
        if (profile.getBuild() != null && scanPlugins(pluginsToRemove, profile.getBuild().getPlugins())) {
            result = true;
        }
    }
    return result;
}
Also used : PluginRemovalState(org.commonjava.maven.ext.core.state.PluginRemovalState) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) Profile(org.apache.maven.model.Profile)

Example 35 with Profile

use of org.apache.maven.model.Profile in project pom-manipulation-ext by release-engineering.

the class ProjectVersionManipulatorTest method updateEffectiveAndOriginalModelDependencyVersions_InProfile.

@Test
public void updateEffectiveAndOriginalModelDependencyVersions_InProfile() throws Exception {
    final Model orig = new Model();
    orig.setGroupId("org.foo");
    orig.setArtifactId("bar");
    orig.setVersion("1.0");
    final Model depModel = new Model();
    depModel.setGroupId("org.foo");
    depModel.setArtifactId("bar-dep");
    depModel.setVersion("1.0");
    final Dependency dep = new Dependency();
    dep.setGroupId(depModel.getGroupId());
    dep.setArtifactId(depModel.getArtifactId());
    dep.setVersion(depModel.getVersion());
    final Profile p = new Profile();
    p.setId("test");
    orig.addProfile(p);
    p.addDependency(dep);
    final DependencyManagement mgmt = new DependencyManagement();
    final Model dmModel = new Model();
    dmModel.setGroupId("org.foo");
    dmModel.setArtifactId("bar-managed-dep");
    dmModel.setVersion("1.0");
    final Dependency managed = new Dependency();
    managed.setGroupId(dmModel.getGroupId());
    managed.setArtifactId(dmModel.getArtifactId());
    managed.setVersion(dmModel.getVersion());
    mgmt.addDependency(managed);
    p.setDependencyManagement(mgmt);
    final String suff = "redhat-1";
    final String mv = orig.getVersion() + "." + suff;
    final Map<ProjectVersionRef, String> versionsByGA = new HashMap<>();
    versionsByGA.put(new SimpleProjectVersionRef(orig.getGroupId(), orig.getArtifactId(), orig.getVersion()), mv);
    versionsByGA.put(new SimpleProjectVersionRef(depModel.getGroupId(), depModel.getArtifactId(), depModel.getVersion()), mv);
    versionsByGA.put(new SimpleProjectVersionRef(dmModel.getGroupId(), dmModel.getArtifactId(), dmModel.getVersion()), mv);
    final List<MavenProject> projects = new ArrayList<>();
    MavenProject project = new MavenProject(depModel.clone());
    project.setOriginalModel(depModel);
    projects.add(project);
    project = new MavenProject(dmModel.clone());
    project.setOriginalModel(dmModel);
    projects.add(project);
    project = new MavenProject(orig.clone());
    project.setOriginalModel(orig);
    projects.add(project);
    final Set<MavenProject> changes = newVersioningModifier().applyVersioningChanges(projects, versionsByGA);
    assertThat(changes.size(), equalTo(3));
    for (final MavenProject proj : changes) {
        final String a = proj.getArtifactId();
        if (a.equals("bar")) {
            assertThat(proj.getOriginalModel().getVersion(), equalTo(mv));
            assertThat(proj.getModel().getVersion(), equalTo(mv));
            final Profile op = proj.getOriginalModel().getProfiles().get(0);
            final Profile ep = proj.getModel().getProfiles().get(0);
            List<Dependency> deps = op.getDependencies();
            assertThat(deps.size(), equalTo(1));
            Dependency d = deps.get(0);
            assertThat(d, notNullValue());
            assertThat(d.getVersion(), equalTo(mv));
            deps = ep.getDependencies();
            assertThat(deps.size(), equalTo(1));
            d = deps.get(0);
            assertThat(d, notNullValue());
            assertThat(d.getVersion(), equalTo(mv));
            deps = op.getDependencyManagement().getDependencies();
            assertThat(deps.size(), equalTo(1));
            d = deps.get(0);
            assertThat(d, notNullValue());
            assertThat(d.getVersion(), equalTo(mv));
            deps = ep.getDependencyManagement().getDependencies();
            assertThat(deps.size(), equalTo(1));
            d = deps.get(0);
            assertThat(d, notNullValue());
            assertThat(d.getVersion(), equalTo(mv));
        } else {
            assertThat(proj.getOriginalModel().getVersion(), equalTo(mv));
            assertThat(proj.getModel().getVersion(), equalTo(mv));
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Dependency(org.apache.maven.model.Dependency) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) Profile(org.apache.maven.model.Profile) MavenProject(org.apache.maven.project.MavenProject) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) Model(org.apache.maven.model.Model) DependencyManagement(org.apache.maven.model.DependencyManagement) Test(org.junit.Test)

Aggregations

Profile (org.apache.maven.model.Profile)52 HashMap (java.util.HashMap)12 Model (org.apache.maven.model.Model)12 ArrayList (java.util.ArrayList)9 Dependency (org.apache.maven.model.Dependency)8 HashSet (java.util.HashSet)7 DependencyManagement (org.apache.maven.model.DependencyManagement)7 File (java.io.File)6 Activation (org.apache.maven.model.Activation)6 MavenProject (org.apache.maven.project.MavenProject)6 Project (org.commonjava.maven.ext.common.model.Project)6 Build (org.apache.maven.model.Build)5 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)5 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)5 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)5 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)4 ArtifactCoordinates (com.itemis.maven.aether.ArtifactCoordinates)3 IOException (java.io.IOException)3 Iterator (java.util.Iterator)3