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?
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.