I'm using XDoclet to generate both my Hibernate and Spring XML files.
Since the spring.xml file needs to contain a list of the Hibernate XML
files, I decided to write a little glue code to piece it all together.
I wanted to share my approach in case anyone else needed to do the
same thing. First I wrote an xdt file that generates the list of
hibernate XML files to include in the spring.xml, which I called
hibernate_spring_xml.xdt:

<list>
<XDtClass:forAllClasses>
<XDtClass:ifHasClassTag tagName="hibernate.class">
 <value><XDtPackage:packageNameAsPath/>/<XDtClass:className/>.hbm.xml</value>
</XDtClass:ifHasClassTag>
</XDtClass:forAllClasses>
</list>

This template needs to get run when hibernatedoclet runs. I'm using
Maven, so I included a template task when running it. Here is the
relevant portion of the project.properties file:

# Hibernate
maven.xdoclet.hibernatedoclet.destDir=${maven.build.dir}/xdoclet/hibernatedoclet
maven.xdoclet.hibernatedoclet.fileset.0=true
maven.xdoclet.hibernatedoclet.fileset.0.include=**/*.java
maven.xdoclet.hibernatedoclet.hibernate.0=true
maven.xdoclet.hibernatedoclet.hibernate.0.Version=3.0

# Hibernate/Spring glue
maven.xdoclet.hibernatedoclet.template.0=true
maven.xdoclet.hibernatedoclet.template.0.templateFile=template/hibernate_spring_xml.xdt
maven.xdoclet.hibernatedoclet.template.0.destinationFile=hibernate_spring.xml

# Spring
maven.xdoclet.springdoclet.destDir=${maven.build.dir}/xdoclet/springdoclet
maven.xdoclet.springdoclet.fileset.0=true
maven.xdoclet.springdoclet.fileset.0.include=**/*.java
maven.xdoclet.springdoclet.springxml.0=true
maven.xdoclet.springdoclet.springxml.0.mergeDir=${maven.build.dir}/merge

In the Hibernate/Spring glue section, you can see that the template
task will generate a file called hibernate_spring.xml. From there, we
need to get that section of code into spring-beans.xml so that it can
get merged into spring.xml. I have a file called spring-beans.xml.base
that contains a @HIBERNATE_MAPPINGS@ token to be replaced by the
contents of the generated hibernate_spring.xml file:

 ...some beans...

 <bean
   id="sessionFactory"
   class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
   <property name="dataSource"><ref local="dataSource"/></property>
   <property name="mappingResources">
       @HIBERNATE_MAPPINGS@
   </property>
   <property name="hibernateProperties">
     <props>
       <prop key="hibernate.dialect">mydialect</prop>
     </props>
   </property>
 </bean>

In maven.xml, I replace the @HIBERNATE_MAPPINGS@ token, with the
loadfile/copy tasks. Then I just merge in the resulting spring-beans.xml
file:

 <!-- Generate Hibernate and Spring XML files -->
 <preGoal name="java:compile">
   <mkdir dir="${maven.xdoclet.hibernatedoclet.destDir}"/>
   <attainGoal name="xdoclet:hibernatedoclet" />

   <!-- Copy hibernate mapping references into spring-beans.xml -->
   <mkdir dir="${maven.build.dir}/merge"/>

   <loadfile 
srcFile="${maven.build.dir}/xdoclet/hibernatedoclet/hibernate_spring.xml"
property="hibernate.mappings"/>

   <copy file="${basedir}/src/merge/spring-beans.xml.base"
     tofile="${maven.build.dir}/merge/spring-beans.xml">
     <filterset>
       <filter token="HIBERNATE_MAPPINGS" value="${hibernate.mappings}"/>
     </filterset>
   </copy>

   <mkdir dir="${maven.xdoclet.springdoclet.destDir}"/>
   <attainGoal name="xdoclet:springdoclet" />
 </preGoal>

This solution has a lot of moving parts, but it seems to work.
Although I used Maven to do this, it should be pretty easy to
replicate in Ant. If anyone has any improvements/alternatives to this
method, please let me know.

--Steven


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_idt12&alloc_id344&op=click
_______________________________________________
xdoclet-user mailing list
xdoclet-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/xdoclet-user

Reply via email to