6

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

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

2 回答 2

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>

正如你所看到的,我无论如何都不是蚂蚁专家,我不得不做一些奇怪的事情来创建唯一的属性名称,但它对我有用。

于 2013-03-01T08:55:52.667 回答
1

您还可以使用JAXB2 Basics中的JAXBIndex 插件

于 2010-07-30T16:12:35.937 回答