首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从org.apache.maven.artifact.Artifact中获取POM属性

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

Stack Overflow用户
提问于 2014-02-05 19:18:47
回答 1查看 312关注 0票数 0

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

但是,一旦使用特定的groupId到达依赖项,就需要访问在其pom中声明的maven属性。如何通过工件或DependencyNode对象访问pom?

EN

回答 1

Stack Overflow用户

发布于 2014-10-16 13:59:17

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

我将使用webjars.org提供的Javascript依赖项作为示例,在该依赖项中,我希望阅读requirejs属性(不幸的是,正如我刚刚发现的那样)。

代码语言:javascript
复制
/**
 * 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工件获得。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21586479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档