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

时间:2014-02-05 19:18:47

标签: maven maven-plugin

我使用org.apache.maven.shared.dependency.graph.DependencyGraphBuilder.buildDependencyGraph()遍历插件中主pom的依赖关系图,结果为org.apache.maven.shared.dependency.graph.DependencyNode

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

1 个答案:

答案 0 :(得分: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
}

同样,除了知道MavenProjectBuilder组件存在于某处之外,我无法强调对PlexusContainer访问权限的保存日期。请注意,此组件已弃用,可通过maven-compat工件获取。