Search in sources :

Example 1 with VersioningState

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

the class ProjectVersioningManipulator method applyChanges.

/**
 * Apply any project versioning changes accumulated in the {@link VersioningState} instance associated with the {@link ManipulationSession} to
 * the list of {@link Project}'s given. This happens near the end of the Maven session-bootstrapping sequence, before the projects are
 * discovered/read by the main Maven build initialization.
 */
@Override
public Set<Project> applyChanges(final List<Project> projects) throws ManipulationException {
    final VersioningState state = session.getState(VersioningState.class);
    if (!session.isEnabled() || state == null || !state.isEnabled()) {
        logger.debug(getClass().getSimpleName() + ": Nothing to do!");
        return Collections.emptySet();
    }
    /*
         * Use the {@link VersionCalculator} to calculate any project version changes, and store them in the {@link VersioningState} that was associated
         * with the {@link ManipulationSession} via the {@link ProjectVersioningManipulator#init(ManipulationSession)}
         */
    logger.info("Version Manipulator: Calculating the necessary versioning changes.");
    state.setVersionsByGAVMap(calculator.calculateVersioningChanges(projects, session));
    final Set<Project> changed = new HashSet<>();
    for (final Project project : projects) {
        if (applyVersioningChanges(project, state)) {
            changed.add(project);
        }
    }
    return changed;
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) HashSet(java.util.HashSet)

Example 2 with VersioningState

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

the class VersioningCalculatorTest method initFailsWithoutSuffixProperty.

@Test
public void initFailsWithoutSuffixProperty() throws Exception {
    final VersioningState session = setupSession(new Properties());
    assertThat(session.isEnabled(), equalTo(false));
}
Also used : VersioningState(org.commonjava.maven.ext.core.state.VersioningState) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with VersioningState

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

the class VersioningCalculatorTest method setupSession.

private VersioningState setupSession(final Properties properties, final Map<ProjectRef, String[]> versionMap) throws Exception {
    final ArtifactRepository ar = new MavenArtifactRepository("test", "http://repo.maven.apache.org/maven2", new DefaultRepositoryLayout(), new ArtifactRepositoryPolicy(), new ArtifactRepositoryPolicy());
    final MavenExecutionRequest req = new DefaultMavenExecutionRequest().setUserProperties(properties).setRemoteRepositories(Arrays.asList(ar));
    final PlexusContainer container = new DefaultPlexusContainer();
    final MavenSession mavenSession = new MavenSession(container, null, req, new DefaultMavenExecutionResult());
    session = new ManipulationSession();
    session.setMavenSession(mavenSession);
    final VersioningState state = new VersioningState(properties);
    session.setState(state);
    final Map<String, byte[]> dataMap = new HashMap<>();
    if (versionMap != null && !versionMap.isEmpty()) {
        for (final Map.Entry<ProjectRef, String[]> entry : versionMap.entrySet()) {
            final String path = toMetadataPath(entry.getKey());
            final byte[] data = setupMetadataVersions(entry.getValue());
            dataMap.put(path, data);
        }
    }
    final Location mdLoc = MavenLocationExpander.EXPANSION_TARGET;
    final Transport mdTrans = new StubTransport(dataMap);
    modder = new TestVersionCalculator(new ManipulationSession(), mdLoc, mdTrans, temp.newFolder("galley-cache"));
    return state;
}
Also used : DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) HashMap(java.util.HashMap) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) StubTransport(org.commonjava.maven.ext.core.fixture.StubTransport) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenSession(org.apache.maven.execution.MavenSession) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ManipulationSession(org.commonjava.maven.ext.core.ManipulationSession) VersioningState(org.commonjava.maven.ext.core.state.VersioningState) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) ProjectRef(org.commonjava.maven.atlas.ident.ref.ProjectRef) Transport(org.commonjava.maven.galley.spi.transport.Transport) StubTransport(org.commonjava.maven.ext.core.fixture.StubTransport) HashMap(java.util.HashMap) Map(java.util.Map) Location(org.commonjava.maven.galley.model.Location)

Example 4 with VersioningState

use of org.commonjava.maven.ext.core.state.VersioningState 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 5 with VersioningState

use of org.commonjava.maven.ext.core.state.VersioningState 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)

Aggregations

VersioningState (org.commonjava.maven.ext.core.state.VersioningState)12 HashSet (java.util.HashSet)4 Project (org.commonjava.maven.ext.common.model.Project)4 HashMap (java.util.HashMap)3 CommonState (org.commonjava.maven.ext.core.state.CommonState)3 Properties (java.util.Properties)2 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)2 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)2 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)2 MavenSession (org.apache.maven.execution.MavenSession)2 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)2 PlexusContainer (org.codehaus.plexus.PlexusContainer)2 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)2 ManipulationSession (org.commonjava.maven.ext.core.ManipulationSession)2 DependencyState (org.commonjava.maven.ext.core.state.DependencyState)2 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1