tal*_*ank 36 maven-plugin maven maven-dependency-plugin aether
在我的插件中,我需要处理依赖关系层次结构并获取有关每个依赖关系的信息(groupId,artifactId,version等)以及它是否被排除.做这个的最好方式是什么?
Ric*_*ler 29
依赖插件具有完成大部分工作的树目标.它处理一个MavenProject使用DependencyTreeBuilder,它返回一个DependencyNode关于已解析的依赖关系(及其传递依赖关系)的分层信息.
您可以直接从TreeMojo复制大部分代码.它使用CollectingDependencyNodeVisitor遍历树并生成List所有节点.
您可以Artifact通过调用访问节点getArtifact(),然后根据需要获取工件信息.为了得到排除的原因,DependencyNode有一个getState()方法返回一个int,指示是否已包含依赖项,或者如果没有,省略它的原因是什么(DependencyNode类中有常量来检查返回值)
//All components need this annotation, omitted for brevity
/**
* @component
* @required
* @readonly
*/
private ArtifactFactory artifactFactory;
private ArtifactMetadataSource artifactMetadataSource;
private ArtifactCollector artifactCollector;
private DependencyTreeBuilder treeBuilder;
private ArtifactRepository localRepository;
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
try {
ArtifactFilter artifactFilter = new ScopeArtifactFilter(null);
DependencyNode rootNode = treeBuilder.buildDependencyTree(project,
localRepository, artifactFactory, artifactMetadataSource,
artifactFilter, artifactCollector);
CollectingDependencyNodeVisitor visitor =
new CollectingDependencyNodeVisitor();
rootNode.accept(visitor);
List<DependencyNode> nodes = visitor.getNodes();
for (DependencyNode dependencyNode : nodes) {
int state = dependencyNode.getState();
Artifact artifact = dependencyNode.getArtifact();
if(state == DependencyNode.INCLUDED) {
//...
}
}
} catch (DependencyTreeBuilderException e) {
// TODO handle exception
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
Pas*_*ent 20
您可以使用MavenProject#getDependencyArtifacts()或MavenProject#getDependencies()(后者也返回传递依赖项).
/**
* Test Mojo
*
* @goal test
* @requiresDependencyResolution compile
*/
public class TestMojo extends AbstractMojo {
/**
* The Maven Project.
*
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project = null;
/**
* Execute Mojo.
*
* @throws MojoExecutionException If an error occurs.
* @throws MojoFailureException If an error occurs.
*/
public void execute() throws MojoExecutionException,
MojoFailureException {
...
Set dependencies = project.getDependencies();
...
}
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定,但我认为这两个方法都返回了一个Artifact实现的集合,它们为groupId,artifactId,version等公开getter.
wre*_*ang 14
这是一个关于如何获取所有依赖项(包括传递)以及如何访问文件本身的最新Maven3示例(例如,如果您需要将路径添加到类路径中).
// Default phase is not necessarily important.
// Both requiresDependencyCollection and requiresDependencyResolution are extremely important however!
@Mojo(name = "simple", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class SimpleMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true)
private MavenProject mavenProject;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
for (final Artifact artifact : mavenProject.getArtifacts()) {
// Do whatever you need here.
// If having the actual file (artifact.getFile()) is not important, you do not need requiresDependencyResolution.
}
}
}
Run Code Online (Sandbox Code Playgroud)
更改Mojo中的参数是我失踪的一件非常重要的事情.没有它,行如下:
@Parameter(defaultValue = "${project.compileClasspathElements}", readonly = true, required = true)
private List<String> compilePath;
Run Code Online (Sandbox Code Playgroud)
只返回classes目录,而不是您期望的路径.
将requiresDependencyCollection和requiresDependencyResolution更改为不同的值将允许您更改要捕获的范围.该Maven的文档可以提供更多的细节.
尝试使用jcabi-aether中的Aether实用程序类来获取任何工件的所有依赖项的列表:
File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.RUNTIME
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15417 次 |
| 最近记录: |