Search in sources :

Example 26 with Project

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

the class RepositoryInjectionManipulator method applyChanges.

/**
 * Apply the repository injection changes to the the top level pom.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final RepositoryInjectionState state = session.getState(RepositoryInjectionState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    final Model remoteModel = modelBuilder.resolveRawModel(state.getRemoteRepositoryInjectionMgmt());
    final List<Repository> remoteRepositories = remoteModel.getRepositories();
    final List<Repository> remotePluginRepositories = remoteModel.getPluginRepositories();
    for (final Project project : projects) {
        final String ga = ga(project);
        logger.info("Applying changes to: " + ga);
        final Model model = project.getModel();
        if (checkProject(state, project)) {
            // inject repositories
            final List<Repository> repositories = model.getRepositories();
            if (!remoteRepositories.isEmpty()) {
                final Iterator<Repository> i1 = remoteRepositories.iterator();
                while (i1.hasNext()) {
                    addRepository(repositories, i1.next());
                }
                changed.add(project);
            }
            // inject plugin repositories
            final List<Repository> pluginRepositories = model.getPluginRepositories();
            if (!remotePluginRepositories.isEmpty()) {
                final Iterator<Repository> i2 = remotePluginRepositories.iterator();
                while (i2.hasNext()) {
                    addRepository(pluginRepositories, i2.next());
                }
                changed.add(project);
            }
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Repository(org.apache.maven.model.Repository) Model(org.apache.maven.model.Model) RepositoryInjectionState(org.commonjava.maven.ext.core.state.RepositoryInjectionState) HashSet(java.util.HashSet)

Example 27 with Project

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

the class VersionCalculator method calculateVersioningChanges.

/**
 * Calculate any project version changes for the given set of projects, and return them in a Map keyed by project
 * GA.
 *
 * @param projects the Projects to adjust.
 * @param session the container session.
 * @return a collection of GAV : new Version
 * @throws ManipulationException if an error occurs.
 */
public Map<ProjectVersionRef, String> calculateVersioningChanges(final List<Project> projects, final ManipulationSession session) throws ManipulationException {
    final VersioningState state = session.getState(VersioningState.class);
    final Map<ProjectVersionRef, String> versionsByGAV = new HashMap<>();
    final Set<String> vesionsWithBuildNums = new HashSet<>();
    for (final Project project : projects) {
        String originalVersion = PropertyResolver.resolveInheritedProperties(session, project, project.getVersion());
        String modifiedVersion = calculate(project.getGroupId(), project.getArtifactId(), originalVersion, session);
        if (state.osgi()) {
            modifiedVersion = Version.getOsgiVersion(modifiedVersion);
        }
        versionsByGAV.put(project.getKey(), modifiedVersion);
        if (Version.hasBuildNumber(modifiedVersion)) {
            vesionsWithBuildNums.add(modifiedVersion);
        }
    }
    // between projects in the reactor.
    for (final Project project : projects) {
        final String originalVersion = project.getVersion();
        String modifiedVersion = versionsByGAV.get(project.getKey());
        // This also fixes the problem where there is a single version and leading zeros.
        if (vesionsWithBuildNums.size() > 1) {
            int buildNumber = findHighestMatchingBuildNumber(modifiedVersion, vesionsWithBuildNums);
            // set the build number to avoid version conflicts.
            if (buildNumber > 0) {
                String paddedBuildNum = StringUtils.leftPad(Integer.toString(buildNumber), state.getIncrementalSerialSuffixPadding(), '0');
                modifiedVersion = Version.setBuildNumber(modifiedVersion, paddedBuildNum);
            }
        }
        vesionsWithBuildNums.add(modifiedVersion);
        logger.debug(gav(project) + " has updated version: {}. Marking for rewrite.", modifiedVersion);
        if (!originalVersion.equals(modifiedVersion)) {
            versionsByGAV.put(project.getKey(), modifiedVersion);
        }
    }
    return versionsByGAV;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) HashMap(java.util.HashMap) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) HashSet(java.util.HashSet)

