我正在按照writing-a-custom-rule创建自定义规则。
目的是使用maven-enforcer-plugin来实现以下目标:
“编写一个自定义规则,说明没有项目版本或 以字符串'master - '“开头的依赖版本。
所以我有以下java类 -
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;
}
}
但该类仅检查项目版本。我也想获得依赖版本。如何让所有版本检查它们是否从“master-”开始?
答案 0 :(得分:1)
您可以通过
获取文物MavenProject project = (MavenProject) helper.evaluate("${project}");
Set<Artifact> artifacts = project.getArtifacts();
通过以下方式检查具有特定组ID的人工制品:
boolean containsInvalid = artifacts.stream()
.filter(item -> item.getGroupId()
.startsWith("master-"))
.anyMatch(item -> isInvalid(item));
isInvalid()
将是您自己的实现。