Search in sources :

Example 1 with GAV

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

the class PomIOTest method testGAVReturnPOMs.

@Test
public void testGAVReturnPOMs() throws Exception {
    URL resource = PomIOTest.class.getResource(filename);
    assertNotNull(resource);
    File pom = new File(resource.getFile());
    assertTrue(pom.exists());
    File targetFile = folder.newFile("target.xml");
    FileUtils.copyFile(pom, targetFile);
    Model model = new Model();
    model.setGroupId("org.commonjava.maven.ext.versioning.test");
    model.setArtifactId("dospom");
    model.setVersion("1.0");
    model.setPackaging("pom");
    model.setModelVersion("4.0.0");
    Project p = new Project(targetFile, model);
    p.setExecutionRoot();
    HashSet<Project> changed = new HashSet<>();
    changed.add(p);
    GAV gav = pomIO.rewritePOMs(changed);
    assertTrue(gav.version.equals("1.0"));
    assertTrue(gav.groupId.equals("org.commonjava.maven.ext.versioning.test"));
    assertTrue(gav.artifactId.equals("dospom"));
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) File(java.io.File) GAV(org.commonjava.maven.ext.common.model.GAV) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with GAV

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

the class ManipulationManager method scanAndApply.

/**
 * Encapsulates {@link #applyManipulations(List)}
 *
 * @param session the container session for manipulation.
 * @throws ManipulationException if an error occurs.
 */
public void scanAndApply(final ManipulationSession session) throws ManipulationException {
    final List<Project> projects = pomIO.parseProject(session.getPom());
    session.setProjects(projects);
    for (final Project project : projects) {
        logger.debug("Got " + project + " (POM: " + project.getPom() + ")");
    }
    Set<Project> changed = applyManipulations(projects);
    // Create a marker file if we made some changes to prevent duplicate runs.
    if (!changed.isEmpty()) {
        logger.info("Maven-Manipulation-Extension: Rewrite changed: " + projects);
        GAV gav = pomIO.rewritePOMs(changed);
        try {
            final VersioningState state = session.getState(VersioningState.class);
            state.setExecutionRootModified(gav);
            new File(session.getTargetDir().getParentFile(), ManipulationManager.MARKER_PATH).mkdirs();
            new File(session.getTargetDir().getParentFile(), ManipulationManager.MARKER_FILE).createNewFile();
            try (FileWriter writer = new FileWriter(new File(session.getTargetDir().getParentFile(), RESULT_FILE))) {
                writer.write(collectResults(session));
            }
        } catch (IOException e) {
            logger.error("Unable to create marker or result file", e);
            throw new ManipulationException("Marker/result file creation failed", e);
        }
    }
    // Ensure shutdown of GalleyInfrastructure Executor Service
    for (ExtensionInfrastructure e : infrastructure.values()) {
        e.finish();
    }
    logger.info("Maven-Manipulation-Extension: Finished.");
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) ExtensionInfrastructure(org.commonjava.maven.ext.io.resolver.ExtensionInfrastructure) FileWriter(java.io.FileWriter) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) IOException(java.io.IOException) GAV(org.commonjava.maven.ext.common.model.GAV) File(java.io.File)

Example 3 with GAV

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

the class PomIO method rewritePOMs.

/**
 * For any project listed as changed (tracked by GA in the session), write the modified model out to disk.
 * Uses JDOM {@link ModelWriter} and {@link MavenJDOMWriter} to preserve as much formatting as possible.
 *
 * @param changed the modified Projects to write out.
 * @return gav execution root GAV
 * @throws ManipulationException if an error occurs.
 */
public GAV rewritePOMs(final Set<Project> changed) throws ManipulationException {
    GAV result = null;
    for (final Project project : changed) {
        if (project.isExecutionRoot()) {
            result = new GAV(Project.modelKey(project.getModel()));
        }
        logger.debug(String.format("%s modified! Rewriting.", project));
        File pom = project.getPom();
        final Model model = project.getModel();
        logger.trace("Rewriting: " + model.toString() + " in place of: " + project.getId() + "\n       to POM: " + pom);
        write(project, pom, model);
        // This is a total hack, but the alternative seems to be adding complexity through a custom model processor.
        if (pom.getName().equals("interpolated-pom.xml")) {
            final File dir = pom.getParentFile();
            pom = dir == null ? new File("pom.xml") : new File(dir, "pom.xml");
            write(project, pom, model);
        }
    }
    return result;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) Project(org.commonjava.maven.ext.common.model.Project) Model(org.apache.maven.model.Model) GAV(org.commonjava.maven.ext.common.model.GAV) File(java.io.File)

Aggregations

File (java.io.File)3 GAV (org.commonjava.maven.ext.common.model.GAV)3 Project (org.commonjava.maven.ext.common.model.Project)3 Model (org.apache.maven.model.Model)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 MavenProject (org.apache.maven.project.MavenProject)1 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)1 VersioningState (org.commonjava.maven.ext.core.state.VersioningState)1 ExtensionInfrastructure (org.commonjava.maven.ext.io.resolver.ExtensionInfrastructure)1 Test (org.junit.Test)1