sjain
sjain

Reputation: 23344

Custom rule that says there can be no project versions or dependency versions that begin with the string "master-"

I am creating a custom rule by following the writing-a-custom-rule.

The intent is to use maven-enforcer-plugin to accomplish the following objective:

"Writing a custom rule that says there can be no project versions or dependency versions that begin with the string 'master-'".

So I have the following java class-

package com.gridpoint.energy.customRule;


import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.RuntimeInformation;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

/**
 * Custom rules to enforce for maven dependencies
 *
 */
public class CustomRule implements EnforcerRule
{
    /**
     * Simple param. This rule will fail if the value is true.
     */
    private boolean shouldIfail = false;

    public void execute( EnforcerRuleHelper helper )
        throws EnforcerRuleException
    {
        Log log = helper.getLog();

        try
        {
            // get the various expressions out of the helper.
            MavenProject project = (MavenProject) helper.evaluate( "${project}" );
            MavenSession session = (MavenSession) helper.evaluate( "${session}" );
            String target = (String) helper.evaluate( "${project.build.directory}" );
            String artifactId = (String) helper.evaluate( "${project.artifactId}" );
            String version = (String) helper.evaluate( "${project.version}" );

            if(version != null && version.length() > 7 && version.substring(0, 7).equals("master-")) {
                this.shouldIfail = true;
            }

            // retreive any component out of the session directly
            ArtifactResolver resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );
            RuntimeInformation rti = (RuntimeInformation) helper.getComponent( RuntimeInformation.class );

            log.info( "Retrieved Target Folder: " + target );
            log.info( "Retrieved ArtifactId: " +artifactId );
            log.info( "Retrieved Project: " + project );
            log.info( "Retrieved RuntimeInfo: " + rti );
            log.info( "Retrieved Session: " + session );
            log.info( "Retrieved Resolver: " + resolver );

            if ( this.shouldIfail )
            {
                throw new EnforcerRuleException( "Failing because my param said so." );
            }
        }
        catch ( ComponentLookupException e )
        {
            throw new EnforcerRuleException( "Unable to lookup a component " + e.getLocalizedMessage(), e );
        }
        catch ( ExpressionEvaluationException e )
        {
            throw new EnforcerRuleException( "Unable to lookup an expression " + e.getLocalizedMessage(), e );
        }
    }


    public String getCacheId()
    {
        //no hash on boolean...only parameter so no hash is needed.
        return ""+this.shouldIfail;
    }


    public boolean isCacheable()
    {
        return false;
    }


    public boolean isResultValid( EnforcerRule arg0 )
    {
        return false;
    }
}

But that class only checks for project version. I want to get dependencies versions as well. How to get all the versions to check if they start from "master-" ?

Upvotes: 1

Views: 723

Answers (1)

Kirinya
Kirinya

Reputation: 245

You can get the artefacts via

MavenProject project = (MavenProject) helper.evaluate("${project}");
Set<Artifact> artifacts = project.getArtifacts();

And check for the artefacts with certain group id by:

boolean containsInvalid = artifacts.stream()
.filter(item -> item.getGroupId()
.startsWith("master-"))
.anyMatch(item -> isInvalid(item));

isInvalid() would be your own implementation.

Upvotes: 1

Related Questions