我正在使用org.apache.maven.shared.dependency.graph.DependencyGraphBuilder.buildDependencyGraph()和生成的org.apache.maven.shared.dependency.graph.DependencyNode遍历插件中的主pom的依赖关系图。
但是,一旦使用特定的groupId到达依赖项,就需要访问在其pom中声明的maven属性。如何通过工件或DependencyNode对象访问pom?
发布于 2014-10-16 13:59:17
让我回答这个时髦的风格..。(使用为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工件获得。
https://stackoverflow.com/questions/21586479
复制相似问题