【问题标题】:How to get POM property from org.apache.maven.artifact.Artifact如何从 org.apache.maven.artifact.Artifact 获取 POM 属性
【发布时间】:2014-03-02 10:17:31
【问题描述】:

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

但是,一旦我到达具有特定 groupId 的依赖项,我需要访问在其 pom.xml 中声明的 maven 属性。如何通过 ArtifactDependencyNode 对象访问 pom?

【问题讨论】:

  • 你是在写插件还是想达到什么目的?
  • 是的。这就是为什么它有 maven-plugin 标签
  • 我编辑了问题以明确说明

标签: maven maven-plugin


【解决方案1】:

让我来回答这个时髦的风格....(使用为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 工件获得。

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 2023-01-20
    • 2014-11-29
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多