如何从org.apache.maven.artifact.Artifact获取POM属性

问题描述 投票:0回答:1

我正在使用org.apache.maven.shared.dependency.graph.DependencyGraphBuilder.buildDependencyGraph()和生成的org.apache.maven.shared.dependency.graph.DependencyNode遍历插件中我的主pom的依赖图

但是,一旦我与特定groupId达成依赖关系,我需要访问在其pom中声明的maven属性。如何通过Artifact或DependencyNode对象访问pom?

maven maven-plugin
1个回答
0
投票

让我回答这个时髦的风格....(使用为GMaven编写的脚本)

我将使用webjars.org提供的Javascript依赖关系作为示例,我想要阅读(不幸的是,我刚刚发现的)requirejs属性。

/**
 * Read the requirejs property in pom (if possible) or in file (if not available in pom)
 * @param log the logger injected into the groovy script
 * @param project the MavenProject object (well, not really, but anyway a good implementor)
 * @param session the MavenSession
 * @param artifact the artifact in which we want to read the requirejs property
 */
def readRequireJSPropertyOf(def log, def project, def session, def artifact) {
    // This is the hardest part : the session gives access (through MavenSession#getContainer()) to the PlexusContainer, which allows lookup of various components
    MavenProjectBuilder projectBuilder = session.container.lookup(MavenProjectBuilder.class);
    // Now we have a MavenProjectBuilder, just build a MavenProject object
    MavenProject artifactProject = projectBuilder.buildFromRepository(artifact, project.remoteArtifactRepositories, session.localRepository);
    log.debug "loaded project ${artifactProject}. Now reading its requirejs property"
    // And read that property from artifact POM
    def requireValue = artifactProject.properties["requirejs"];
    return requireValue
}

再说一次,除了知道PlexusContainer组件存在于某个地方之外,我无法强调如何获取MavenProjectBuilder获取的日子。请注意,此组件已弃用,可通过maven-compat工件获得。

© www.soinside.com 2019 - 2024. All rights reserved.