Example 28 with Project

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

the class DistributionEnforcingManipulatorTest method applyTest.

private void applyTest(final EnforcingMode mode, final Model model, final Model expectChanged) throws Exception {
    setModeProperty(mode);
    setMavenSession();
    manipulator.init(session);
    final Project project = new Project(model);
    final List<Project> projects = new ArrayList<>();
    projects.add(project);
    final Set<Project> changed = manipulator.applyChanges(projects);
    if (expectChanged != null) {
        assertThat(changed.isEmpty(), equalTo(false));
        assertThat(changed.contains(new Project(expectChanged)), equalTo(true));
    } else {
        assertThat(changed.isEmpty(), equalTo(true));
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ArrayList(java.util.ArrayList)

Example 29 with Project

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

the class PropertiesUtils method updateProperties.

/**
 * Recursively update properties.
 *
 * @param session the DependencyState
 * @param project the current set of projects we are scanning.
 * @param ignoreStrict whether to ignore strict alignment.
 * @param key a key to look for.
 * @param newValue a value to look for.
 * @return {@code PropertyUpdate} enumeration showing status of any changes.
 * @throws ManipulationException if an error occurs
 */
public static PropertyUpdate updateProperties(ManipulationSession session, Project project, boolean ignoreStrict, String key, String newValue) throws ManipulationException {
    final String resolvedValue = PropertyResolver.resolveProperties(session, project.getInheritedList(), "${" + key + '}');
    logger.debug("Fully resolvedValue is {} for {} ", resolvedValue, key);
    if ("project.version".equals(key)) {
        logger.debug("Not updating key {} with {} ", key, newValue);
        return PropertyUpdate.IGNORE;
    }
    for (final Project p : project.getReverseInheritedList()) {
        if (p.getModel().getProperties().containsKey(key)) {
            logger.trace("Searching properties of {} ", p);
            return internalUpdateProperty(session, p, ignoreStrict, key, newValue, resolvedValue, p.getModel().getProperties());
        } else {
            for (Profile pr : ProfileUtils.getProfiles(session, p.getModel())) {
                logger.trace("Searching properties of profile {} within project {} ", pr.getId(), p);
                // Lets check the profiles for property updates...
                if (pr.getProperties().containsKey(key)) {
                    return internalUpdateProperty(session, p, ignoreStrict, key, newValue, resolvedValue, pr.getProperties());
                }
            }
        }
    }
    return PropertyUpdate.NOTFOUND;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Profile(org.apache.maven.model.Profile)

Example 30 with Project

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

the class XMLManipulator method applyChanges.

/**
 * Apply the xml changes to the specified file(s).
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final XMLState state = session.getState(XMLState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    final List<XMLState.XMLOperation> scripts = state.getXMLOperations();
    for (final Project project : projects) {
        if (project.isExecutionRoot()) {
            for (XMLState.XMLOperation operation : scripts) {
                internalApplyChanges(project, operation);
                changed.add(project);
            }
            break;
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) XMLState(org.commonjava.maven.ext.core.state.XMLState) HashSet(java.util.HashSet)

Aggregations

Project (org.commonjava.maven.ext.common.model.Project)56 Model (org.apache.maven.model.Model)27 Test (org.junit.Test)25 HashSet (java.util.HashSet)21 File (java.io.File)19 HashMap (java.util.HashMap)11 ManipulationSession (org.commonjava.maven.ext.core.ManipulationSession)9 Profile (org.apache.maven.model.Profile)7 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)7 PomIO (org.commonjava.maven.ext.io.PomIO)7 ArrayList (java.util.ArrayList)6 Properties (java.util.Properties)6 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)6 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)6 Map (java.util.Map)5 Dependency (org.apache.maven.model.Dependency)5 SimpleProjectVersionRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)4 DependencyState (org.commonjava.maven.ext.core.state.DependencyState)4 IOException (java.io.IOException)3