Some plugins allow you to use external descriptors (for example the maven-assembly-plugin). Unfortunately the xml-maven-plugin isn't yet one of them.
One option is to copy the relevant goals from the xml-maven-plugin and shoehorn the processing from maven-shared-io into the goal. I've been looking to do this myself with a view to raising requests against various plugins to use descriptor files and the LocatorStrategy approach to find the descriptors. Here's some processing that will modify the xml-maven-plugin to allow descriptors to be used. Note there is little validation of the files involved so it is a bit fragile as is, but it does work.
1) Create a new maven-plugin project (say called xml-ext-maven-plugin) with the following dependencies:
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0-beta-2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
</dependency>
</dependencies>
2) Copy the TransformMojo and the AbstractXmlMojo .java files from the xml-maven-plugin (you need the parent mojo to inherit the properties from its javadoc).
3) Add a descriptors property to the TransformMojo:
/**
* A list of descriptor files to obtain the transformation sets from
*
* @parameter
*/
private String[] descriptors;
4) modify the execute() method to read the descriptors for transformationSets
public void execute() throws MojoExecutionException, MojoFailureException {
//insert at start of method to resolve transformationSets fronm descriptors
if (descriptors != null && descriptors.length > 0) {
transformationSets = readDescriptors(descriptors);
}
...
5) Implement readDescriptors() to allow you to locate descriptors on the classpath or within the project (the reader processing is largely lifted from the assembly-plugin's DefaultAssemblyReader). Note there is little validation in this implementation, a proper plugin would check if values are set etc.
private TransformationSet[] readDescriptors(String[] descriptors)
throws MojoExecutionException {
List descriptorSets = new ArrayList();
// add all the existing transformationSets
if (transformationSets != null && transformationSets.length != 0) {
descriptorSets.addAll(Arrays.asList(transformationSets));
}
for (int i = 0; i < descriptors.length; i++) {
getLog().info(
"Reading transformation descriptor: " + descriptors[i]);
Location location = getLocation(descriptors[i]);
Reader reader = null;
try {
reader = new InputStreamReader(location.getInputStream(),
"UTF-8");
Xpp3Dom dom = Xpp3DomBuilder.build(reader);
descriptorSets.addAll(parseTransformationSets(dom));
} catch (IOException e) {
throw new MojoExecutionException(
"Error reading transformation descriptor: "
+ descriptors[i], e);
} catch (XmlPullParserException e) {
throw new MojoExecutionException(
"Error parsing transformation descriptor: "
+ descriptors[i], e);
} finally {
IOUtil.close(reader);
}
}
return (TransformationSet[]) descriptorSets
.toArray(new TransformationSet[descriptorSets.size()]);
}
/**
* Create transformationSets from the Xpp3Dom.
* TODO use plexus utilities to resolve these elegantly?
*/
private List parseTransformationSets(Xpp3Dom dom) {
// TODO validation of the input files!
Xpp3Dom[] setDoms = dom.getChildren("transformationSet");
List sets = new ArrayList();
for (int i = 0; i < setDoms.length; i++) {
TransformationSet set = new TransformationSet();
set.setDir(new File(setDoms[i].getChild("dir").getValue()));
set.setStylesheet(new File(setDoms[i].getChild("stylesheet")
.getValue()));
Xpp3Dom outDom = setDoms[i].getChild("outputDir");
if (outDom != null) {
set.setOutputDir(new File(outDom.getValue()));
}
sets.add(set);
}
return sets;
}
6) Implement getLocation() to use various strategies to discover the file either as a relative path, url, or from the classpath.
private Location getLocation(String path) {
List strategies = new ArrayList();
strategies.add(new RelativeFileLocatorStrategy(getBasedir()));
strategies.add(new ClasspathResourceLocatorStrategy());
strategies.add(new FileLocatorStrategy());
strategies.add(new URLLocatorStrategy());
List refStrategies = new ArrayList();
refStrategies.add(classpathStrategy);
Locator locator = new Locator();
locator.setStrategies(strategies);
Location location = locator.resolve(path);
return location;
}
7) Override asAbsoluteFile() to resolve files using the locator strategy (allows us to define the xsl files in the descriptor project as well).
protected File asAbsoluteFile(File f) {
String path = f.getPath();
// ensure we're getting a path in the form that URL can handle
if (path != null) {
path = path.replaceAll("\\\\", "/");
}
Location location = getLocation(path);
if (location == null) {
//can't find the file, let the parent implementation have a try
return super.asAbsoluteFile(f);
}
try {
return location.getFile();
} catch (IOException e) {
throw new RuntimeException("unable to read file " + f.getPath(), e);
}
}
8) Install the plugin to your repository.
9) Create a new maven project to host your transformationSets (say called xml-ext-test-descriptor). the process is the same as for the shared descriptors of the assembly-plugin, i.e. create a project, add some xml files under src/main/resources, and install the project. The xml files are of the form of the standard transformationSets configuration. For example put a couple of the transformations in src/main/resources/transformations1.xml:
<transformationSets>
<transformationSet>
<!--the config directory is in the root of the project -->
<dir>config/xsltUpdates/input</dir>
<!-- the stylesheet can be in the descriptor project-->
<stylesheet>/stylesheets/update1-8-3.xsl</stylesheet>
<outputDir>config/xsltUpdates/update1-8-3</outputDir>
</transformationSet>
<transformationSet>
<dir>config/xsltUpdates/update1-8-3</dir>
<stylesheet>/stylesheets/update1-8-9.xsl</stylesheet>
<outputDir>config/xsltUpdates/update1-8-9</outputDir>
</transformationSet>
</transformationSets>
10) Put your xsl files in the descriptor project, e.g. src/main/resources/stylesheets/update1-8-3.xsl
11) Configure the new plugin in your project to reference the descriptor project as a dependency and reference the xml file as a descriptor:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>name.seller.rich</groupId>
<artifactId>xml-ext-test-descriptor</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<configuration>
<descriptors>
<!-- will be resolved from xml-ext-test-descriptor -->
<descriptor>/transformationSet1.xml</descriptor>
</descriptors>
</plugin>
If all the above steps worked, when executed the custom plugin will resolve transformationSet1.xml and your xsl files from the xml-ext-test-descriptor dependency and process them as normal.