我找到了一些关于 Artifact 检索问题的帖子,但答案似乎不适用于我的具体情况。
我正在编写一个插件来帮助 skinny war EAR一代,我正在运行我为我的 EAR maven 模块编写的插件。在插件代码中,我已经到了我希望能够获得 WAR 依赖项的依赖项/Artifact 的阶段 - 目前这些都没有通过我尝试过的任何东西来实现。我认为这是因为即使在我的 EAR 模块上运行 dependency:tree 也不包含它们,它们不是“传递”依赖项。
// Neither of the two below return WAR transitive dependencies, just the WARs
project.getDependencies()
project.getArtifacts()
目前我的新方法是这样的:
ArtifactRepository localRepository = project.getDistributionManagementArtifactRepository();
List remoteRepositories = project.getRemoteArtifactRepositories();
ResolutionGroup resolutionGroup = artifactMetadataSource.retrieve(warArtifact, localRepository, remoteRepositories);
Set<Artifact> artifactDependencies = resolutionGroup.getArtifacts();
(注意,这是利用项目的 ArtifactMetadataSource
组件对象和依赖项 maven-dependency-plugin:2.4
)
这不 工作。 artifactDependencies
集为空。现在这一定是可能的,因为在模块 warArtifact
的目录中运行 mvn dependency:tree
可以正常工作。
有什么想法吗?
最佳答案
我终于有了答案。诚然,它没有正确回答我最初的问题,但我已经解决了。
它没有完全回答我原来的帖子的原因:它需要 Maven 3。
无论如何,对于那些感兴趣的人来说,这里是:
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.DependencyRequest;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;
import java.util.ArrayList;
import java.util.List;
/**
*
* @goal findShareables
* @phase process-resources
*/
@SuppressWarnings("unchecked")
public class ShareableJarsInWarsExtractor extends AbstractMojo
{
// ...
/**
* The MavenProject object.
*
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;
/**
* The entry point to Aether, i.e. the component doing all the work.
*
* @component
*/
private RepositorySystem repoSystem;
/**
* The current repository/network configuration of Maven.
*
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession repoSession;
/**
* The project's remote repositories to use for the resolution of plugins and their dependencies.
*
* @parameter default-value="${project.remotePluginRepositories}"
* @readonly
*/
private List<RemoteRepository> remoteRepos;
public void execute() throws MojoExecutionException
{
Artifact projectArtifact = getProjectArtifact();
List<Dependency> projectDependencies = getArtifactsDependencies(projectArtifact);
for (Dependency d : projectDependencies)
{
if (d.getArtifact() != null && "war".equals(d.getArtifact().getExtension()))
{
List<Dependency> warDependencies = getArtifactsDependencies(d.getArtifact());
// I now have all of the WAR's dependencies!! Hooray!
// ...
}
}
// ...
}
private List<Dependency> getArtifactsDependencies(Artifact a)
{
List<Dependency> ret = new ArrayList<Dependency>();
// Note: I get the POM artifact, not the WAR or whatever.
DefaultArtifact pomArtifact = new DefaultArtifact(a.getGroupId(), a.getArtifactId(), "pom", a.getVersion());
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(pomArtifact, "compile"));
collectRequest.setRepositories(remoteRepos);
try
{
DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
DependencyRequest projectDependencyRequest = new DependencyRequest(node, null);
repoSystem.resolveDependencies(repoSession, projectDependencyRequest);
PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
node.accept(nlg);
ret.addAll(nlg.getDependencies(true));
}
catch (Exception e)
{
e.printStackTrace();
}
return ret;
}
private Artifact getProjectArtifact()
{
// Todo: There must be a better way!
return new DefaultArtifact(project.getArtifact().toString());
}
// ...
}
我的 POM 定义了以下依赖项:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.0.4</version>
</dependency>
关于java - 使用 Maven 2 API 获取 WAR Artifact 的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364270/