我正在尝试为Maven enforcer插件编写一个自定义规则,该规则与enforcer插件中包含的RequireSameVersions
规则非常相似。
我想查看项目的所有依赖项,如果它们有自定义属性集,那么我们必须确保所有依赖项中的属性相同(同时忽略没有该属性集的任何依赖项,因为这些是版本无关)。
我面临的问题是,在RequireSameVersions
规则的代码中,工件在API中公开了版本,这样您就可以为每个依赖工件调用artifact.getVersion()
,但是只有似乎可以调用getProperties()
的对象是maven项目本身。
因此,我真正想为自定义规则编写的代码是:
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
Set<String> versions = new HashSet<String>();
MavenProject project = null;
try {
project = (MavenProject) helper.evaluate("${project}");
Set<Artifact> dependentArtifacts = project.getDependencyArtifacts();
for (Artifact artifact : dependentArtifacts) {
Properties childProperties = getProperties(artifact); //TODO: what to put in this method
String versionNumber = childProperties.getProperty("bespoke.version");
if (versionNumber != null) {
versions.add(versionNumber);
}
}
} catch (ExpressionEvaluationException eee) {
throw new EnforcerRuleException(
"Unable to retrieve the MavenProject: ", eee);
}
if (versions.size() > 1) {
// we have more than one version type across child projects, so we
// fail the build.
throw new EnforcerRuleException(
"modules of this project refer to more than one version");
}
}
但是,我不知道在getProperties(artifact)
方法中放什么。这里的任何建议都很棒,谢谢。