Monday, April 19, 2010

Using Maven 2 and Ant's XMLTask to modify XML files

When we are talking about software development, it's not only about writing a code (for sure, high-quality code). It's also about a bunch of supporting processes like automated building, testing, deployment, integration, ... In this blog I am trying to touch every aspect so this post starts a series of articles about building Java projects with Apache Maven 2. The Maven's web site has very good documentation so I will skip introductory part and concentrate on some practical issues which arrive quite often.

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.

8 comments:

NReddy said...

Hi Andriy

thanks for the info... Its very useful.. May I know which Maven repository holds this xmltask.jar...

Yuriy Ashaev said...

A little bit outdated, but for all who need repo use http://maven.atlassian.com/repository/public. You need to add it in pluginRepositories section of .pom.

Cheers.

Andriy Redko said...

Thanks a lot for update, Yuriy!

Suguna Gollapally said...

using maven-annt xmltask, build is succesful but xml file is not being copied to destination folder with modifications. Downloaded xmltask.jar anda dded to my lcoal maven repository aswell.

Here is the pom.xml content




maven-antrun-plugin


com.oopsconsultancy
xmltask
1.14
local




prepare-package













Suguna Gollapally said...



maven-antrun-plugin


com.oopsconsultancy

xmltask
1.14
local




prepare-package
















Let me know wts wrong

Andriy Redko said...

Hi Suguna,

I would be happy to help with your problem but it's a bit hard to understand your POM file content. Could you please copy / paste the POM in HTML friendly way?

Thank you!

IZAZ NAHA said...

Plugin org.apache.maven.plugins:maven-antrun-plugin:1.3 or one of its dependencies could not be resolved: Failure to find com.oopsconsultancy:xmltask:jar:1.14 in https://repo.maven.apache.org/maven2

I am getting the following problem.
I ma new to maven.Can you please suggest a work around

Andriy Redko said...

Hi IZAZ NAHA,

Many things have changed since the post have been published. Please add the following repository to your pom.xml:

<pluginRepositories>
  <pluginRepository>
    <id>JBoss</id>
    <url>http://maven.wso2.org/nexus/content/repositories/wso2maven2/</url>
  </pluginRepository>
</pluginRepositories>

It should fix your problem.
Thank you!

Best Regards,
Andriy Redko