Search in sources :

Example 1 with PropertyInterpolator

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

the class PropertyInterpolatorTest method testInteropolateDependencies.

@Test
public void testInteropolateDependencies() throws Exception {
    final Model model = TestUtils.resolveModelResource(RESOURCE_BASE, "infinispan-bom-8.2.0.Final.pom");
    Project project = new Project(model);
    PropertyInterpolator pi = new PropertyInterpolator(project.getModel().getProperties(), project);
    String nonInterp = "", interp = "";
    // Explicitly calling the non-resolved model dependencies...
    for (Dependency d : project.getModel().getDependencyManagement().getDependencies()) {
        nonInterp += (d.getGroupId().equals("${project.groupId}") ? project.getGroupId() : d.getGroupId()) + ":" + (d.getArtifactId().equals("${project.artifactId}") ? project.getArtifactId() : d.getArtifactId()) + System.lineSeparator();
        interp += pi.interp(d.getGroupId().equals("${project.groupId}") ? project.getGroupId() : d.getGroupId()) + ":" + pi.interp(d.getArtifactId().equals("${project.artifactId}") ? project.getArtifactId() : d.getArtifactId()) + System.lineSeparator();
    }
    assertTrue(nonInterp.contains("${"));
    assertFalse(interp.contains("${"));
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) Dependency(org.apache.maven.model.Dependency) PropertyInterpolator(org.commonjava.maven.ext.common.util.PropertyInterpolator) Test(org.junit.Test)

Example 2 with PropertyInterpolator

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

the class ProjectVersioningManipulator method applyVersioningChanges.

/**
 * Apply any project versioning changes applicable for the given {@link Model}, using accumulated version-change information stored in the
 * {@link VersioningState} instance, and also produced during the initial {@link Manipulator#applyChanges(List)} invocation.
 *
 * These changes include the main POM version, but may also include the parent declaration and dependencies, if they reference other POMs in the
 * current build.
 *
 * If the project is modified, then it is marked as changed in the {@link ManipulationSession}, which triggers the associated POM to be rewritten.
 *
 * @param project Project undergoing modification.
 * @param state the VersioningState
 * @return whether any changes have been applied.
 * @throws ManipulationException if an error occurs.
 */
