Search in sources :

Example 46 with Project

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

the class GroovyManipulator method applyChanges.

/**
 * Apply the groovy script changes to the top level pom.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final GroovyState state = session.getState(GroovyState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    for (File groovyScript : parseGroovyScripts(state.getGroovyScripts())) {
        GroovyShell shell = new GroovyShell();
        Script script;
        for (final Project project : projects) {
            if (project.isExecutionRoot()) {
                logger.info("Executing {} on {}", groovyScript, project);
                try {
                    script = shell.parse(groovyScript);
                    if (script instanceof BaseScript) {
                        ((BaseScript) script).setValues(session.getUserProperties(), projects, project);
                    } else {
                        throw new ManipulationException("Cannot cast " + groovyScript + " to a BaseScript to set values.");
                    }
                } catch (MissingMethodException e) {
                    try {
                        logger.debug("Failure when injecting into script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to inject values into base script", e);
                } catch (CompilationFailedException e) {
                    try {
                        logger.debug("Failure when parsing script {} ", FileUtils.readFileToString(groovyScript), e);
                    } catch (IOException e1) {
                        logger.debug("Unable to read script file {} for debugging! {} ", groovyScript, e1);
                    }
                    throw new ManipulationException("Unable to parse script", e);
                } catch (IOException e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                try {
                    script.run();
                } catch (Exception e) {
                    throw new ManipulationException("Unable to parse script", e);
                }
                changed.add(project);
            }
        }
    }
    return changed;
}
Also used : Script(groovy.lang.Script) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException) GroovyShell(groovy.lang.GroovyShell) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyState(org.commonjava.maven.ext.core.state.GroovyState) Project(org.commonjava.maven.ext.common.model.Project) MissingMethodException(groovy.lang.MissingMethodException) BaseScript(org.commonjava.maven.ext.core.groovy.BaseScript) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) File(java.io.File) HashSet(java.util.HashSet)

Example 47 with Project

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

the class ProfileInjectionManipulator method applyChanges.

/**
 * Apply the profile injection changes to the top level pom.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final ProfileInjectionState state = session.getState(ProfileInjectionState.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.getRemoteProfileInjectionMgmt());
    final List<Profile> remoteProfiles = remoteModel.getProfiles();
    for (final Project project : projects) {
        if (project.isInheritanceRoot()) {
            logger.info("Applying changes to: {} ", ga(project));
            project.updateProfiles(remoteProfiles);
            changed.add(project);
            break;
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) ProfileInjectionState(org.commonjava.maven.ext.core.state.ProfileInjectionState) Profile(org.apache.maven.model.Profile) HashSet(java.util.HashSet)

Example 48 with Project

use of org.commonjava.maven.ext.common.model.Project 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)

Example 49 with Project

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

the class ProjectInheritanceTest method testVerifyInheritanceMultiple.

@Test
public void testVerifyInheritanceMultiple() throws Exception {
    // Locate the PME project pom file. Use that to verify inheritance tracking.
    final File projectroot = new File(TestUtils.resolveFileResource(RESOURCE_BASE, "").getParentFile().getParentFile().getParentFile().getParentFile(), "integration-test/src/it/project-inheritance/pom.xml");
    PomIO pomIO = new PomIO();
    List<Project> projects = pomIO.parseProject(projectroot);
    for (int i = 0; i <= 3; i++) {
        if (i == 0) {
            // First project should be root
            assertTrue(projects.get(i).getProjectParent() == null);
            assertTrue(projects.get(i).getPom().equals(projectroot));
            assertTrue(projects.get(i).getInheritedList().size() == 1);
        } else if (i == 1) {
            List<Project> inherited = projects.get(i).getInheritedList();
            assertTrue(inherited.size() == 2);
            assertTrue(inherited.get(1).getProjectParent().getPom().equals(projectroot));
        } else if (i == 2) {
            List<Project> inherited = projects.get(i).getInheritedList();
            assertTrue(inherited.size() == 3);
            assertTrue(inherited.get(0).getPom().equals(projectroot));
        }
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) List(java.util.List) File(java.io.File) PomIO(org.commonjava.maven.ext.io.PomIO) Test(org.junit.Test)

Example 50 with Project

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

the class ProjectInheritanceTest method testVerifyInheritanceReversedMultiple.

@Test
public void testVerifyInheritanceReversedMultiple() throws Exception {
    // Locate the PME project pom file. Use that to verify inheritance tracking.
    final File projectroot = new File(TestUtils.resolveFileResource(RESOURCE_BASE, "").getParentFile().getParentFile().getParentFile().getParentFile(), "integration-test/src/it/project-inheritance/pom.xml");
    PomIO pomIO = new PomIO();
    List<Project> projects = pomIO.parseProject(projectroot);
    for (int i = 0; i <= 3; i++) {
        if (i == 0) {
            // First project should be root
            assertTrue(projects.get(i).getProjectParent() == null);
            assertTrue(projects.get(i).getPom().equals(projectroot));
            assertTrue(projects.get(i).getReverseInheritedList().size() == 1);
        } else if (i == 1) {
            List<Project> inherited = projects.get(i).getReverseInheritedList();
            assertTrue(inherited.size() == 2);
            assertTrue(inherited.get(0).getProjectParent().getPom().equals(projectroot));
        } else if (i == 2) {
            List<Project> inherited = projects.get(i).getReverseInheritedList();
            assertTrue(inherited.size() == 3);
            assertTrue(inherited.get(2).getPom().equals(projectroot));
        }
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) List(java.util.List) File(java.io.File) PomIO(org.commonjava.maven.ext.io.PomIO) Test(org.junit.Test)

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