Search code examples
xmlmavenmaven-2maven-3maven-plugin

Create XML file from Maven


There are many Maven plugins for file manipulation, filtering, copying etc. But is there also a way to create a new XML file from Maven (without creating my own plugin)?

Ideally, I could do something like this:

<build>
    <create-file-xml>
        <file>${basedir}/src/main/resources/my.xml</file>
        <content>
            <xml-root>
                <any-tags>
                   ${any-variable}
                </any-tags>
            </xml-root>
        </content>
    </create-xml-file>
</build>

So that ${basedir}/src/main/resources/my.xml is created with the tags/content under <content>. And my.xml should be available during my test runs (mvn test).


Solution

  • You can try using antrun plugin from Maven. Hope you can customise following code as per your need.

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>add-test-resource</id>
            <phase>generate-test-resources</phase>
            <configuration>
                <target>
                    <echo file="src/test/resources/output.xml" append="true">
                        <![CDATA[<xml-root>
                           hello!!!
                        </xml-root>]]>
                    </echo>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>