一些插件允许您使用外部描述符(例如maven-assembly-plugin)。不幸的是,xml-maven-plugin 还不是其中之一。
一种选择是从 xml-maven-plugin 复制相关目标,并将处理从 maven-shared-io 硬塞到目标中。我一直在寻求自己这样做,以期针对各种插件提出请求以使用描述符文件和 LocatorStrategy 方法来查找描述符。这是一些将修改 xml-maven-plugin 以允许使用描述符的处理。请注意,所涉及的文件几乎没有验证,因此它有点脆弱,但它确实有效。
1) 使用以下依赖项创建一个新的 maven-plugin 项目(比如称为 xml-ext-maven-plugin):
<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) 从 xml-maven-plugin 复制 TransformMojo 和 AbstractXmlMojo .java 文件(您需要父 mojo 从其 javadoc 继承属性)。
3)向 TransformMojo添加一个描述符属性:
/**
* A list of descriptor files to obtain the transformation sets from
*
* @parameter
*/
private String[] descriptors;
4) 修改execute()方法读取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) 实现readDescriptors()以允许您在类路径或项目内定位描述符(读取器处理主要从程序集插件的 DefaultAssemblyReader 提升)。请注意,此实现中几乎没有验证,适当的插件会检查是否设置了值等。
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) 实现getLocation()以使用各种策略以相对路径、url 或类路径来发现文件。
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) 覆盖asAbsoluteFile()以使用定位器策略解析文件(也允许我们在描述符项目中定义 xsl 文件)。
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) 将插件安装到您的存储库。
9) 创建一个新的 maven 项目来托管您的 transformationSets(比如称为 xml-ext-test-descriptor)。过程与assembly-plugin的共享描述符相同,即创建一个工程,在src/main/resources下添加一些xml文件,然后安装工程。xml 文件采用标准 transformationSets 配置的形式。例如,在 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) 将您的 xsl 文件放入描述符项目中,例如 src/main/resources/stylesheets/update1-8-3.xsl
11)在你的项目中配置新插件,将描述符项目作为依赖引用,将xml文件作为描述符引用:
<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>
如果上述所有步骤都有效,则执行自定义插件时,将从 xml-ext-test-descriptor 依赖项解析 transformationSet1.xml 和您的 xsl 文件,并正常处理它们。