views:

188

answers:

1

Hi,

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>
A: 

You can also use the JAXBIndex plugin from JAXB2 Basics.

lexicore