How to get POM property from org.apache.maven.artifact.Artifact

☆樱花仙子☆ 提交于 2019-12-13 05:29:05

问题


I am traversing a dependency graph of my main pom in a plugin using org.apache.maven.shared.dependency.graph.DependencyGraphBuilder.buildDependencyGraph() and the resulting org.apache.maven.shared.dependency.graph.DependencyNode

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?


回答1:


Let me answer this groovy style .... (using a script written for GMaven)

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.



来源:https://stackoverflow.com/questions/21586479/how-to-get-pom-property-from-org-apache-maven-artifact-artifact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!