Search in sources :

Example 16 with Project

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

the class RepoAndReportingRemovalManipulator method applyChanges.

/**
 * Apply the reporting and repository removal changes to the list of {@link Project}'s given.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final RepoReportingState state = session.getState(RepoReportingState.class);
    if (!session.isEnabled() || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    final Set<Project> changed = new HashSet<>();
    Settings backupSettings = new Settings();
    Profile backupProfile = new Profile();
    backupProfile.setId("removed-by-pme");
    backupSettings.addActiveProfile("removed-by-pme");
    for (final Project project : projects) {
        final String ga = ga(project);
        logger.debug("Applying changes to: " + ga);
        final Model model = project.getModel();
        Iterator<Repository> it = model.getRepositories().iterator();
        while (it.hasNext()) {
            Repository repository = it.next();
            if (removeRepository(state, repository)) {
                backupProfile.addRepository(repository);
                it.remove();
                changed.add(project);
            }
        }
        it = model.getPluginRepositories().iterator();
        while (it.hasNext()) {
            Repository repository = it.next();
            if (removeRepository(state, repository)) {
                backupProfile.addPluginRepository(repository);
                it.remove();
                changed.add(project);
            }
        }
        if (model.getReporting() != null) {
            backupProfile.setReporting(model.getReporting());
            model.setReporting(null);
            changed.add(project);
        }
        // remove repositories in the profiles as well
        final List<Profile> profiles = ProfileUtils.getProfiles(session, model);
        for (final Profile profile : profiles) {
            Profile repoProfile = new Profile();
            repoProfile.setId(profile.getId());
            it = profile.getRepositories().iterator();
            while (it.hasNext()) {
                Repository repository = it.next();
                if (removeRepository(state, repository)) {
                    repoProfile.addRepository(repository);
                    it.remove();
                    changed.add(project);
                }
            }
            it = profile.getPluginRepositories().iterator();
            while (it.hasNext()) {
                Repository repository = it.next();
                if (removeRepository(state, repository)) {
                    repoProfile.addPluginRepository(repository);
                    it.remove();
                    changed.add(project);
                }
            }
            if (profile.getReporting() != null) {
                repoProfile.setReporting(profile.getReporting());
                profile.setReporting(null);
                changed.add(project);
            }
            if (!repoProfile.getRepositories().isEmpty() || !repoProfile.getPluginRepositories().isEmpty() || repoProfile.getReporting() != null) {
                backupSettings.addProfile(SettingsUtils.convertToSettingsProfile(repoProfile));
            }
        }
    }
    // create new settings file with the removed repositories and reporting
    if (!backupProfile.getRepositories().isEmpty() || !backupProfile.getPluginRepositories().isEmpty() || backupProfile.getReporting() != null) {
        backupSettings.addProfile(SettingsUtils.convertToSettingsProfile(backupProfile));
        String settingsFilePath = state.getRemovalBackupSettings();
        if (!isEmpty(settingsFilePath)) {
            File settingsFile;
            if (settingsFilePath.equals("settings.xml")) {
                String topDir = session.getTargetDir().getParentFile().getPath();
                settingsFile = new File(topDir, settingsFilePath);
            } else {
                settingsFile = new File(settingsFilePath);
            }
            settingsWriter.update(backupSettings, settingsFile);
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Repository(org.apache.maven.model.Repository) Model(org.apache.maven.model.Model) File(java.io.File) RepoReportingState(org.commonjava.maven.ext.core.state.RepoReportingState) Settings(org.apache.maven.settings.Settings) Profile(org.apache.maven.model.Profile) HashSet(java.util.HashSet)

Example 17 with Project

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

the class PropertiesUtilsTest method testUpdateNestedProperties.

@Test
public void testUpdateNestedProperties() throws Exception {
    Project pP = getProject();
    ManipulationSession session = createUpdateSession();
    assertTrue(updateProperties(session, pP, false, "version.hibernate.core", "5.0.4.Final-redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    assertTrue(updateProperties(session, pP, false, "version.scala", "2.11.7.redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    try {
        updateProperties(session, pP, false, "version.scala", "3.11.7-redhat-1");
    } catch (ManipulationException e) {
    // Pass.
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Test(org.junit.Test)

Example 18 with Project

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

the class PropertiesUtilsTest method testCacheProperty.

@Test
public void testCacheProperty() throws Exception {
    Map<Project, Map<String, String>> propertyMap = new HashMap<>();
    CommonState state = new CommonState(new Properties());
    Project project = getProject();
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${foobar}${foobar2}", null, null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "suffix.${foobar}", null, null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, null, "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "1.0", "2.0", null, false));
    assertTrue(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.org.jboss}", "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${project.version}", "2.0", null, false));
    // DependencyManipulator does dependency.getVersion(). This could return e.g. ${version.scala} which can
    // refer to <version.scala>${version.scala.major}.7</version.scala>. If we are attempting to change version.scala
    // to e.g. 2.11.7.redhat-1 then in this case we need to ignore the version.scala.major property and append the .redhat-1.
    // If the property is ${...}.foobar then we only want to append suffix to foobar to change the version
    // However we don't need to change the value of the property. If the property is foobar.${....} then
    // we want to append suffix to the property ... but we need to handle that part of the property is hardcoded.
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.scala}.7", "2.0", null, false));
    assertFalse(PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.foo}.${version.scala}.7", "2.0", null, false));
    try {
        PropertiesUtils.cacheProperty(project, state, propertyMap, "${version.scala}.7.${version.scala2}", "2.0", null, false);
    } catch (ManipulationException e) {
    // Pass.
    }
}
Also used : CommonState(org.commonjava.maven.ext.core.state.CommonState) Project(org.commonjava.maven.ext.common.model.Project) HashMap(java.util.HashMap) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) PropertiesUtils.updateProperties(org.commonjava.maven.ext.core.util.PropertiesUtils.updateProperties) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 19 with Project

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

the class PropertiesUtilsTest method testResolveProperties.

@Test
public void testResolveProperties() throws Exception {
    final Model modelChild = TestUtils.resolveModelResource(RESOURCE_BASE, "inherited-properties.pom");
    final Model modelParent = TestUtils.resolveModelResource(RESOURCE_BASE, "infinispan-bom-8.2.0.Final.pom");
    ManipulationSession session = createUpdateSession();
    Project pP = new Project(modelParent);
    Project pC = new Project(modelChild);
    List<Project> al = new ArrayList<>();
    al.add(pC);
    al.add(pP);
    String result = PropertyResolver.resolveProperties(session, al, "${version.scala.major}.${version.scala.minor}");
    assertTrue(result.equals("2.11.7"));
    result = PropertyResolver.resolveProperties(session, al, "TestSTART.and.${version.scala.major}.now.${version.scala.minor}");
    assertTrue(result.equals("TestSTART.and.2.11.now.7"));
    result = PropertyResolver.resolveProperties(session, al, "${project.version}");
    assertTrue(result.equals("1"));
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) ArrayList(java.util.ArrayList) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) Test(org.junit.Test)

Example 20 with Project

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

the class PropertiesUtilsTest method testUpdateNestedProperties3.

@Test
public void testUpdateNestedProperties3() throws Exception {
    // io.hawt:project:1.4.9
    final Model modelParent = TestUtils.resolveModelResource(RESOURCE_BASE, "project-1.4.9.pom");
    Project pP = new Project(modelParent);
    ManipulationSession session = createUpdateSession();
    assertTrue(updateProperties(session, pP, false, "perfectus-build", "610379.redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    try {
        assertTrue(updateProperties(session, pP, false, "perfectus-build", "610.NOTTHEVALUE.redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    } catch (ManipulationException e) {
        e.printStackTrace();
    // Pass.
    }
    try {
        assertTrue(updateProperties(session, pP, true, "perfectus-build", "610.NOTTHEVALUE.redhat-1") == PropertiesUtils.PropertyUpdate.FOUND);
    } catch (ManipulationException e) {
        e.printStackTrace();
    // Pass.
    }
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) 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