Skip to content

Commit

Permalink
[MRELEASE-1054] Support for excluding submodules changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
wuwen5 committed Jul 19, 2023
1 parent ee11ded commit 8844aea
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
*/
package org.apache.maven.shared.release.phase;

import java.io.File;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.project.MavenProject;
Expand All @@ -39,6 +43,7 @@
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.SelectorUtils;
import org.slf4j.Logger;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -97,6 +102,8 @@ public abstract class AbstractMapVersionsPhase extends AbstractReleasePhase {
*/
private final boolean convertToBranch;

private final Set<String> exclusionPatterns = new HashSet<>();

public AbstractMapVersionsPhase(
ScmRepositoryConfigurator scmRepositoryConfigurator,
Prompter prompter,
Expand All @@ -118,6 +125,15 @@ public ReleaseResult execute(
throws ReleaseExecutionException {
ReleaseResult result = new ReleaseResult();

List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes();

if (additionalExcludes != null) {
for (String additionalExclude : additionalExcludes) {
exclusionPatterns.add(
additionalExclude.replace("\\", File.separator).replace("/", File.separator));
}
}

MavenProject rootProject = ReleaseUtil.getRootProject(reactorProjects);

if (releaseDescriptor.isAutoVersionSubmodules() && ArtifactUtils.isSnapshot(rootProject.getVersion())) {
Expand Down Expand Up @@ -165,17 +181,24 @@ public ReleaseResult execute(
}
}
} else {
URI root = rootProject.getBasedir().toURI();
for (MavenProject project : reactorProjects) {
String projectId = ArtifactUtils.versionlessKey(project.getGroupId(), project.getArtifactId());

String nextVersion = resolveNextVersion(project, projectId, releaseDescriptor, releaseEnvironment);

if (!convertToSnapshot) {
releaseDescriptor.addReleaseVersion(projectId, nextVersion);
} else if (releaseDescriptor.isBranchCreation() && convertToBranch) {
releaseDescriptor.addReleaseVersion(projectId, nextVersion);
} else {
releaseDescriptor.addDevelopmentVersion(projectId, nextVersion);
URI pom = project.getFile().toURI();
final String path = root.relativize(pom).getPath();

if (exclusionPatterns.stream()
.noneMatch(exclusionPattern -> SelectorUtils.matchPath(exclusionPattern, path))) {
if (!convertToSnapshot) {
releaseDescriptor.addReleaseVersion(projectId, nextVersion);
} else if (releaseDescriptor.isBranchCreation() && convertToBranch) {
releaseDescriptor.addReleaseVersion(projectId, nextVersion);
} else {
releaseDescriptor.addDevelopmentVersion(projectId, nextVersion);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;

import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -61,6 +63,7 @@
import org.apache.maven.shared.release.transform.ModelETLRequest;
import org.apache.maven.shared.release.transform.jdom2.JDomModelETLFactory;
import org.apache.maven.shared.release.util.ReleaseUtil;
import org.codehaus.plexus.util.SelectorUtils;
import org.codehaus.plexus.util.StringUtils;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -91,6 +94,8 @@ public abstract class AbstractRewritePomsPhase extends AbstractReleasePhase impl

private long startTime = -1 * 1000;

private final Set<String> exclusionPatterns = new HashSet<>();

protected AbstractRewritePomsPhase(
ScmRepositoryConfigurator scmRepositoryConfigurator,
Map<String, ModelETLFactory> modelETLFactories,
Expand Down Expand Up @@ -142,6 +147,15 @@ public ReleaseResult execute(
throws ReleaseExecutionException, ReleaseFailureException {
ReleaseResult result = new ReleaseResult();

List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes();

if (additionalExcludes != null) {
for (String additionalExclude : additionalExcludes) {
exclusionPatterns.add(
additionalExclude.replace("\\", File.separator).replace("/", File.separator));
}
}

transform(releaseDescriptor, releaseEnvironment, reactorProjects, false, result);

result.setResultCode(ReleaseResult.SUCCESS);
Expand Down Expand Up @@ -199,13 +213,18 @@ private void transform(

for (MavenProject project : reactorProjects) {
URI pom = project.getFile().toURI();
logInfo(
result,
"Transforming " + root.relativize(pom).getPath() + ' '
+ buffer().project(project.getArtifactId()) + " '" + project.getName() + "'"
+ (simulate ? " with ." + getPomSuffix() + " suffix" : "") + "...");
final String path = root.relativize(pom).getPath();

if (exclusionPatterns.stream()
.noneMatch(exclusionPattern -> SelectorUtils.matchPath(exclusionPattern, path))) {
logInfo(
result,
"Transforming " + root.relativize(pom).getPath() + ' '
+ buffer().project(project.getArtifactId()) + " '" + project.getName() + "'"
+ (simulate ? " with ." + getPomSuffix() + " suffix" : "") + "...");

transformProject(project, releaseDescriptor, releaseEnvironment, simulate, result);
transformProject(project, releaseDescriptor, releaseEnvironment, simulate, result);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
package org.apache.maven.shared.release.phase;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmException;
Expand All @@ -41,6 +44,7 @@
import org.apache.maven.shared.release.scm.ReleaseScmRepositoryException;
import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
import org.apache.maven.shared.release.util.ReleaseUtil;
import org.codehaus.plexus.util.SelectorUtils;

import static java.util.Objects.requireNonNull;

Expand All @@ -61,6 +65,8 @@ public abstract class AbstractScmCommitPhase extends AbstractReleasePhase {
*/
protected final String descriptorCommentGetter;

private final Set<String> exclusionPatterns = new HashSet<>();

protected AbstractScmCommitPhase(
ScmRepositoryConfigurator scmRepositoryConfigurator, String descriptorCommentGetter) {
this.scmRepositoryConfigurator = requireNonNull(scmRepositoryConfigurator);
Expand All @@ -77,6 +83,15 @@ public ReleaseResult execute(

validateConfiguration(releaseDescriptor);

List<String> additionalExcludes = releaseDescriptor.getCheckModificationExcludes();

if (additionalExcludes != null) {
for (String additionalExclude : additionalExcludes) {
exclusionPatterns.add(
additionalExclude.replace("\\", File.separator).replace("/", File.separator));
}
}

runLogic(releaseDescriptor, releaseEnvironment, reactorProjects, relResult, false);

relResult.setResultCode(ReleaseResult.SUCCESS);
Expand Down Expand Up @@ -166,9 +181,12 @@ protected void performCheckins(
}
} else {
List<File> pomFiles = createPomFiles(releaseDescriptor, reactorProjects);
ScmFileSet fileSet = new ScmFileSet(new File(releaseDescriptor.getWorkingDirectory()), pomFiles);

checkin(provider, repository, fileSet, releaseDescriptor, message);
if (!pomFiles.isEmpty()) {
ScmFileSet fileSet = new ScmFileSet(new File(releaseDescriptor.getWorkingDirectory()), pomFiles);

checkin(provider, repository, fileSet, releaseDescriptor, message);
}
}
}

Expand Down Expand Up @@ -288,11 +306,20 @@ protected static List<File> createPomFiles(ReleaseDescriptor releaseDescriptor,
* @param reactorProjects a {@link java.util.List} object
* @return a {@link java.util.List} object
*/
protected static List<File> createPomFiles(
ReleaseDescriptor releaseDescriptor, List<MavenProject> reactorProjects) {
protected List<File> createPomFiles(ReleaseDescriptor releaseDescriptor, List<MavenProject> reactorProjects) {
MavenProject rootProject = ReleaseUtil.getRootProject(reactorProjects);
URI root = rootProject.getBasedir().toURI();

List<File> pomFiles = new ArrayList<>();
for (MavenProject project : reactorProjects) {
pomFiles.addAll(createPomFiles(releaseDescriptor, project));

URI pom = project.getFile().toURI();
final String path = root.relativize(pom).getPath();

if (exclusionPatterns.stream()
.noneMatch(exclusionPattern -> SelectorUtils.matchPath(exclusionPattern, path))) {
pomFiles.addAll(createPomFiles(releaseDescriptor, project));
}
}
return pomFiles;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.shared.release.phase;

import java.io.File;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -46,7 +47,10 @@ private static MavenProject createProject(String artifactId, String version) {
model.setGroupId("groupId");
model.setArtifactId(artifactId);
model.setVersion(version);
return new MavenProject(model);

MavenProject mavenProject = new MavenProject(model);
mavenProject.setFile(new File("src/test/resources/pomfinder/pom1.xml"));
return mavenProject;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.maven.shared.release.phase;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -2452,6 +2453,9 @@ private static MavenProject createProject(String artifactId, String version) {
model.setGroupId("groupId");
model.setArtifactId(artifactId);
model.setVersion(version);
return new MavenProject(model);

MavenProject mavenProject = new MavenProject(model);
mavenProject.setFile(new File("src/test/resources/pomfinder/pom1.xml"));
return mavenProject;
}
}

0 comments on commit 8844aea

Please sign in to comment.