Resolve Maven POM
Shows how to access any Maven POM data from an XWiki page |
Type | Snippet |
Category | |
Developed by | |
Rating | |
License | GNU 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!
Here's a quick example in Velocity to get access to the data from the XWiki Platform v3.4 POM:
#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:
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:
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:
Last, if you wish to just reuse an Extension Repository already defined in XWiki's configuration, you can use:
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!