我不能影响将类路径依赖项复制到WEB-INF/lib类别的过程:没有特殊的ANT任务复制这些jar(至少,我找不到任何带有相关"WEB-INF/lib"字符串作为PATH参数的'copy'任务),但它们出现在项目构建之后。如何影响这个程序?基本上,我需要排除JAXB jar以避免依赖冲突。同时,我需要这个jar在编译时,所以我不能删除它们。也许,使用"delete"任务手动删除这些jar更容易?
您的挣扎是多类路径管理。在一个典型的构建中,至少有4种类型的类路径:
- 编译:你的代码直接调用的类
- runtime:你的代码通过其他类间接调用的类
- 提供:需要编译的类,但其实现将由目标平台 提供
- test:测试代码时需要的附加类(如junit),但这些类没有随最终应用程序一起提供
Maven构建工具正式确定了这些常见的类路径,并提供了一个依赖管理系统,用于在构建过程中解析和填充类路径。
坏消息是ANT早于Maven,因此将类路径管理完全交给程序员....通常,这是通过将jar放到不同的目录中或在构建逻辑中使用复杂的文件集来完成的。
好消息是,有一个名为ivy的ANT插件可以执行类似maven的依赖管理。它值得学习,特别是如果你经常使用开源库(现在越来越多地使用Maven)编程的话。
示例(不含ivy)
组成单个类路径的文件必须在构建的顶部进行管理。显然,这些文件必须单独下载到"lib"目录中。随着文件数量的增加,这种方法变得笨拙。
<project name="demo" default="build">
<!--
================
File collections
================
-->
<fileset dir="lib" id="compile.files">
<include name="*.jar"/>
<exclude name="slf4j-log4j12.jar"/>
<exclude name="log4j.jar"/>
<exclude name="junit.jar"/>
<exclude name="hamcrest-core.jar"/>
</fileset>
<fileset dir="lib" id="runtime.files">
<include name="*.jar"/>
<exclude name="junit.jar"/>
<exclude name="hamcrest-core.jar"/>
</fileset>
<fileset dir="lib" id="test.files">
<include name="*.jar"/>
</fileset>
<!--
===============
Compile targets
===============
-->
..
..
<target name="compile" depends="init,resolve, resources" description="Compile code">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true">
<classpath>
<fileset refid="compile.files"/>
</classpath>
</javac>
</target>
<!--
===============
Distribution targets
===============
-->
..
..
<target name="package" depends="test" description="Create the WAR file">
<copy todir="build/lib">
<fileset refid="runtime.files"/>
</copy>
<war destfile="${war.file}" webxml="${resources.dir}/web.xml">
<fileset dir="${resources.dir}" excludes="web.xml"/>
<lib dir="${build.dir}/lib"/>
</war>
</target>
示例(使用ivy)
对ivy及其任务的高水平介绍。请参阅下面的"检索"ivy任务,它提供了您正在寻找的功能。
build . xml
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<!--
===========
Build setup
===========
-->
<target name="bootstrap" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib"/>
<get src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.2.0/ivy-2.2.0.jar"
dest="${user.home}/.ant/lib/ivy.jar"/>
</target>
<!--
============================
Resolve project dependencies
============================
-->
<target name="resolve" description="Use ivy to resolve classpaths">
<ivy:resolve/>
<ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="runtime.path" conf="runtime"/>
<ivy:cachepath pathid="test.path" conf="test"/>
</target>
<!--
===============
Compile targets
===============
-->
..
..
<target name="compile" depends="init,resolve, resources" description="Compile code">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>
<!--
===============
Distribution targets
===============
-->
..
..
<target name="package" depends="test" description="Create the WAR file">
<ivy:retrieve pattern="${build.dir}/lib/[artifact].[ext]" conf="runtime"/>
<war destfile="${war.file}" webxml="${resources.dir}/web.xml">
<fileset dir="${resources.dir}" excludes="web.xml"/>
<lib dir="${build.dir}/lib"/>
</war>
</target>
指出- "bootstrap"目标被设计用来安装ivy(它没有打包在ANT核心中)
- "cachepath"任务用于创建自定义ANT路径
- "检索"任务用运行时所需的jar填充WAR文件的WEB-INF/lib目录(由ivy配置管理)
中
该文件列出了项目的依赖项。它使用配置在逻辑上将jar分组在一起,并使ivy"cachpath"任务能够在构建中创建匹配的类路径。最后,在构建过程中下载并缓存第三方jar。这是非常方便的,它意味着你可以减少你的项目的大小。
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile" description="Required to compile application"/>
<conf name="runtime" description="Additional run-time dependencies" extends="compile"/>
<conf name="test" description="Required for test only" extends="runtime"/>
</configurations>
<dependencies>
<!-- compile dependencies -->
<dependency org="org.slf4j" name="slf4j-api" rev="1.7.2" conf="compile->default"/>
<!-- runtime dependencies -->
<dependency org="org.slf4j" name="slf4j-log4j12" rev="1.7.2" conf="runtime->default"/>
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
</dependencies>
</ivy-module>