开发者

Merge XML files with configurable rules (context: Maven POMs, Java)

I'm currently writing some kind of a Maven POM preprocessor that assembles a POM for a project from multiple input files (basically a template and a module specific file). The files are hierarchically ordered (template = most general, module specific = least general). The problem is now to merge these files into a single POM file.

Long story short or if you're not familiar with Maven: a POM looks like this (shortened):

<project>
  <modelVersion>4.0.0</modelVersion>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-core</artifactId>
    </dependency>
  </dependencies>
</project>

Basically the merger shall replace all values of the more general file with the values of the more specific file (i.e. singletons, e.g. the <modelVersion> above) but there are certain elements where the merger shall add the more certain element to a parent (i.e. collections, e.g. <dependency> elements shall always be added to the <dependencies> element and no <dependency> element shall be replaced by another).

A more detailed example with definition of desired output:

File A:

<project>
  <modelVersion>A</modelVersion>
  <dependencies>
    <dependency>
      <groupId>groupIdA</groupId>
      <artifactId>artifactIdA</artifactId>
    </dependency>
  </dependencies>
</project>

File B:

<project>
  <modelVersion>B</modelVersion>
  <dependencies>
    <dependency>
      <groupId>groupIdB</groupId>
      <artifactId>artifactIdB</artifactId>
    </dependency>
  </dependencies>
</project>

Desired output:

<project>
  <modelVersion>B</modelVersion>
  <dependencies>
    <dependency>
      <groupId>groupIdA</groupId>
      <artifactId>artifactIdA</artifactId>
    </dependency>
    <dependency>
      <groupId>groupIdB</groupId>
      <artifactId>artifactIdB</artifactId>
    </dependency>
  </dependencies>
</project>

The set of collection type elements are known and should be configurable (preferably via a set of XPath expressions).

A Java based solution is appreciated.

What looked most promising so far was the tool mentio开发者_StackOverflowned here but the MERGE action produces something like

<dependency>
  <groupId>groupIdAgroupIdB</groupId>
  <artifactId>artifactIdAartifactIdB</artifactId>
</dependency>

when merging, which is not what I need.

Any ideas? Thanks for your help!


I think that goal help:effective-pom [1] works exacly like you want. Even if not, You can look into sources how they did this.

[1] http://maven.apache.org/plugins/maven-help-plugin/effective-pom-mojo.html


I don't have a Java solution, but if you're interested in a Windows tool, Project: Merge will do that just fine. You'll have to tell it which values it should choose (because it wont second guess for two-way merges), but it will do it.

You can set up the format specification for element identification so that it knows to use the character data inside <groupId> to identify <dependency> elements. Once that's done it should work fine.


But isn't that all the purpose of the parent POM? Providing default value for most of used elements?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