我正在寻找一种方法来在Ant文件中包含.jar,这样我就可以直接使用它并在我的目标中调用它的方法.就我而言,它是ant-contrib-1.0b3.jar
.
Dav*_* W. 12
最好的方法是将Ant-Contrib jar文件放在你的项目中.例如,假设build.xml
它位于项目的根目录中.创建一个ant.lib\ant-contrib
在项目中调用的目录,然后将其ant-contrib*.jar
放在此文件夹中.您可以将此方法用于您可能需要的其他可选Ant任务(例如,Ivy,Findbugs,Cobrrtura等).
然后,在您的build.xml
文件中,您可以执行以下操作:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/ant.lib/ant-contrib"/>
</classpath>
</taskdef>
Run Code Online (Sandbox Code Playgroud)
我喜欢这样做,因为带有任务的可选罐子都包含在项目中.如果您将所有内容都检查到版本控制系统,那么有人可以检查您的代码,并在不下载Ant-Contrib并自行安装的情况下进行构建.
您可以定义XML命名空间.这为您的Ant-Contrib任务提供了前缀,以避免在您使用具有相同任务名称的其他可选ant任务时发生任务名称冲突.此外,它还警告用户这不是标准的Ant任务.
如果使用XML命名空间,则需要在<project>
标题中放置XMLNS声明.这将包含一个URI,用于将Ant Contrib任务连接到XML命名空间.例如,ac:
命名空间适用于所有Ant Contrib任务:
<project name="my.project" default="package" basedir="."
xmlns:ac="http://ant-contrib.sourceforge.net">
<taskdef resource="net/sf/antcontrib/antlib.xml"
uri="http://ant-contrib.sourceforge.net">
<classpath>
<fileset dir="${basedir}/ant.lib/ant-contrib"/>
</classpath>
</taskdef>
Run Code Online (Sandbox Code Playgroud)
这样做是匹配的XML命名空间(XMLNS)ac
与URI http://ant-contrib.sourceforge.net
.URI可以是任何东西.例如:
<project name="my.project" default="package" basedir="."
xmlns:ac="hamburger:with-fries">
<taskdef resource="net/sf/antcontrib/antlib.xml"
uri="hamburger:with-fries">
<classpath>
<fileset dir="${basedir}/ant.lib/ant-contrib"/>
</classpath>
</taskdef>
Run Code Online (Sandbox Code Playgroud)
标准是使用类似的东西antlib:net.sf.antcontrib
:
<project name="my.project" default="package" basedir="."
xmlns:ac="antlib:net.sf.antcontrib">
<taskdef resource="net/sf/antcontrib/antlib.xml"
uri="antlib:net.sf.antcontrib">
<classpath>
<fileset dir="${basedir}/ant.lib/ant-contrib"/>
</classpath>
</taskdef>
Run Code Online (Sandbox Code Playgroud)
但是,我喜欢使用项目的URL.这样,如果有人想要有关Ant-Contrib任务的文档,他们就知道Ant-Contrib项目所在的URL.
在上面的所有三种情况中,我都定义了XML命名空间ac
.因此,您必须为所有Ant-Contrib任务名称添加前缀ac:
.你可以使用antcontrib
或任何你喜欢的.使用ac:
命名空间,您的Ant-contrib任务将如下所示:
<ac:if>
<istrue value="${include.debug.code}"/>
<ac:then>
[...]
</ac:then>
<ac:else>
[...]
</ac:else>
<ac:if>
Run Code Online (Sandbox Code Playgroud)
如果跳过整个命名空间,可以简单地使用Ant-Contrib任务,如下所示:
<if>
<istrue value="${include.debug.code}"/>
<then>
[...]
</then>
<else>
[...]
</else>
Run Code Online (Sandbox Code Playgroud)
最好的解决方案是集成 apache ivy 依赖管理器。Ivy 可用于管理所有 Maven 风格的构建类路径!
该文件描述了您的项目的第 3 方依赖项。Ivy 使用配置将文件逻辑分组在一起。在您的情况下,请注意特殊的“构建”配置用于配置构建所需的 ANT 任务:
<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"/>
<conf name="build" description="3rd party ANT tasks"/>
</configurations>
<dependencies>
<!-- compile dependencies -->
<dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>
<!-- runtime dependencies -->
<dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime->default"/>
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
<!-- Build dependencies -->
<dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" conf="build->default"/>
</dependencies>
</ivy-module>
Run Code Online (Sandbox Code Playgroud)
笔记:
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:antcontrib="antlib:net.sf.antcontrib">
<target name="bootstrap" description="Install ivy">
<mkdir dir="${user.home}/.ant/lib"/>
<get src="https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=org.apache.ivy&a=ivy&v=LATEST&e=jar"
dest="${user.home}/.ant/lib/ivy.jar"/>
</target>
<target name="init" description="Use ivy to resolve classpaths">
<ivy:resolve/>
<ivy:report todir='build/ivy-reports' 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"/>
<ivy:cachepath pathid="build.path" conf="build"/>
</target>
<target name="taskdefs" depends="init" description="Declare 3rd party ANT tasks">
<taskdef uri="antlib:net.sf.antcontrib" classpathref="build.path"/>
</target>
<target name="build" depends="taskdefs" description="Build logic using ant-contrib tasks">
<echo message="The first five letters of the alphabet are:"/>
<antcontrib:for list="a,b,c,d,e" param="letter">
<sequential>
<echo>Letter @{letter}</echo>
</sequential>
</antcontrib:for>
</target>
<target name="clean" description="Cleanup build files">
<delete dir="build"/>
</target>
<target name="clean-all" depends="clean" description="Additionally purge ivy cache">
<ivy:cleancache/>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
笔记:
归档时间: |
|
查看次数: |
12298 次 |
最近记录: |