Skip to content

Commit

Permalink
[MRELEASE-1022] Use versions defined in release.properties when doi…
Browse files Browse the repository at this point in the history
…ng `release:prepare` (#111)

this also works with version passed as arguments on the command line.
  • Loading branch information
sleberrigaud authored Apr 26, 2022
1 parent f3f5c07 commit eb2ac92
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class MapVersionsPhase
*/
private Map<String, VersionPolicy> versionPolicies;

void setConvertToSnapshot( boolean convertToSnapshot )
{
this.convertToSnapshot = convertToSnapshot;
}
void setPrompter( Prompter prompter )
{
this.prompter = prompter;
Expand Down Expand Up @@ -138,8 +142,13 @@ else if ( releaseDescriptor.isBranchCreation() && convertToBranch )

if ( convertToSnapshot )
{
String subProjectNextVersion = releaseDescriptor.getProjectDevelopmentVersion( subProjectId );
String v;
if ( ArtifactUtils.isSnapshot( subProject.getVersion() ) )
if ( subProjectNextVersion != null )
{
v = subProjectNextVersion;
}
else if ( ArtifactUtils.isSnapshot( subProject.getVersion() ) )
{
v = nextVersion;
}
Expand All @@ -159,7 +168,15 @@ else if ( releaseDescriptor.isBranchCreation() && convertToBranch )
}
else
{
releaseDescriptor.addReleaseVersion( subProjectId, nextVersion );
String subProjectNextVersion = releaseDescriptor.getProjectReleaseVersion( subProjectId );
if ( subProjectNextVersion != null )
{
releaseDescriptor.addReleaseVersion( subProjectId, subProjectNextVersion );
}
else
{
releaseDescriptor.addReleaseVersion( subProjectId, nextVersion );
}
}
}
}
Expand Down Expand Up @@ -332,22 +349,36 @@ private String resolveSuggestedVersion( String baseVersion, String policyId )

private String getDevelopmentVersion( String projectId, ReleaseDescriptor releaseDescriptor )
{
String defaultVersion = releaseDescriptor.getDefaultDevelopmentVersion();
if ( StringUtils.isEmpty( defaultVersion ) )
String projectVersion = releaseDescriptor.getProjectDevelopmentVersion( projectId );

if ( StringUtils.isEmpty( projectVersion ) )
{
defaultVersion = releaseDescriptor.getProjectDevelopmentVersion( projectId );
projectVersion = releaseDescriptor.getDefaultDevelopmentVersion();
}
return defaultVersion;

if ( StringUtils.isEmpty( projectVersion ) )
{
return null;
}

return projectVersion;
}

private String getReleaseVersion( String projectId, ReleaseDescriptor releaseDescriptor )
{
String nextVersion = releaseDescriptor.getDefaultReleaseVersion();
if ( StringUtils.isEmpty( nextVersion ) )
String projectVersion = releaseDescriptor.getProjectReleaseVersion( projectId );

if ( StringUtils.isEmpty( projectVersion ) )
{
nextVersion = releaseDescriptor.getProjectReleaseVersion( projectId );
projectVersion = releaseDescriptor.getDefaultReleaseVersion();
}
return nextVersion;

if ( StringUtils.isEmpty( projectVersion ) )
{
return null;
}

return projectVersion;
}


Expand Down Expand Up @@ -387,7 +418,7 @@ public ReleaseResult simulate( ReleaseDescriptor releaseDescriptor, ReleaseEnvir

return result;
}

private ResourceBundle getResourceBundle( Locale locale )
{
return ResourceBundle.getBundle( "release-messages", locale, MapVersionsPhase.class.getClassLoader() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -253,6 +254,97 @@ public void testMapReleaseVersionsNonInteractiveWithExplicitVersion()
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectReleaseVersion("groupId:artifactId") );
}

/**
* MRELEASE-1022: don't ignore command line (or release.properties) versions when auto-versioning sub-modules
*/
@Test
public void testMapReleaseVersionsForSubModuleWithExplicitVersion()
throws Exception
{
// prepare
MavenProject rootProject = createProject("rootArtifactId", "SNAPSHOT");
rootProject.setExecutionRoot(true);

final MavenProject moduleProject = createProject("artifactId", "SNAPSHOT");
moduleProject.setParent(rootProject);

List<MavenProject> reactorProjects = Arrays.asList( rootProject, moduleProject );

MapVersionsPhase phase = (MapVersionsPhase) lookup( ReleasePhase.class, TEST_MAP_RELEASE_VERSIONS );

ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder()
.setInteractive(false) // batch mode
.setAutoVersionSubmodules( true )
.addReleaseVersion( "groupId:artifactId", "2.0" );

phase.setPrompter( mockPrompter );

// execute
phase.execute( ReleaseUtils.buildReleaseDescriptor( builder ), new DefaultReleaseEnvironment(), reactorProjects );

// verify
assertEquals( "Check mapped versions", "1.0",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectReleaseVersion("groupId:rootArtifactId") );
assertEquals( "Check mapped versions", "2.0",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectReleaseVersion("groupId:artifactId") );

// execute
phase.simulate( ReleaseUtils.buildReleaseDescriptor( builder ), new DefaultReleaseEnvironment(), reactorProjects );

// verify
assertEquals( "Check mapped versions", "1.0",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectReleaseVersion("groupId:rootArtifactId") );
assertEquals( "Check mapped versions", "2.0",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectReleaseVersion("groupId:artifactId") );
}

