Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a jaxb.index file on the fly using Ant (or Maven)

Tags:

maven

ant

jaxb2

This is more of knowledge sharing rather than asking a question. Thought this little Ant snippet might be useful to someone.

<target name="create-jaxb-index" depends="compile">
    <!-- Create a suitable jaxb.index file on the fly to remove the need for an ObjectFactory
         jaxb.index is a simple list of the domain objects without package or extension, e.g.
         org.example.Domain.java -> Domain
    -->
    <fileset id="domain-sources" dir="${src}">
      <include name="org/example/*.java"/>
    </fileset>
    <pathconvert property="domain-list" refid="domain-sources" pathsep="${line.separator}">
      <chainedmapper>
        <flattenmapper/>
        <globmapper from="*.java" to="*" casesensitive="false"/>
      </chainedmapper>
    </pathconvert>
    <echo file="${target}/classes/org/example/jaxb.index" message="${domain-list}"/>
  </target>

OK, OK so it doesn't go the whole way and store up all the package names so that it can reconstruct the appropriate file structure, but it's good enough to get you started.

Hope it helps.

Also, you could just insert this little snippet (less the target element) into a Maven build like this:

  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.3</version>
    <executions>
      <execution>
        <phase>compile</phase>
        <configuration>
          <tasks>
              <!-- Create a suitable jaxb.index file on the fly to remove the need for an ObjectFactory
                   jaxb.index is a simple list of the domain objects without package or extension, e.g.
                   org.example.Domain.java -> Domain
              -->
              <fileset id="domain-sources" dir="${build.sourceDirectory}">
                <include name="org/example/domain/*.java"/>
              </fileset>
              <pathconvert property="domain-list" refid="domain-sources" pathsep="${line.separator}">
                <chainedmapper>
                  <flattenmapper/>
                  <globmapper from="*.java" to="*" casesensitive="false"/>
                </chainedmapper>
              </pathconvert>
              <echo file="${build.outputDirectory}/org/example/domain/jaxb.index" message="${domain-list}"/>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
like image 675
Gary Rowe Avatar asked Jul 29 '10 10:07

Gary Rowe


2 Answers

Following on from Gary's example, I took it and extended it so it would work for more than one package directory. The following should work if you have the antcontrib dependency in your plugin's dependencies:

<target>
    <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.dependency.classpath" />
    <for param="dto-dir">
        <path>
            <dirset dir="${basedir}/src/main/java">
                <include name="com/example/**/dto"/>
            </dirset>
        </path>
        <sequential>
            <property name="@{dto-dir}" basedir="${basedir}/src/main/java" relative="true" location="@{dto-dir}" />
            <echo message="Creating jaxb.index file for directory: ${@{dto-dir}}" />
            <echo message="@{dto-dir}" />
            <fileset id="@{dto-dir}_dtos" dir="@{dto-dir}">
                <include name="*Dto.java" />
            </fileset>
            <pathconvert property="@{dto-dir}_contents" refid="@{dto-dir}_dtos" pathsep="${line.separator}">
                <chainedmapper>
                    <flattenmapper />
                    <globmapper from="*.java" to="*" casesensitive="false" />
                </chainedmapper>
            </pathconvert>
            <echo file="${project.build.outputDirectory}/${@{dto-dir}}/jaxb.index" message="${@{dto-dir}_contents}" />                              
        </sequential>
    </for>
</target>

I am not an ant expert by any means as you can see, and I had to do some weird stuff to create unique property names, but it works for me.

like image 80
dansoton Avatar answered Nov 20 '22 04:11

dansoton


You can also use the JAXBIndex plugin from JAXB2 Basics.

like image 1
lexicore Avatar answered Nov 20 '22 03:11

lexicore