// TODO: Loooong method
protected boolean applyVersioningChanges(final Project project, final VersioningState state) throws ManipulationException {
    if (!state.hasVersionsByGAVMap()) {
        return false;
    }
    final Model model = project.getModel();
    if (model == null) {
        return false;
    }
    String g = model.getGroupId();
    String v = model.getVersion();
    final Parent parent = model.getParent();
    // If the groupId or version is null, it means they must be taken from the parent config
    if (g == null && parent != null) {
        g = parent.getGroupId();
    }
    if (v == null && parent != null) {
        v = parent.getVersion();
    }
    boolean changed = false;
    Map<ProjectVersionRef, String> versionsByGAV = state.getVersionsByGAVMap();
    // If the parent version is not defined, it will be taken automatically from the project version
    if (parent != null && parent.getVersion() != null) {
        final ProjectVersionRef parentGAV = new SimpleProjectVersionRef(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
        if (versionsByGAV.containsKey(parentGAV)) {
            final String newVersion = versionsByGAV.get(parentGAV);
            logger.debug("Changed parent version to: " + newVersion + " in " + parent);
            if (parentGAV.getVersionString().startsWith("${")) {
                if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(parentGAV.getVersionString()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
                    logger.error("Unable to find property {} to update with version {}", parentGAV.getVersionString(), newVersion);
                }
            } else {
                parent.setVersion(newVersion);
            }
            changed = true;
        }
    }
    ProjectVersionRef gav = new SimpleProjectVersionRef(g, model.getArtifactId(), v);
    if (model.getVersion() != null) {
        final String newVersion = versionsByGAV.get(gav);
        logger.info("Looking for new version: " + gav + " (found: " + newVersion + ")");
        if (newVersion != null && model.getVersion() != null) {
            if (gav.getVersionString().startsWith("${")) {
                if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(gav.getVersionString()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
                    logger.error("Unable to find property {} to update with version {}", gav.getVersionString(), newVersion);
                }
            } else {
                model.setVersion(newVersion);
            }
            logger.info("Changed main version in " + gav(model));
            changed = true;
        }
    } else // force inject the new version.
    if (changed == false && model.getVersion() == null && project.isInheritanceRoot()) {
        final String newVersion = versionsByGAV.get(gav);
        logger.info("Looking to force inject new version for : " + gav + " (found: " + newVersion + ")");
        if (newVersion != null) {
            model.setVersion(newVersion);
            changed = true;
        }
    }
    final Set<ModelBase> bases = new HashSet<>();
    bases.add(model);
    bases.addAll(ProfileUtils.getProfiles(session, model));
    final PropertyInterpolator pi = new PropertyInterpolator(model.getProperties(), project);
    for (final ModelBase base : bases) {
        final DependencyManagement dm = base.getDependencyManagement();
        if (dm != null && dm.getDependencies() != null) {
            for (final Dependency d : dm.getDependencies()) {
                if (isEmpty(pi.interp(d.getVersion()))) {
                    logger.trace("Skipping dependency " + d + " as empty version.");
                    continue;
                }
                try {
                    gav = new SimpleProjectVersionRef(pi.interp(d.getGroupId()), pi.interp(d.getArtifactId()), pi.interp(d.getVersion()));
                    final String newVersion = versionsByGAV.get(gav);
                    if (newVersion != null) {
                        logger.debug("Examining dependency (from depMgmt) {} to change version to {} ", d, newVersion);
                        if (d.getVersion().startsWith("${")) {
                            if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(d.getVersion()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
                                logger.error("Unable to find property {} to update with version {}", d.getVersion(), newVersion);
                            }
                        } else {
                            d.setVersion(newVersion);
                            logger.info("Changed managed: " + d + " in " + base + " to " + newVersion + " from " + gav.getVersionString());
                        }
                        changed = true;
                    }
                } catch (InvalidRefException ire) {
                    logger.debug("Unable to change version for dependency {} due to {} ", d.toString(), ire);
                }
            }
        }
        if (base.getDependencies() != null) {
            for (final Dependency d : base.getDependencies()) {
                try {
                    if (isEmpty(pi.interp(d.getVersion()))) {
                        logger.trace("Skipping dependency " + d + " as empty version.");
                        continue;
                    }
                    gav = new SimpleProjectVersionRef(pi.interp(d.getGroupId()), pi.interp(d.getArtifactId()), pi.interp(d.getVersion()));
                    final String newVersion = versionsByGAV.get(gav);
                    if (newVersion != null && d.getVersion() != null) {
                        logger.debug("Examining dependency {} to change version to {} ", d, newVersion);
                        if (d.getVersion().startsWith("${")) {
                            if (PropertiesUtils.updateProperties(session, project, false, extractPropertyName(d.getVersion()), newVersion) == PropertiesUtils.PropertyUpdate.NOTFOUND) {
                                logger.error("Unable to find property {} to update with version {}", d.getVersion(), newVersion);
                            }
                        } else {
                            d.setVersion(newVersion);
                            logger.info("Changed: " + d + " in " + base + " to " + newVersion + " from " + gav.getVersionString());
                        }
                        changed = true;
                    }
                } catch (InvalidRefException ire) {
                    logger.debug("Unable to change version for dependency {} due to {} ", d.toString(), ire);
                }
            }
        }
    }
    return changed;
}
Also used : Parent(org.apache.maven.model.Parent) InvalidRefException(org.commonjava.maven.atlas.ident.ref.InvalidRefException) ModelBase(org.apache.maven.model.ModelBase) Dependency(org.apache.maven.model.Dependency) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef) PropertyInterpolator(org.commonjava.maven.ext.common.util.PropertyInterpolator) 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) HashSet(java.util.HashSet)

Example 3 with PropertyInterpolator

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

the class PropertyInterpolatorTest method testInteropolateProperties.

@Test
public void testInteropolateProperties() throws Exception {
    final Model model = TestUtils.resolveModelResource(RESOURCE_BASE, "infinispan-bom-8.2.0.Final.pom");
    Project p = new Project(model);
    Properties props = p.getModel().getProperties();
    boolean containsProperty = false;
    for (Object o : props.values()) {
        if (((String) o).contains("${")) {
            containsProperty = true;
        }
    }
    assertTrue(containsProperty);
    PropertyInterpolator pi = new PropertyInterpolator(props, p);
    assertTrue(pi.interp("${version.hibernate.osgi}").equals("5.0.4.Final"));
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) Properties(java.util.Properties) PropertyInterpolator(org.commonjava.maven.ext.common.util.PropertyInterpolator) Test(org.junit.Test)

Aggregations

Model (org.apache.maven.model.Model)3 PropertyInterpolator (org.commonjava.maven.ext.common.util.PropertyInterpolator)3 Dependency (org.apache.maven.model.Dependency)2 Project (org.commonjava.maven.ext.common.model.Project)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1 ModelBase (org.apache.maven.model.ModelBase)1 Parent (org.apache.maven.model.Parent)1 InvalidRefException (org.commonjava.maven.atlas.ident.ref.InvalidRefException)1 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)1 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)1