Skip to content

Commit

Permalink
[MRELEASE-1078] Conventional Commits Version Policy
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Feb 6, 2023
1 parent 906b6e4 commit 1571e4f
Show file tree
Hide file tree
Showing 31 changed files with 1,815 additions and 6 deletions.
4 changes: 4 additions & 0 deletions maven-release-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ public interface ReleaseDescriptor
*/
String getProjectVersionPolicyId();

/**
* Get the (optional) config for the VersionPolicy implementation used to calculate the project versions.
*
* @return The parsed XML of the provided config (an instance of XmlPlexusConfiguration) or null.
*/
Object getProjectVersionPolicyConfig();

/**
* Get the role-hint for the release Strategy implementation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.repository.ScmRepository;

/**
* <p>VersionPolicyRequest class.</p>
Expand All @@ -33,6 +35,12 @@ public class VersionPolicyRequest

private Metadata metaData;

private ScmRepository scmRepository;
private ScmProvider scmProvider;
private String workingDirectory;

private String config;

/**
* <p>Getter for the field <code>version</code>.</p>
*
Expand Down Expand Up @@ -76,5 +84,93 @@ public VersionPolicyRequest setMetaData( Metadata metaData )
this.metaData = metaData;
return this;
}


/**
* <p>Getter for the field <code>scmRepository</code>.</p>
*
* @return a {@link ScmRepository} object
*/
public ScmRepository getScmRepository()
{
return scmRepository;
}

/**
* <p>Setter for the field <code>scmRepository</code>.</p>
*
* @param scmRepository The {@link ScmRepository} where the history can be retrieved.
* @return a {@link org.apache.maven.shared.release.policy.version.VersionPolicyRequest} object
*/
public VersionPolicyRequest setScmRepository( ScmRepository scmRepository )
{
this.scmRepository = scmRepository;
return this;
}

/**
* <p>Getter for the field <code>scmProvider</code>.</p>
*
* @return a {@link ScmProvider} object
*/
public ScmProvider getScmProvider()
{
return scmProvider;
}

/**
* <p>Setter for the field <code>scmProvider</code>.</p>
*
* @param scmProvider The {@link ScmProvider} where the history can be retrieved.
* @return a {@link org.apache.maven.shared.release.policy.version.VersionPolicyRequest} object
*/
public VersionPolicyRequest setScmProvider( ScmProvider scmProvider )
{
this.scmProvider = scmProvider;
return this;
}

/**
* <p>Getter for the field <code>workingDirectory</code>.</p>
*
* @return the {@link String} that contains the workingDirectory (can be null or empty).
*/
public String getWorkingDirectory()
{
return workingDirectory;
}

/**
* <p>Setter for the field <code>workingDirectory</code>.</p>
*
* @param workingDirectory The {@link String} that contains the workingDirectory (can be null or empty).
* @return a {@link org.apache.maven.shared.release.policy.version.VersionPolicyRequest} object
*/
public VersionPolicyRequest setWorkingDirectory( String workingDirectory )
{
this.workingDirectory = workingDirectory;
return this;
}

/**
* <p>Getter for the field <code>config</code>.</p>
*
* @return the {@link String} that contains the config (can be null or empty).
*/
public String getConfig()
{
return config;
}

