Search in sources :

Example 1 with PropertyState

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

the class PropertyManipulator method init.

@Override
public void init(final ManipulationSession session) {
    this.session = session;
    session.setState(new PropertyState(session.getUserProperties()));
}
Also used : PropertyState(org.commonjava.maven.ext.core.state.PropertyState)

Example 2 with PropertyState

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

the class PropertyManipulator method applyChanges.

/**
 * Apply the property changes to the list of {@link Project}'s given.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final PropertyState state = session.getState(PropertyState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Properties overrides = loadRemotePOMProperties(state.getRemotePropertyMgmt());
    final Set<Project> changed = new HashSet<>();
    for (final Project project : projects) {
        final Model model = project.getModel();
        if (!overrides.isEmpty()) {
            // Only inject the new properties at the top level.
            if (project.isInheritanceRoot()) {
                logger.info("Applying property changes to: " + ga(project) + " with " + overrides);
                model.getProperties().putAll(overrides);
                changed.add(project);
            } else {
                // For any matching property that exists in the current project overwrite that value.
                @SuppressWarnings({ "unchecked", "rawtypes" }) final Set<String> keyClone = new HashSet(model.getProperties().keySet());
                keyClone.retainAll(overrides.keySet());
                if (!keyClone.isEmpty()) {
                    final Iterator<String> keys = keyClone.iterator();
                    while (keys.hasNext()) {
                        final String matchingKey = keys.next();
                        logger.info("Overwriting property (" + matchingKey + " in: " + ga(project) + " with value " + overrides.get(matchingKey));
                        model.getProperties().put(matchingKey, overrides.get(matchingKey));
                        changed.add(project);
                    }
                }
            }
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) Properties(java.util.Properties) PropertyState(org.commonjava.maven.ext.core.state.PropertyState) HashSet(java.util.HashSet)

Aggregations

PropertyState (org.commonjava.maven.ext.core.state.PropertyState)2 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 Model (org.apache.maven.model.Model)1 Project (org.commonjava.maven.ext.common.model.Project)1