Suppose, you have XML configuration files and depending on build profile you have to modify some parameters (database server address, JMS endpoints, ...). How to do that with Apache Maven 2? Quite easy using ... Apache Ant integration for Apache Maven 2. Apache Ant has excellent and very powerful plug-in to work with XML files - XMLTask. Let us make use of it!
<profiles> <profile> <id>testing</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <dependencies> <dependency> <groupId>com.oopsconsultancy</groupId> <artifactId>xmltask</artifactId> <version>1.14</version> </dependency> </dependencies> <executions> <execution> <phase>prepare-package</phase> <configuration> <tasks> <echo message="Using testing configuration" /> <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpathref="maven.plugin.classpath"/> <xmltask source="${project.basedir}/src/main/webapp/WEB-INF/web.xml" dest="${project.build.directory}/web.xml" preserveType="true"> <remove path="//*[@id='<some id here>']" /> </xmltask> </configuration> </executions> </execution> </plugin> </plugins> </build> </profile> </profiles>
What this simple fragment does: for testing builds, it will remove from web.xml all XML elements with id attribute <some id here>. Not very meaningful but gives the idea how it works. XMLTask could do mostly everything you need: insert/removed elements and XML fragments, insert/remove/modify attributes with values and properties, copy/cut/paste XML, and a lot more. I found it extremely useful.