SweetXML provides a Maven 2 plugin for copying and converting files between XML and SweetXML. This allows you convert any XML file which is bundled into you app at build time to SweetXML, without any code or tools changes. It does take some fiddling in your POM, however.
Typical usage might look like this:
pom.xml |
---|
<plugin> <groupId>net.innig.sweetxml</groupId> <artifactId>sweetxml-maven</artifactId> <executions> <execution> <goals> <goal>convert-resources</goal> </goals> </execution> </executions> </plugin> |
Alternatively, if you set Maven up to recognize SweetXML POM files (see below), the configuration would look like this:
pom.sxml |
---|
plugin groupId: net.innig.sweetxml artifactId: sweetxml-maven executions execution goals goal: convert-resources |
(Ah! Isn't that easier on the eyes?)
The plugin converts SweetXML to XML by default, but can convert in either direction. It converts only files with the appropriate input extension, depending on the conversion mode you choose. By default, it will convert resources in your project's outputDirectory
during the process-resources
phase.
This default behavior is not always sufficient. For example, if you are building a webapp, you may wish to have a web.sxml
instead of a web.xml
. To accomplish this, you will need to filter not only the usual resources, but also the files in your webapp directory. Theis additional conversion requires additional configuration:
pom.sxml |
---|
plugin groupId: net.innig.sweetxml artifactId: sweetxml-maven executions execution # first execution converts classpath resources id: resources goals goal: convert-resources execution # second execution converts webapp resources id: webapp configuration inputDirectory: "${basedir}/src/main/webapp" outputDirectory: "${project.build.directory}/${project.build.finalName}" goals goal: convert-resources |
Note: If you do this, the jetty:run
goal will no longer work, because it looks for unfiltered webapp files in your source directory. You will need to use the jetty:run-exploded
or jetty:run-war
goals instead, both of which use the filtered resources in the build directory.
Other situations may require similarly creative solutions. Maven can be a bit opaque, so don't hesitate to seek help in the SweetXML discussion group. And if you have a solid understanding of Maven, we would love your help! It is quite a boggling beast.
The plugin accepts the following parameters:
Parameter | Description | Default |
---|---|---|
mode | Conversion mode. Possible values are:
|
s2x |
inputDirectory | The directory in which there are files to be converted | project's outputDirectory |
outputDirectory | Determines where the converted files will go. | plugin's inputDirectory |
overwrite | If true, the task will overwrite existing files. If false, the task will skip existing files without failing. | true |
deleteSources | If true, the task will remove the input file after converting it. This option is useful if you are converting files in place in a staging directory. Use with caution! | false |
quiet | Reduces the output of the task to critical error messages. | true |
You may find the command-line interface useful for performing the initial conversion of project files from XML to SweetXML.
Maven does not have native support for SweetXML. However, it is nonetheless possible to use a pom.sxml
instead of a pom.xml
. To do this, you will need to use a wrapper script for Maven which converts your pom.sxml
, then passes control to Maven.
The SweetXML distribution includes a Maven wrapper script for OS X, Linux, and other UNIX-y OSes, in bin/
. (Those of you using MS-DOS are currently on your own; if anybody wants to submit a mvn.bat
wrapper script, it would be quite welcome!)
Place this script in your path in front of Maven's bin directory, and mvn
will automatically use pom.sxml
if it is present. Be careful — if pom.sxml
exists, it will overwrite pom.xml
!
The wrapper script requires that you either have mvn
and sweetxml
in your path, or that you set the environment variables M2_HOME
and SWEETXML_HOME
.