Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate an xml file with all dependencies with maven

I need to generate module.xml file for JBoss7 for a maven project which has a lot of jar-dependencies. What is the easiest way to do it? The file looks like:

<module xmlns="urn:jboss:module:1.0" name="ats.platform">
  <resources>
    <resource-root path="dom4j-1.6.1.jar"/>
    <resource-root path="jdom-1.0.jar"/>
...
  </resources>
</module>

so that the <resource-root> element should be created for each project jar-dependency.

Or maybe I doing something wrong? What's correct way to create a JBoss7 module from a maven project?

like image 815
kan Avatar asked Oct 13 '11 13:10

kan


1 Answers

I don't really know about JBoss and whether there's another way to do this, but you can do it quite simply with GMaven:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <version>1.3</version>
    <configuration>
        <source>
            def sw = new StringWriter()
            def xml = new groovy.xml.MarkupBuilder(sw)
            xml.module(xmlns:'urn:jboss:module:1.0', name:'ats.platform') {
              resources {
                project.runtimeClasspathElements.each {
                  def path = it.find(".*?([\\w\\.-]*\\.jar)") { it[1] }
                  !path?:'resource-root'(path:path)
                }
              }
            }
            println sw
        </source>
    </configuration>
</plugin>

A couple of things to note:

  1. That script spits the XML out to stdout, but you can obviously write it to a file or whatever very easily.
  2. The runtimeClasspathElements contain absolute paths to the jar, which is why I parse it with a regex. You can adjust the regex to include more of the path or just prepend a string if you need more than just the jar file name.

I've posted a working example on github (it's just a POM) where I've bound the above plugin configuration to the initialize build phase. If you have git, you can clone and run it yourself with:

git clone git://github.com/zzantozz/testbed tmp
cd tmp
mvn -q initialize -pl stackoverflow/7755255-gmaven-to-build-xml-from-classpath

In the sample project, I added jdom 1.0 and dom4j 1.6.1 as dependencies, and here's the output it created:

<module xmlns='urn:jboss:module:1.0' name='ats.platform'>
  <resources>
    <resource-root path='jdom-1.0.jar' />
    <resource-root path='dom4j-1.6.1.jar' />
    <resource-root path='xml-apis-1.0.b2.jar' />
    <resource-root path='aspectjrt-1.6.11.jar' />
  </resources>
</module>

Note: I'm not a groovy expert, so there may be a groovier way to do it, but you can see how easy it is even so.

like image 145
Ryan Stewart Avatar answered Sep 21 '22 05:09

Ryan Stewart