Как объединить два pom.xml автор dom4j

#java #xml #maven

#java #xml #maven

Вопрос:

У меня есть два pom.xml исходят из двух похожих проектов. Я должен объединить эти два pom.xml в один и замените им исходные pom-файлы.

Первый pom.xml:

 ...
<dependencies>
    <dependency>xxx</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>zzz</groupId>
            <artifactId>zzz</artifactId>
            <executions>
                <execution>...</execution>
            </executions>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>
...
 

Тот , другой:

 ...
<dependencies>
    <dependency>yyy</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>zzz</groupId>
            <artifactId>zzz</artifactId>
            <configuration>
                <annotationProcessorPaths>...</annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>
...
 

Желаемый результат:

 ...
<dependencies>
    <dependency>xxx</dependency>
    <dependency>yyy</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>zzz</groupId>
            <artifactId>zzz</artifactId>
            <executions>
                <execution>...</execution>
            </executions>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessorPaths>...</annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>
...
 

Я пробовал кодировать с помощью Dom4j, но он работает только с «зависимостями». Я не знаю, как объединить «плагины». Это мой код:

     Document docOfTarget = readXml(outPutFile);
    Document docOfSource = readXml(request.getSrcTemplateFile());
    Element dependenciesOfTarget = docOfTarget.getRootElement().element("dependencies");
    Element dependenciesOfSource = docOfSource.getRootElement().element("dependencies");

    List<Node> targetContents = dependenciesOfTarget.content();
    List<Node> addContents = new LinkedList<>();
    for (Node addContent : dependenciesOfSource.content()) {
        if (includeNode(targetContents, addContent)) {
            continue;
        }
        addContents.add(addContent);
    }
    targetContents.addAll(addContents);
 
     protected final boolean includeNode(List<Node> contents, Node comparatorNode) {
        NodeComparator nodeComparator = new NodeComparator();
        for (Node content : contents) {
            if (nodeComparator.compare(content, comparatorNode) == 0) {
                return true;
            }
        }
        return false;
    }
 

Что мне делать дальше?
Спасибо за вашу помощь!

Комментарии:

1. Почему бы не сделать это вручную?

2. @JFabianMeierj, я пишу инициализатор проекта maven, например start.spring.io чего требует мой коллега. Разница в том, что pom.xml исходят из двух существующих файлов.

3. Код для инициализации проекта существует github.com/spring-io/initializr . ?

4. @khmarbaisek, к сожалению, я не могу использовать это, оно должно основываться на старом проекте.

5. Кроме того, объединенный результат выглядит не очень хорошо. Источник / цель обычно для maven-compiler-plugin, который должен быть установлен через свойства <maven.compiler.target> и / или <maven.compiler.source> , кроме того, я бы прочитал всю модель p om не на основе dom , что намного проще и гибче… таким образом, вы можете программно получить всю информацию и выполнить обработку там .. и, наконец, написать новый pom-файл…