我正在尝试编写maven-enforcer规则,该规则检查该项目是否使用依赖关系管理。
但是我很难获得当前项目的pom.xml中编写的版本。我认为DependencyNode#getPremanagedVersion()会提供它,但看起来它正在返回一个由依赖项设置的版本(即log4j在jbossall-client中设置)。
如何在当前的pom.xml中获取依赖项的版本?
package org.example;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
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.project.MavenProject;
import org.apache.maven.shared.dependency.tree.DependencyNode;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
import org.opencredo.maven.plugins.enforcer.utils.DefaultDependencyTreePrinter;
import com.oneandone.access.preclearing.AbstractNonCacheableEnforcerRule;
public class ForceDependencyManagement extends AbstractNonCacheableEnforcerRule implements EnforcerRule {
private String message = "";
private MavenProject project;
private ArtifactRepository localRepository;
private ArtifactFactory artifactFactory;
private ArtifactMetadataSource artifactMetadataSource;
private ArtifactCollector artifactCollector;
private DependencyTreeBuilder dependencyTreeBuilder;
private void init(EnforcerRuleHelper helper) throws EnforcerRuleException {
try {
project = (MavenProject) helper.evaluate("${project}");
localRepository = (ArtifactRepository) helper.evaluate("${localRepository}");
artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class);
artifactCollector = (ArtifactCollector) helper.getComponent(ArtifactCollector.class);
artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class);
dependencyTreeBuilder = (DependencyTreeBuilder) helper.getComponent(DependencyTreeBuilder.class);
} catch (Exception eee) {
throw new EnforcerRuleException("Unable to retrieve the rule dependencies: ", eee);
}
}
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
init(helper);
DependencyNode rootNode = buildDependencyTree();
if (rootNode != null) {
Set found = new HashSet();
Iterator iter = rootNode.iterator();
System.out.println("Dependencies: ");
while (iter.hasNext()) {
DependencyNode node = (DependencyNode) iter.next();
if (StringUtils.isNotEmpty(node.getPremanagedVersion())) {
found.add(node);
}
}
if (found.size() > 0) {
fail(found);
}
}
}
protected DependencyNode buildDependencyTree() {
try {
DependencyNode rootNode =
dependencyTreeBuilder.buildDependencyTree(project, localRepository, artifactFactory,
artifactMetadataSource, null, artifactCollector);
return rootNode;
} catch (Exception e) {
throw new RuntimeException("Failed to build dependency tree", e);
}
}
private void fail(Set found) throws EnforcerRuleException {
Iterator iter;
StringBuffer fullMessage = new StringBuffer();
if (StringUtils.isNotEmpty(message)) {
fullMessage.append(message);
} else {
fullMessage.append("Found artifact without dependency management:");
}
fullMessage.append("\n");
iter = found.iterator();
while (iter.hasNext()) {
DependencyNode node = (DependencyNode) iter.next();
Artifact artifact = node.getArtifact();
fullMessage.append(" " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " is set to version "
+ node.getPremanagedVersion() + "\n");
getTreePrinter().printDependencyTree(fullMessage, artifact, 4);
}
throw new EnforcerRuleException(fullMessage.toString());
}
private DefaultDependencyTreePrinter getTreePrinter() {
return new DefaultDependencyTreePrinter(project, localRepository, artifactFactory, artifactMetadataSource,
artifactCollector, dependencyTreeBuilder);
}
public boolean isCacheable() {
return false;
}
public String getCacheId() {
return null;
}
public boolean isResultValid(EnforcerRule cachedRule) {
return false;
}
}
答案 0 :(得分:1)
这应该这样做
MavenProject project = (MavenProject) helper.evaluate("${project}");
List dependencies = project.getDependencies();
从那里只需遍历依赖项列表并调用.getVersion()
for (int i = 0; i < project.getDependencies().size(); i++) {
String version = project.getDependencies().get(i).getVersion();
}
您还可以从项目对象
获取依赖关系管理信息