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;
}
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));
}
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;
}
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.");
}
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;
}
Aggregations