Resolve Maven POM

Last modified by Andreea Popescu on 2021/03/18 11:28

cogShows how to access any Maven POM data from an XWiki page
TypeSnippet
Category
Developed by

Vincent Massol

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Have you ever tried to access Maven POM data in your Java application? If so you know it's a real pain to try using the Maven 3 in embedded mode. A little bit better is to use Aether but you'll face several issues too: lack of documentation and the fact that Aether only provides access to a very small subset of the Maven POM information...

So here comes the XWiki Extension Module to save the day!

If you wish instead to use XWiki as a Java Library instead and resolve a Maven POM from Java, you could check Vincent's maven-pom-resolution example on GitHub.

Here's a quick example in Velocity to get access to the data from the XWiki Platform v3.4 POM:

{{velocity}}
#set ($model = $services.extension.resolve("org.xwiki.platform:xwiki-platform", "3.4").getProperty("maven.Model"))
Name: $model.name
Description: $model.description
Organization name: $model.organization.name
...
{{velocity}}

This will display:

mavenpom-simple.png

Now this will look for the Maven Artifact in all configured XWiki Extension Repositories.

You can look in any given repository directory with some Groovy code, just a little bit more complex:

{{groovy}}
import com.xpn.xwiki.web.Utils
import org.xwiki.extension.repository.*
import org.xwiki.extension.*

def repositoryFactory =  Utils.getComponent(ExtensionRepositoryFactory, "maven")
def repositoryId = new ExtensionRepositoryId("maven-xwiki", "maven", new URI("http://nexus.xwiki.org/nexus/content/groups/public"))
def repository = repositoryFactory.createRepository(repositoryId)
def extension = repository.resolve(new ExtensionId("org.xwiki.commons:xwiki-commons-component-default", "3.4"))
def model = extension.getProperty("maven.Model")

println "Name: ${model.name}"

model.getDependencies().each() {
  println "* ${it.groupId}:${it.artifactId}:${it.version}"
}
{{/groovy}}

This snippet will generate:

mavenpom.png

Last, if you wish to just reuse an Extension Repository already defined in XWiki's configuration, you can use:

{{groovy}}
import com.xpn.xwiki.web.Utils
import org.xwiki.extension.repository.*
import org.xwiki.extension.*

def repositoryManager = Utils.getComponent(ExtensionRepositoryManager.class)
def repository = repositoryManager.getRepository("maven-xwiki")

def extension = repository.resolve(new ExtensionId("org.xwiki.platform:xwiki-platform", "3.4"))
def model = extension.getProperty("maven.Model")

println model.name
{{/groovy}}

It's that simple! emoticon_smile

Get Connected