如何外化maven构建文件?

我遇到了如何以XML格式对配置文件进行版本化的问题。 最简单的方法是编写XSLT更新。 应用程序的每个版本都有自己的XSLT更新。 所有这些更新文件都足够小,可以通过IDE管理,特别是它的DIFF工具。

由于该项目已经开发为Maven2 Java逻辑解决方案是通过maven构建文件触发这些更新。

这就是今天应用一组更新的部分:

 org.codehaus.mojo xml-maven-plugin   compile  transform       config/xsltUpdates/input config/xsltUpdates/update1-8-3.xsl config/xsltUpdates/update1-8-3   config/xsltUpdates/update1-8-3 config/xsltUpdates/update1-8-9.xsl config/xsltUpdates/update1-8-9   config/xsltUpdates/update1-8-9 config/xsltUpdates/update1-9-0.xsl config/xsltUpdates/update1-9-0   config/xsltUpdates/update1-9-0 config/xsltUpdates/update1-10-0.xsl config/xsltUpdates/update1-10-0   config/xsltUpdates/update1-10-0 config/xsltUpdates/update1-10-0-1.xsl config/xsltUpdates/update1-10-0-1   config/xsltUpdates/update1-10-0-1 config/xsltUpdates/update1-10-0-2.xsl config/xsltUpdates/update1-10-0-2   config/xsltUpdates/update1-10-0-2 config/xsltUpdates/updateCurrent.xsl config/xsltUpdates/output      net.sf.saxon saxon 8.7    

我想在一些properties / xml文件导入中外化有关transformationSet的信息。 我的pom.xml文件将更清晰,外部化信息更易于维护。

我怎样才能做到这一点?

我可以在构建文件中使用一些迭代控制语句吗? 有没有办法从一些外部文件导入数据?

一些插件允许您使用外部描述符(例如maven-assembly-plugin )。 不幸的是,xml-maven-plugin还不是其中之一。

一种选择是从xml-maven-plugin复制相关目标,并将maven-shared-io的处理从目标转移到目标。 我一直在寻找这样做,以期针对各种插件提出请求以使用描述符文件和LocatorStrategy方法来查找描述符。 这里有一些处理将修改xml-maven-plugin以允许使用描述符。 请注意,所涉及的文件几乎没有validation,因此它有点脆弱,但确实有效。

1)使用以下依赖项创建一个新的maven-plugin项目(称为xml-ext-maven-plugin):

   org.codehaus.mojo xml-maven-plugin 1.0-beta-2   org.apache.maven.plugins maven-assembly-plugin 2.2-beta-2   

2)从xml-maven-plugin复制TransformMojo和AbstractXmlMojo .java文件(您需要父mojo从其javadocinheritance属性)。

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)。 请注意,此实现中几乎没有validation,正确的插件会检查是否设置了值等。

 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配置的forms。 例如,在src / main / resources / transformations1.xml中放入一些转换:

    config/xsltUpdates/input  /stylesheets/update1-8-3.xsl config/xsltUpdates/update1-8-3   config/xsltUpdates/update1-8-3 /stylesheets/update1-8-9.xsl config/xsltUpdates/update1-8-9   

10)将xsl文件放在描述符项目中,例如src / main / resources / stylesheets / update1-8-3.xsl

11)在项目中配置新插件以将描述符项目作为依赖项引用,并将xml文件作为描述符引用:

  org.codehaus.mojo xml-maven-plugin   compile  transform      name.seller.rich xml-ext-test-descriptor 0.0.1      /transformationSet1.xml   

如果以上所有步骤都有效,则执行时自定义插件将从xml-ext-test-descriptor依赖项中解析transformationSet1.xml和xsl文件,并正常处理它们。

可能还有其他方法可以执行此操作,但您可以在父pom中使用pluginManagement部分。

pluginManagement:是一个沿侧插件看到的元素。 插件管理以大致相同的方式包含插件元素,除了不是为此特定项目构建配置插件信息,它旨在配置从此inheritance的项目构建。 但是,这仅配置在子节点的plugins元素中实际引用的插件。 孩子们有权重写pluginManagement定义。

例如:

父项目POM(需要运行mvn install以确保您的子项目可见)

  4.0.0 org.nkl parent pom 0.0.1-SNAPSHOT     org.codehaus.mojo xml-maven-plugin   compile  transform       config/xsltUpdates/input config/xsltUpdates/update1-8-3.xsl config/xsltUpdates/update1-8-3   config/xsltUpdates/update1-8-3 config/xsltUpdates/update1-8-9.xsl config/xsltUpdates/update1-8-9   config/xsltUpdates/update1-8-9 config/xsltUpdates/update1-9-0.xsl config/xsltUpdates/update1-9-0   config/xsltUpdates/update1-9-0 config/xsltUpdates/update1-10-0.xsl config/xsltUpdates/update1-10-0   config/xsltUpdates/update1-10-0 config/xsltUpdates/update1-10-0-1.xsl config/xsltUpdates/update1-10-0-1   config/xsltUpdates/update1-10-0-1 config/xsltUpdates/update1-10-0-2.xsl config/xsltUpdates/update1-10-0-2   config/xsltUpdates/update1-10-0-2 config/xsltUpdates/updateCurrent.xsl config/xsltUpdates/output      net.sf.saxon saxon 8.7        

儿童项目POM

   parent org.nkl 0.0.1-SNAPSHOT  4.0.0 org.nkl child 0.0.1-SNAPSHOT    org.codehaus.mojo xml-maven-plugin