How to enable ant -contrib.jar dynamically in Ant

I am looking for a way to include .jar from an Ant file so that I can immediately use it and call its methods for my purposes. In my case, this is ant-contrib-1.0b3.jar .

+4
source share
2 answers

The best way is to place the Ant -Contrib jarfile project in the project. For example, suppose build.xml is at the root of your project. Create a directory in your project named ant.lib\ant-contrib , then put ant-contrib*.jar in this folder. You can use this method for other optional Ant tasks that you may need (for example, Ivy, Findbugs, Cobrrtura, etc.).

Then in your build.xml you can do this:

 <taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <fileset dir="${basedir}/ant.lib/ant-contrib"/> </classpath> </taskdef> 

I like to do it this way because additional options with tasks are included in the project. If you check everything in your version control system, someone can check your code and build without downloading Ant -Contrib and installing it yourself.

You can define XML namespaces. This gives your Ant -Contrib tasks a prefix to avoid task name conflicts if you use other optional Ant tasks that have the same task name. In addition, it warns users that this is not a standard Ant task.

If you are using an XML namespace, you need to place the XMLNS declaration in the <project> header. This will contain a URI that will connect your Ant Contrib tasks to an XML namespace. For example, the ac: namespace is for all Ant Contrib tasks:

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

This means that the XML namespace (xmlns) ac matches the URI of http://ant-contrib.sourceforge.net . URI can be anything. For instance:

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

The standard should use something like 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> 

However, I like to use the project url. Thus, if someone wants documentation on Ant -Contrib tasks, they know the URL where the Ant -Contrib project lives.

In all three cases described above, I defined the XML namespace using ac . Therefore, you must prefix all Ant -Contrib task names with ac: You can use antcontrib or whatever. In the ac: namespace ac: your Ant -contrib tasks will look like this:

 <ac:if> <istrue value="${include.debug.code}"/> <ac:then> [...] </ac:then> <ac:else> [...] </ac:else> <ac:if> 

If you miss the whole thing of the namespace, you can simply use Ant -Contrib tasks as described:

 <if> <istrue value="${include.debug.code}"/> <then> [...] </then> <else> [...] </else> 

+12
source

The best solution is to integrate the apache ivy dependency manager. Ivy can be used to manage all of your Maven-style class classes!

Example

ivy.xml

This file describes your third-party dependencies. Ivy uses configurations to logically group files. In your case, note that the special “build” configuration is used to configure the ANT tasks necessary for the build:

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

Note:

  • Dependencies are retrieved by default from Maven Central , which now hosts about 90% of the open source tanks

build.xml

 <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&amp;g=org.apache.ivy&amp;a=ivy&amp;v=LATEST&amp;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> 

Notes:

  • There is a special “bootstrap” target used to start a new installation. Unfortunately, ivy does not spread with the ANT core
  • The cachepath ivy task is used to create ANT paths containing uploaded and cached javas files with a plus.
  • The ivy report task is very useful for understanding the transitive dependencies of your third-party libraries.
  • antcontrib now extends as ANT lib , explaining the weird namespace stuff.
+4
source

Source: https://habr.com/ru/post/1439760/


All Articles