如何使用包含任意自定义类路径的 ANT 构建 WAR
How to build a WAR with ANT including an arbitrary, custom classpath
我必须从 ANT 脚本构建一个 WAR 文件。我已经用由任意库组成的编译类路径声明了一个 fileset
(在下面的示例中,它们只有 2 个。在我的真实情况下,最多 90 个)。我想在 war 中包含 相同的一组 库,而不必在两个地方声明所有库:在 <javac>
和 <war>
任务。
这是我的 ANT 脚本:
<project name="war-with-custom-classpath" basedir=".">
<property environment="env" />
<property name="lib" location="${env.USERPROFILE}/.m2/repository" />
<fileset id="my.classpath" dir="${lib}">
<include name="commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar" />
<include name="commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar" />
</fileset>
<target name="compile">
<javac srcdir="src/test/java" classpathref="my.classpath">
</javac>
</target>
<target name="war">
<delete file="mywar.war" />
<war destfile="mywar.war" needxmlfile="false">
<lib refid="my.classpath">
</lib>
</war>
<!--Read the created war to see its contents-->
<exec command="jar ft mywar.war">
</exec>
</target>
</project>
...但是虽然这些库包含在最终的 war 中,但它们 保留其原始路径 如下所示:
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/lib/
WEB-INF/lib/commons-logging/
WEB-INF/lib/commons-logging/commons-logging/
WEB-INF/lib/commons-logging/commons-logging/1.1.1/
WEB-INF/lib/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
WEB-INF/lib/commons-pool/
WEB-INF/lib/commons-pool/commons-pool/
WEB-INF/lib/commons-pool/commons-pool/1.5.6/
WEB-INF/lib/commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar
在从路径中剥离 JAR 后,是否有任何形式包含它们?像这样:
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/lib/
WEB-INF/lib/commons-logging-1.1.1.jar
WEB-INF/lib/commons-pool-1.5.6.jar
我已经试过了:
<lib refid="my.classpath" prefix="/WEB-INF/lib"/>
...但是结果还是一样
我也试过:
<lib refid="my.classpath" fullpath="WEB-INF/lib"/>
或
<lib refid="my.classpath" fullpath="/WEB-INF/lib"/>
... 并出现错误 Cannot set both fullpath and prefix attributes
.
我也试过了:
<zipfileset refid="my.classpath" fullpath="WEB-INF/lib" />
...但它打开错误 fullpath attribute may only be specified for filesets that specify a single file.
而且我也在 Ant Manual 和 SO 中进行了研究,但到目前为止还没有运气。
考虑使用 <mappedresources>
containing a <chainedmapper>
that. The chain mapper, in turn, contains a <flattenmapper>
followed by a <globmapper>
...
<war destfile="mywar.war" needxmlfile="false">
<mappedresources>
<fileset refid="my.classpath" />
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="WEB-INF/lib/*.jar" />
</chainedmapper>
</mappedresources>
</war>
<mappedresources>
替换 <war>
任务中的 <lib>
。
<flattenmapper>
从 <fileset>
中的文件路径中删除目录。 <globmapper>
将目录添加回去。
<mappedresources>
至少需要 Ant 1.8。
我必须从 ANT 脚本构建一个 WAR 文件。我已经用由任意库组成的编译类路径声明了一个 fileset
(在下面的示例中,它们只有 2 个。在我的真实情况下,最多 90 个)。我想在 war 中包含 相同的一组 库,而不必在两个地方声明所有库:在 <javac>
和 <war>
任务。
这是我的 ANT 脚本:
<project name="war-with-custom-classpath" basedir=".">
<property environment="env" />
<property name="lib" location="${env.USERPROFILE}/.m2/repository" />
<fileset id="my.classpath" dir="${lib}">
<include name="commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar" />
<include name="commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar" />
</fileset>
<target name="compile">
<javac srcdir="src/test/java" classpathref="my.classpath">
</javac>
</target>
<target name="war">
<delete file="mywar.war" />
<war destfile="mywar.war" needxmlfile="false">
<lib refid="my.classpath">
</lib>
</war>
<!--Read the created war to see its contents-->
<exec command="jar ft mywar.war">
</exec>
</target>
</project>
...但是虽然这些库包含在最终的 war 中,但它们 保留其原始路径 如下所示:
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/lib/
WEB-INF/lib/commons-logging/
WEB-INF/lib/commons-logging/commons-logging/
WEB-INF/lib/commons-logging/commons-logging/1.1.1/
WEB-INF/lib/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar
WEB-INF/lib/commons-pool/
WEB-INF/lib/commons-pool/commons-pool/
WEB-INF/lib/commons-pool/commons-pool/1.5.6/
WEB-INF/lib/commons-pool/commons-pool/1.5.6/commons-pool-1.5.6.jar
在从路径中剥离 JAR 后,是否有任何形式包含它们?像这样:
META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/lib/
WEB-INF/lib/commons-logging-1.1.1.jar
WEB-INF/lib/commons-pool-1.5.6.jar
我已经试过了:
<lib refid="my.classpath" prefix="/WEB-INF/lib"/>
...但是结果还是一样
我也试过:
<lib refid="my.classpath" fullpath="WEB-INF/lib"/>
或
<lib refid="my.classpath" fullpath="/WEB-INF/lib"/>
... 并出现错误 Cannot set both fullpath and prefix attributes
.
我也试过了:
<zipfileset refid="my.classpath" fullpath="WEB-INF/lib" />
...但它打开错误 fullpath attribute may only be specified for filesets that specify a single file.
而且我也在 Ant Manual 和 SO 中进行了研究,但到目前为止还没有运气。
考虑使用 <mappedresources>
containing a <chainedmapper>
that. The chain mapper, in turn, contains a <flattenmapper>
followed by a <globmapper>
...
<war destfile="mywar.war" needxmlfile="false">
<mappedresources>
<fileset refid="my.classpath" />
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="WEB-INF/lib/*.jar" />
</chainedmapper>
</mappedresources>
</war>
<mappedresources>
替换 <war>
任务中的 <lib>
。
<flattenmapper>
从 <fileset>
中的文件路径中删除目录。 <globmapper>
将目录添加回去。
<mappedresources>
至少需要 Ant 1.8。