0
votes

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>
        </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://www.w3.org/1999/XSL/Transform"
xmlns="http://www.springframework.org/schema/beans"
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://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="    http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  <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 ?

1

1 Answers

1
votes

Use

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.springframework.org/schema/beans"
xpath-default-namespace="http://www.springframework.org/schema/beans"
version="2.0">

assuming you have an XSLT 2.0 processor like Saxon 9. With an XSLT 1.0 processor like Xalan you need

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:sb="http://www.springframework.org/schema/beans"
    version="1.0">

  <xsl:template match="sb:beans">
    <xsl:copy>
      <xsl:apply-templates select="@* | *"/>
      <bean/>
    </xsl:copy>
  </xsl:template>

With your current code the match="beans" only matches elements with local name beans in no namespace.