플러그인 내에서 Maven의 종속성 계층에 액세스하는 방법
-
18-09-2019 - |
문제
내 플러그인에서는 종속성 계층 구조를 처리하고 각 종속성에 대한 정보 (GroupId, artifactid, 버전 등)를 가져와야합니다. 이것을하는 가장 좋은 방법은 무엇입니까?
해결책
의존성 플러그인에는 다음에 있습니다 나무 목표 그것은이 일의 대부분을합니다. 처리 a MavenProject
사용 DependencyTreeBuilder
, 이것은 a를 반환합니다 DependencyNode
해결 된 종속성 (및 전이 의존성)에 대한 계층 적 정보.
Treemojo에서 많은 코드를 직접 복사 할 수 있습니다. 사용합니다 CollectingDependencyNodeVisitor
나무를 가로 지르고 생산하기 위해 List
모든 노드 중.
당신은 액세스 할 수 있습니다 Artifact
호출하여 노드의 경우 getArtifact()
, 그런 다음 필요에 따라 아티팩트 정보를 얻으십시오. 배제 이유를 얻으려면 DependencyNode
a getState()
int를 반환하는 메소드 종속성이 포함되어 있는지 또는 생략 한 이유가 아닌 경우 (종속성 노드 클래스에 상수가 있습니다.
//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();
}
}
다른 팁
당신은 사용할 수 있습니다 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();
...
}
}
나는 완전히 확실하지 않지만 두 방법 모두의 모음을 반환한다고 생각합니다. 인공물 GroupId, artifactid, 버전 등에 게터를 노출시키는 구현
다음은 모든 종속성을 얻는 방법 (전이 포함)과 파일 자체에 액세스 할 수있는 방법에 대한 최신 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.
}
}
}
모조의 매개 변수를 변경하는 것은 내가 놓친 매우 중요한 부분입니다. 그것 없이는 다음과 같은 선 :
@Parameter(defaultValue = "${project.compileClasspathElements}", readonly = true, required = true)
private List<String> compilePath;
당신이 기대하는 경로가 아니라 클래스 디렉토리 만 반환합니다.
요구 사항 의존성 검색 및 요구 사항을 다른 값으로 변경하면 잡고 싶은 범위를 변경할 수 있습니다. 그만큼 Maven 문서 더 자세한 내용을 제공 할 수 있습니다.
사용하려고 노력하십시오 Aether
유틸리티 클래스 jcabi-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
);
왜 모든 종속성 (직접 및 전이적 종속성)을 되 찾아서 배제를 확인하지 않겠습니까?
@Parameter(property = "project", required = true, readonly = true)
private MavenProject project;
public void execute() throws MojoExecutionException
{
for (Artifact a : project.getArtifacts()) {
if( a.getScope().equals(Artifact.SCOPE_TEST) ) { ... }
if( a.getScope().equals(Artifact.SCOPE_PROVIDED) ) { ... }
if( a.getScope().equals(Artifact.SCOPE_RUNTIME) ) { ... }
}
}
Maven 3 AEther를 사용하십시오. 여기에는 샘플이 있습니다.https://docs.sonatype.org/display/aether/home
Maven 3의 경우 종속성 GraphBuilder를 사용할 수 있습니다. 그것은 종속성 직장기와 거의 동일합니다.
여기 예입니다
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
import org.apache.maven.shared.dependency.graph.DependencyNode;
import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
public class AnanlyzeTransitiveDependencyMojo extends AbstractMojo{
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;
@Component(hint="maven3")
private DependencyGraphBuilder dependencyGraphBuilder;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
// If you want to filter out certain dependencies.
ArtifactFilter artifactFilter = new IncludesArtifactFilter("groupId:artifactId:version");
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(project);
try{
DependencyNode depenGraphRootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, artifactFilter);
CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
depenGraphRootNode.accept(visitor);
List<DependencyNode> children = visitor.getNodes();
getLog().info("CHILDREN ARE :");
for(DependencyNode node : children) {
Artifact atf = node.getArtifact();
}
}catch(Exception e) {
e.printStackTrace();
}