Как создать файл jaxb.index на лету с помощью Ant (или Maven) - PullRequest
6 голосов
/ 29 июля 2010

Это скорее обмен знаниями, а не вопрос. Думаю, этот маленький фрагмент муравья может быть кому-то полезен.

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

2 голосов
/ 01 марта 2013

Следуя примеру Гэри, я взял и расширил его, чтобы он работал для более чем одного каталога пакетов. Следующее должно работать, если у вас есть зависимость 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 голос
/ 30 июля 2010

Вы также можете использовать плагин JAXBIndex из Основы JAXB2 .

...