如何使用Ant(或Maven)动态创建jaxb.index文件
这更多的是知识共享,而不是提问。我想这段小小的蚂蚁片段可能对某些人有用如何使用Ant(或Maven)动态创建jaxb.index文件,ant,maven,jaxb2,Ant,Maven,Jaxb2,这更多的是知识共享,而不是提问。我想这段小小的蚂蚁片段可能对某些人有用 <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
<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>
maven antrun插件
1.3
编译
跑
您也可以使用from。继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>
正如您所看到的,我并不是一个蚂蚁专家,我不得不做一些奇怪的事情来创建唯一的属性名称,但这对我很有用。谢谢,这很有用!如果你脱离了框架的预期范围,那么你就进入了一个怪癖和黑客的地狱。有时我觉得我对Java、Maven、Groovy等越来越厌倦了@boumbh很高兴能帮上忙。减少web开发的巨大复杂性的一种方法是尝试其他语言。我注意到现在使用大锤敲打坚果的趋势。JAXB是一个非常有效的大锤,它可以很好地解决我的问题(没有负面含义),但有时我会错过一个简单工具的简单处理。如果使用另一种语言,问题就不同了。不一定更好。正如有人所说:“一个坏工人总是责备他的工具。一个好工人有正确的工具。”如果我不能选择我的工具,那我就是一个坏工人。@boumbh在工作中你是有限的。但是,在工作之外阅读“七周七种语言”会给你带来新的价值。