Sunday, 1 February 2015

How to append a new node to existing XML document using maven?



I want to automate adding Spring declarations using Maven, ie appending bean nodes to an existing XML document.


I tried using the AntRun plugin with xmltask with no success: beans.xml is duplicated with no insertion of the bean node element.


I'm now falling back on using maven XML plugin with XSL transformation and still failing to meet my goal with this solution: beans.xml is duplicated with no insertion of the bean node element.


Betting on my lack of knowledge about XML manipulation, I've been reading here and there, but I can't figure out what I'm missing.


I'm using the following POM declaration to configure Maven XML plugin:



<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>${basedir}/src/main/resources/spring</dir>
<excludes>
<exclude>spring-context.xml</exclude>
</excludes>
<stylesheet>${basedir}/templates/xslt/spring-stylesheet.xslt</stylesheet>
<parameters>
<parameter>
<name>resource-name</name>
<value>${resourceName}</value>
</parameter>
</parameters>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>saxon</artifactId>
<version>8.7</version>
</dependency>
</dependencies>
</plugin>


And the following stylesheet to configure XML transformation



<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR"
xmlns="http://ift.tt/GArMu6"
version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="beans">
<xsl:copy>
<xsl:apply-templates select="@* | *"/>
<xsl:element name="bean"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


I'm getting with the following file (which is an exact copy of my source document)



<beans xmlns="http://ift.tt/GArMu6"
xmlns:aop="http://ift.tt/OpNdV1"
xmlns:tx="http://ift.tt/OGfeU2"
xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation=" http://ift.tt/GArMu6 http://ift.tt/QEDs1e http://ift.tt/OGfeU2 http://ift.tt/1cQrvTl http://ift.tt/OpNdV1 http://ift.tt/QEDs1g">
<bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location" value="classpath:properties/db.properties"/>
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
id="sessionFactory">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<value>classpath:hibernate/queries.hbm.xml</value>
</property>
</bean>
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
id="txManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
id="persistenceExceptionTranslationPostProcessor"/>
</beans>


May someone point out what's wrong here, or suggest another way to append to XML resource using Maven ?


No comments:

Post a Comment