/**
* <p>Setter for the field <code>config</code>.</p>
*
* @param config The {@link String} that contains the config (can be null or empty).
* @return a {@link org.apache.maven.shared.release.policy.version.VersionPolicyRequest} object
*/
public VersionPolicyRequest setConfig( String config )
{
this.config = config;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ public VersionParseException( String message )
{
super( message );
}

/**
* <p>Constructor for VersionParseException.</p>
*
* @param message a {@link java.lang.String} object
*/
public VersionParseException( String message, Exception e )
{
super( message, e );
}
}
1 change: 0 additions & 1 deletion maven-release-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ public void write( BuilderReleaseDescriptor config, File file )
{
properties.setProperty( "projectVersionPolicyId", config.getProjectVersionPolicyId() );
}
if ( config.getProjectVersionPolicyConfig() != null )
{
properties.setProperty( "projectVersionPolicyConfig", config.getProjectVersionPolicyConfig().toString() );
}
if ( config.getProjectNamingPolicyId() != null )
{
properties.setProperty( "projectNamingPolicyId", config.getProjectNamingPolicyId() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ public ReleaseDescriptorBuilder setProjectVersionPolicyId( String projectVersion
return this;
}

/**
* <p>setProjectVersionPolicyConfig.</p>
*
* @param setProjectVersionPolicyConfig a {@link java.lang.String} object
* @return a {@link org.apache.maven.shared.release.config.ReleaseDescriptorBuilder} object
*/
public ReleaseDescriptorBuilder setProjectVersionPolicyConfig( String setProjectVersionPolicyConfig )
{
releaseDescriptor.setProjectVersionPolicyConfig( setProjectVersionPolicyConfig );
return this;
}

/**
* <p>setPushChanges.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public static void copyPropertiesToReleaseDescriptor( Properties properties, Rel
{
builder.setProjectVersionPolicyId( properties.getProperty( "projectVersionPolicyId" ) );
}
if ( properties.containsKey( "projectVersionPolicyConfig" ) )
{
builder.setProjectVersionPolicyConfig( properties.getProperty( "projectVersionPolicyConfig" ) );
}
if ( properties.containsKey( "projectNamingPolicyId" ) )
{
builder.setProjectNamingPolicyId( properties.getProperty( "projectNamingPolicyId" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.repository.ScmRepositoryException;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.release.ReleaseExecutionException;
import org.apache.maven.shared.release.ReleaseResult;
import org.apache.maven.shared.release.config.ReleaseDescriptor;
import org.apache.maven.shared.release.env.ReleaseEnvironment;
import org.apache.maven.shared.release.policy.PolicyException;
import org.apache.maven.shared.release.policy.version.VersionPolicy;
import org.apache.maven.shared.release.policy.version.VersionPolicyRequest;
import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
import org.apache.maven.shared.release.util.ReleaseUtil;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.StringUtils;
Expand Down Expand Up @@ -67,9 +75,16 @@
* @author <a href="mailto:[email protected]">Brett Porter</a>
* @author Robert Scholte
*/
@Component( role = ReleasePhase.class, hint = "map-release-versions" )
public abstract class AbstractMapVersionsPhase
extends AbstractReleasePhase
{
/**
* Tool that gets a configured SCM repository from release configuration.
*/
@Requirement
private ScmRepositoryConfigurator scmRepositoryConfigurator;

/**
* Component used to prompt for input.
*/
Expand Down Expand Up @@ -275,14 +290,14 @@ else if ( releaseDescriptor.isBranchCreation() )
{
suggestedVersion =
resolveSuggestedVersion( baseVersion,
releaseDescriptor.getProjectVersionPolicyId() );
releaseDescriptor );
}
catch ( VersionParseException e )
{
if ( releaseDescriptor.isInteractive() )
{
suggestedVersion =
resolveSuggestedVersion( "1.0", releaseDescriptor.getProjectVersionPolicyId() );
resolveSuggestedVersion( "1.0", releaseDescriptor );
}
else
{
Expand Down Expand Up @@ -347,16 +362,42 @@ private String getContextString( ReleaseDescriptor releaseDescriptor )
return "new development";
}

private String resolveSuggestedVersion( String baseVersion, String policyId )
private String resolveSuggestedVersion( String baseVersion, ReleaseDescriptor releaseDescriptor )
throws PolicyException, VersionParseException
{
String policyId = releaseDescriptor.getProjectVersionPolicyId();
VersionPolicy policy = versionPolicies.get( policyId );
if ( policy == null )
{
throw new PolicyException( "Policy '" + policyId + "' is unknown, available: " + versionPolicies.keySet() );
}

VersionPolicyRequest request = new VersionPolicyRequest().setVersion( baseVersion );

if ( releaseDescriptor.getProjectVersionPolicyConfig() != null )
{
request.setConfig( releaseDescriptor.getProjectVersionPolicyConfig().toString() );
}
request.setWorkingDirectory( releaseDescriptor.getWorkingDirectory() );

if ( scmRepositoryConfigurator != null && releaseDescriptor.getScmSourceUrl() != null )
{
try
{
ScmRepository repository = scmRepositoryConfigurator
.getConfiguredRepository( releaseDescriptor, new Settings() );

ScmProvider provider = scmRepositoryConfigurator
.getRepositoryProvider( repository );

request.setScmRepository( repository );
request.setScmProvider( provider );
}
catch ( ScmRepositoryException | NoSuchScmProviderException e )
{
getLogger().warn( "Next Version will NOT be based on the version control: {}", e.getMessage() );
}
}
return convertToSnapshot ? policy.getDevelopmentVersion( request ).getVersion()
: policy.getReleaseVersion( request ).getVersion();
}
Expand Down
9 changes: 9 additions & 0 deletions maven-release-manager/src/main/mdo/release-descriptor.mdo
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,15 @@
The role-hint for the VersionPolicy implementation used to calculate the project versions.
</description>
</field>
<field>
<name>projectVersionPolicyConfig</name>
<version>3.0.0+</version>
<type>DOM</type>
<description>
The optional config string for the VersionPolicy implementation used to calculate the project versions.
The format of this depends on the specific VersionPolicy that is used.
</description>
</field>
<field>
<name>projectNamingPolicyId</name>
<version>3.0.0+</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ private ReleaseDescriptorBuilder createReleaseConfigurationForWriting()
builder.addReleaseVersion( "groupId:artifactId", "1.0" );
builder.addDevelopmentVersion( "groupId:artifactId", "1.1-SNAPSHOT" );

// The actual kind of string you will get when setting the projectVersionPolicyConfig
builder.setProjectVersionPolicyConfig("<projectVersionPolicyConfig><foo>bar</foo></projectVersionPolicyConfig>");

IdentifiedScm scm = new IdentifiedScm();
scm.setId( "id-write" );
scm.setConnection( "connection-write" );
Expand Down
6 changes: 6 additions & 0 deletions maven-release-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.maven.release</groupId>
<artifactId>maven-release-ccsemver-policy</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
Loading

0 comments on commit 1571e4f

Please sign in to comment.