1

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).

cnmuc
  • 6,025
  • 2
  • 24
  • 29
  • Please describe more in detail what kind of problem you have and the solution you have so far? – khmarbaise May 14 '22 at 20:38
  • @khmarbaise i've added some more details... – cnmuc May 15 '22 at 17:03
  • If you want to have that file available in your tests you can simply access that file via classpath (`getClass().getResourcesAsStream("/my.xml")`. You can create that file from within your tests without using a plugin.. What you migt can check https://www.mojohaus.org/xml-maven-plugin/ to transform a file from one to another via xslt... I think the simplest solution would be to generate the file during your tests via test code... ? ?? – khmarbaise May 15 '22 at 19:40
  • To create an xml in `src/main/resources` makes only sense if the contens changes very often...otherwise it would be easier to simply put that file into src/main/resources??? – khmarbaise May 15 '22 at 19:41
  • @khmarbaise thanks. i know there are ways to modify, transform, move etc. existing(!) files. but my question is if there is a way to create a new one. – cnmuc May 15 '22 at 21:59
  • Creating as I wrote directly from your test code...the question: From what source would you like to create xml file? Or just create an XML file? If the latter just use your test code... if you need to generate that for your main code write a plugin it's the cleanest way... can you give a real example how/why you need to create such xml file? – khmarbaise May 15 '22 at 22:56

1 Answers1

0

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>
Rajani B
  • 193
  • 5