如何从org.apache.maven.artifact.Artifact获取POM属性 [英] How to get POM property from org.apache.maven.artifact.Artifact
问题描述
我正在使用org.apache.maven.shared.dependency.graph.DependencyGraphBuilder.buildDependencyGraph()
和生成的org.apache.maven.shared.dependency.graph.DependencyNode
但是,一旦我达到了具有特定groupId的依赖关系,就需要访问在其pom中声明的maven属性.如何通过 Artifact 或 DependencyNode 对象访问pom?
However, once I reach a dependency with a specific groupId I need to access a maven property declared in its pom. How can I access the pom via the Artifact or the DependencyNode object?
推荐答案
让我回答这种时髦的样式....(使用为 GMaven )
Let me answer this groovy style .... (using a script written for GMaven)
我将以webjars.org提供的Javascript依赖示例为例,在其中我想读取(不幸的是,我刚刚发现的)requirejs
属性.
I'll use as an example Javascript dependencies provided by webjars.org in which I want to read the (unfortunatly optional, as I've just discovered) requirejs
property.
/**
* 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
}
Again, I can't emphasize enough on how the access to PlexusContainer
saved day, aside the knowledge that the MavenProjectBuilder
component existed somewhere. Notice this component is deprecated and available through maven-compat artifact.
这篇关于如何从org.apache.maven.artifact.Artifact获取POM属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!