如何使用Ant(或Maven)动态创建jaxb.in​​dex文件

时间:2010-07-29 10:06:30

标签: ant maven jaxb2

这更多是知识共享而不是提问。认为这个小的Ant片段可能对某人有用。

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

好的,好的,所以它不会全面存储并存储所有的包名,以便它可以重建适当的文件结构,但它足以让你开始。

希望它有所帮助。

此外,你可以将这个小片段(少于目标元素)插入到这样的Maven构建中:

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

2 个答案:

答案 0 :(得分:2)

继Gary的例子之后,我接受了它并将其扩展,因此它适用于多个包目录。如果您在插件的依赖项中具有antcontrib依赖项,则以下内容应该有效:

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

我不是一个蚂蚁专家,你可以看到,我不得不做一些奇怪的事情来创建独特的属性名称,但它对我有用。

答案 1 :(得分:1)

您还可以使用JAXBIndex plugin中的JAXB2 Basics