/**
* MRELEASE-1022: don't ignore command line (or release.properties) versions when auto-versioning sub-modules
*/
@Test
public void testMapDevelopmentVersionsForSubModuleWithExplicitVersion()
throws Exception
{
// prepare
MavenProject rootProject = createProject("rootArtifactId", "1.0");
rootProject.setExecutionRoot(true);

final MavenProject moduleProject = createProject("artifactId", "1.0");
moduleProject.setParent(rootProject);

List<MavenProject> reactorProjects = Arrays.asList( rootProject, moduleProject );

MapVersionsPhase phase = (MapVersionsPhase) lookup( ReleasePhase.class, TEST_MAP_RELEASE_VERSIONS );

ReleaseDescriptorBuilder builder = new ReleaseDescriptorBuilder()
.setInteractive( false ) // batch mode
.setAutoVersionSubmodules( true )
.setDefaultDevelopmentVersion( "1.1-SNAPSHOT" )
.addDevelopmentVersion( "groupId:artifactId", "2.0-SNAPSHOT" );

phase.setConvertToSnapshot( true );
phase.setPrompter( mockPrompter );


// execute
phase.execute( ReleaseUtils.buildReleaseDescriptor( builder ), new DefaultReleaseEnvironment(), reactorProjects );

// verify
assertEquals( "Check mapped versions", "1.1-SNAPSHOT",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectDevelopmentVersion("groupId:rootArtifactId") );
assertEquals( "Check mapped versions", "2.0-SNAPSHOT",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectDevelopmentVersion("groupId:artifactId") );

// execute
phase.simulate( ReleaseUtils.buildReleaseDescriptor( builder ), new DefaultReleaseEnvironment(), reactorProjects );

// verify
assertEquals( "Check mapped versions", "1.1-SNAPSHOT",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectDevelopmentVersion("groupId:rootArtifactId") );
assertEquals( "Check mapped versions", "2.0-SNAPSHOT",
ReleaseUtils.buildReleaseDescriptor( builder ).getProjectDevelopmentVersion("groupId:artifactId") );
}

@Test
public void testExecuteSnapshotNonInteractive_MapRelease()
throws Exception
Expand Down Expand Up @@ -2131,11 +2223,11 @@ public void testNonExistentVersionPolicy()
builder.setProjectVersionPolicyId( "UNKNOWN" );

// test
ReleaseExecutionException e = assertThrows( ReleaseExecutionException.class,
ReleaseExecutionException e = assertThrows( ReleaseExecutionException.class,
() -> phase.execute( ReleaseUtils.buildReleaseDescriptor( builder ), new DefaultReleaseEnvironment(), reactorProjects ) );
assertThat( e.getCause(), CoreMatchers.instanceOf( PolicyException.class ) );
}

@Test
public void testUpdateBranchInvalidDefaultReleaseVersion_NonInteractive()
throws Exception
Expand All @@ -2160,7 +2252,7 @@ public void testUpdateBranchInvalidDefaultReleaseVersion_NonInteractive()
assertEquals( "3.0 is invalid, expected a snapshot", e.getMessage() );
}
}

@Test
public void testUpdateReleaseInvalidDefaultReleaseVersion_NonInteractive()
throws Exception
Expand All @@ -2184,7 +2276,7 @@ public void testUpdateReleaseInvalidDefaultReleaseVersion_NonInteractive()
assertEquals( "3.0-SNAPSHOT is invalid, expected a non-snapshot", e.getMessage() );
}
}

@Test
public void testUpdateDevelopmentInvalidDefaultDevelopmentVersion_NonInteractive()
throws Exception
Expand Down Expand Up @@ -2217,4 +2309,4 @@ private static MavenProject createProject( String artifactId, String version )
model.setVersion( version );
return new MavenProject( model );
}
}
}

0 comments on commit eb2ac92

Please sign in to comment.