Maven-Ant dependency problem - ant

I am trying to make a build file for a Java project in eclipse using Maven and Ant. However, I can't make the maven-ant-tasks dependency work. Can anybody help me on what to do with this? Maybe my URL and file is wrong. Here it is:
<property name="maven.ant.dir" value="http://apache.hoxt.com/maven/binaries"/>
<property name="maven.ant.file" value="http://apache.hoxt.com/maven/binaries/maven-ant-tasks-2.1.3.jar"/>

Here's how the Maven ANT task works
<project .. xmlns:artifact="antlib:org.apache.maven.artifact.ant">
..
<target name="retrieve" description="Retrieve dependencies using Maven">
<artifact:dependencies filesetId="compile.fileset" useScope="compile">
<pom file="${pom.dir}/pom.xml"/>
</artifact:dependencies>
<copy todir="${retrieve.dir}/compile">
<fileset refid="compile.fileset" />
<mapper type="flatten" />
</copy>
..
Documentation is here

Related

Conditional ivysettings file in Ant

I am trying to adapt an Ant build.xml script to be able to work in both our local office network and within AWS. As such, I have to use a different ivysettings.xml file depending on where the build is happening. In both cases the build is kicked off in Jenkins. My idea was to inject a property 'aws=true' when kicked off from AWS and have the property absent otherwise. We are using Ant 1.7.1 local and a newer version in AWS but I'm not able to see which one right now, build.xml should be able to run on both so 1.7.1 is the limit. I could get this upgraded if necessary.
Can someone help me with the syntax required to adapt this piece of the build.xml file for this purpose?
<!-- Resolve dependencies -->
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:settings file="ivysettings.xml"/>
<ivy:retrieve sync="true"/>
</target>
If aws=true I want to use a file called ivysettings_aws.xml, else ivysettings.xml.
Thank you.
I figured this out using ant_contrib.
<!-- Resolve dependencies -->
<target name="resolve" description="retrieve dependencies with ivy">
<if>
<equals arg1="${aws}" arg2="true" />
<then>
<ivy:settings file="ivysettings_aws.xml"/>
</then>
<else>
<ivy:settings file="ivysettings.xml"/>
</else>
</if>
<ivy:retrieve sync="true"/>
</target>
Works.
Would the following be simpler?
<property name="ivy.settings" value="ivysettings.xml"/>
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:settings file="${ivy.settings}"/>
..
</target>
Alternate settings files are then specified as follows:
ant -Divy.settings=ivysettings_aws.xml ..
You should use the new if/unless feature introduced with Ant 1.9.1 to get rid of an additional dependency to antcontrib, which is out of service btw., latest release 1.0b3 nearly 10 years ago.
Something like :
<project
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>
<!-- Resolve dependencies -->
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:settings file="ivysettings_aws.xml" if:true="${aws}"/>
<ivy:settings file="ivysettings.xml" unless:true="${aws}"/>
<ivy:retrieve sync="true" />
</target>
</project>
Alternatively use the solution proposed by Mark O' Connor, but you need to remember changing the -Divy.settings=... parameter to meet your needs.

Importing ZXing - missing core/build.xml

I'm trying to import Google's ZXing.
I downloaded the latest release from https://code.google.com/p/zxing/downloads/detail?name=ZXing-2.2.zip&can=2&q=
From the cmd prompt I navigated to the root directory of the downloaded zxing and tried to execute
ant -f core\build.xml
PROBLEM :
Buildfile : build.xml does not exist!
Build failed
My zxing-2.2/core file contains :
src
test
pom.xml
Questions:
How to build a file that is missing?
Is it a problem from the zxing-2.2.jar I downloaded?
This problem happened to me too, I solved it by creating the build.xml file inside core folder
change name="whatever you want" in the second line, here it's "project"
code of build.xml:
<?xml version="1.0" encoding="utf-8" ?>
<project name="project" default="jar" basedir=".">
<target name="compile" description="Compile source">
<mkdir dir="bin" />
<javac srcdir="src" includes="**" destdir="bin"/>
<copy todir="bin">
<fileset dir="src" />
</copy>
</target>
<target name="jar" description="Package into JAR" depends="compile">
<jar destfile="project.jar" basedir="bin" compress="true" />
</target>
</project>
Run the build command again and see if it works.
Please do consider that you can always use the pom.xml to achieve the same. This is the method prescribed in the official zxing documentation
https://code.google.com/p/zxing/wiki/GettingStarted
The commands I have used are as follows:
cd core
mvn -DskipTests -Dgpg.skip=true install
That's it, you are done. Obviously, maven is to be installed before using the code
I tried the accepted answer, but unfortunately it not worked. Actually the jar was built successfully, but it was not built into the apk when Eclipse built the project. This was when i referenced ZXing as a library project. I managed to write an ant script which works, so i share it here:
<?xml version="1.0" encoding="utf-8" ?>
<project name="core" basedir="." default="dist" >
<property name="dist.dir" value="dist" />
<property name="src.dir" value="src" />
<property name="build.dir" value="bin" />
<target name="dist" depends="clean, package" />
<target name="clean" >
<delete dir="${build.dir}" />
</target>
<target name="init" >
<mkdir dir="${build.dir}" />
</target>
<target name="compile" >
<javac debug="off" destdir="${build.dir}" source="1.6" srcdir="${src.dir}" target="1.6" />
</target>
<target name="package" depends="init, compile" >
<jar basedir="${build.dir}" destfile="${dist.dir}/core.jar" />
</target>
</project>
If you just need the core.jar from zxing, you can skip that process and get the pre-built JARs from the GettingStarted wiki page
Core.jar is the library solution to integrate zxing in your app
(the other option is via Intent but BarcodeScanner.apk is needed)
For zxing2.2, you can obtain the core.jar from the zxing Maven repository here
Zxing 2.2 and above don't include core/build.xml
If the original file is necessary I recommend using Zxing 2.1, which can be found here:
https://code.google.com/p/zxing/downloads/detail?name=ZXing-2.2.zip&can=2&q=

How to make ant not fail if folder on classpath is not found

I'm trying to modify my ant script so that it will build without error whether or not a local lib folder exists. I want to use the same script on multiple wars, some of which will have WEB-INF/lib, and some of which won't. If the folder exists, include it in the classpath, if not, do not include it. I have tried putting but I can't figure out where it should go. I think this should be a lot simpler than I'm making it out to be but my Googl Fu is failing me.
<property name="local.libs" value="WebContent/WEB-INF/lib" />
<path id="local.libs.path">
<fileset dir="${local.libs}" includes="*.jar" />
</path>
<target name="compile">
<mkdir dir="${build.classes.dir}"/>
<javac srcdir="${src.java.dir}" destdir="${build.classes.dir}" debug="true" includeantruntime="false">
<compilerarg value="-Xlint:-path" />
<classpath refid="local.libs.path" />
<classpath refid="server.libs.path" /> <!-- not referenced in snippet -->
</javac>
</target>
I ended up solving this by making the value of local.libs just WebContent/WEB-INF:
<property name="local.libs" value="WebContent/WEB-INF" />
and then the fileset
<fileset dir="${local.libs}" includes="*lib/*.jar" />
Then it would build whether or not the lib folder existed.

ANT Generated jar: is it a namespace issue?

I have a Eclipse-Java-Project with an ANT-build-file. This build file exports a jar of the project without compiling it. So I only export the sources.
<target name="jar">
<mkdir dir="/jar"/>
<jar destfile="/jar/my_test_jarfile.jar" basedir="/src" />
</target>
I use this generated jar in another eclipse java project and set the path to the jar in the build-path-settings of the project. The problem is that eclipse says it cannot resolve the namespace of the imported classes of the jar.
If I export the jar manually by right clicking on the project and then "Export" and putting the jar to the build path of the other project, everything works fine and there are no errors. So the question is now, what am I doing wrong?
So here is my solution. It seems that you have to compile the source first and then pack it into a jar. I don't give a guarantee that this jar is exactly the same like the one you get from eclipse when you do the right click thing and export etc.
But it works for me, there are no namespace errors any longer. so here is a minimum version of my ant targets:
<project default="run" basedir=".">
<property name="src.dir" value="src" />
<property name="classes.dir" value="bin" />
<property name="build.dir" value="build" />
<path id="libs">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${basedir}\${classes.dir}"/>
</path>
<target name="run">
<antcall target="compile"/>
<antcall target="jar"/>
</target>
<target name="compile">
<javac debug="true" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="libs" encoding="UTF-8" />
</target>
<target name="jar">
<jar destfile="${build.dir}/my_jar_file.jar" basedir="${classes.dir}">
</target>
</project>

Using Ivy to download/install ant-contrib, bsf, beanshell, commons-logging

I am building a project using Ant and Ivy. The build.xml file depends on ant-contrib, bean scripting framework, beanshell, and commons-logging.
Ant searches for libraries in several places, including ${user.home}/.ant/lib.
Is there any way in the build.xml file to have these libraries automatically download and install in the ${user.home}/.ant/lib directory if they are not already present, perhaps using Ivy itself?
Thanks, Ralph
The only jar you need in your ant lib is ivy :-)
Declare your dependencies as normal within your ivy.xml file. Make use of a configuration to collectively group the jars associated with ANT tasks:
<configurations>
<conf name="tasks" description="Ant tasks"/>
</configurations>
<dependencies>
<dependency org="ant-contrib" name="cpptasks" rev="1.0b5" conf="tasks->default"/>
<dependency org="junit" name="junit" rev="3.8" conf="tasks->default"/>
..
In your build.xml file you can create a path from this configuration
<ivy:resolve/>
<ivy:cachepath pathid="tasks.path" conf="tasks"/>
<taskdef name="task1" classname="??" classpathref="tasks.path"/>
<taskdef name="task2" classname="??" classpathref="tasks.path"/>
I happened on this question as I was reading the Ivy cachefileset documentation, which states:
Please prefer the use of retrieve +
standard ant path creation, which make
your build more independent from ivy
(once artifacts are properly
retrieved, ivy is not required any
more).
The Ivy cachepath documentation similarly states:
If you want to make your build more
independent from Ivy, you could
consider using the retrieve task. Once
the artifacts are properly retrieved,
you can use standard Ant path creation
which makes Ivy not necessary any
more.
So, it would seem a better answer would be to modify Mark's response to something that uses retrieve in conjunction with ant paths. Something along the lines of the following:
Mark's Response (modified)
<configurations>
<conf name="tasks" description="Ant tasks"/>
</configurations>
<dependencies>
<dependency org="ant-contrib" name="cpptasks" rev="1.0b5"
conf="tasks->default"/>
<dependency org="junit" name="junit" rev="3.8" conf="tasks->default"/>
..
In your build.xml file you can create a path from this config
<ivy:retrieve conf="tasks"
pattern="${dir.where.you.want.taskdef.jars}/[artifact]-[revision].[ext] />
<path id="tasks.path">
<fileset dir="${dir.where.you.want.taskdef.jars}">
<include name="**/*.jar"/>
</fileset>
</path>
<taskdef name="task1" classname="??" classpathref="tasks.path"/>
<taskdef name="task2" classname="??" classpathref="tasks.path"/>
This would even allow you to move the retrieve task into a separate ant file that deals with dependencies. Thereby, you don't have to rely on ivy after your dependencies are retrieved into their directories.
The intention of ivy is that you use it to pull down your jars (resolve & retrieve). Once you have them in place, you can switch back to using standard Ant.
Note: I would just pull these dependencies into the lib directory. That would simplify the retrieve task:
<ivy:retrieve conf="tasks" />
Also Note: visit the 'Path-like Structures' section of this page for more on "standard ant path creation"
I'd use ant to install everything INTO ant =D
Just use depends="init-ant-contrib, init-ivy"
<!-- ANT-CONTRIB Auto Installer -->
<available property="ant-contrib-exists"
file="${ant.library.dir}/ant-contrib-1.0b3.jar" />
<target name="download-ant-contrib" unless="ant-contrib-exists">
<mkdir dir="${ant.library.dir}" />
<get src="http://downloads.sourceforge.net/project/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3-bin.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fant-contrib%2Ffiles%2Fant-contrib%2F1.0b3%2F&use_mirror=cdnetworks-us-1"
dest="${ant.library.dir}/ant-contrib-1.0b3-bin.zip"
username="true" />
<unzip src="${ant.library.dir}/ant-contrib-1.0b3-bin.zip"
dest="${ant.library.dir}"
overwrite="no" />
<move todir="${ant.library.dir}">
<fileset file="${ant.library.dir}/ant-contrib/*.jar" />
<fileset file="${ant.library.dir}/ant-contrib/lib/*.jar" />
</move>
<delete file="${ant.library.dir}/ant-contrib-1.0b3-bin.zip" />
<delete dir="${ant.library.dir}/ant-contrib" />
</target>
<target name="init-ant-contrib" depends="download-ant-contrib">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${ant.library.dir}/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
</target>
<!-- IVY Auto Installer -->
<property name="ivy.install.version" value="2.1.0-rc2" />
<condition property="ivy.home" value="${env.IVY_HOME}">
<isset property="env.IVY_HOME" />
</condition>
<property name="ivy.home" value="${user.home}/.ant" />
<property name="ivy.jar.dir" value="${ivy.home}/lib" />
<property name="ivy.jar.file" value="${ivy.jar.dir}/ivy.jar" />
<available file="${ivy.jar.file}" property="ivy-exists" />
<target name="download-ivy" unless="ivy-exists">
<mkdir dir="${ivy.jar.dir}" />
<!-- download Ivy from web site so that it can be used even without any special installation -->
<get src="http://repo2.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
dest="${ivy.jar.file}"
usetimestamp="true" />
</target>
<target name="init-ivy" depends="download-ivy">
<!-- try to load ivy here from ivy home, in case the user has not already dropped
it into ant's lib dir (note that the latter copy will always take precedence).
We will not fail as long as local lib dir exists (it may be empty) and
ivy is in at least one of ant's lib dir or the local lib dir. -->
<path id="ivy.lib.path">
<fileset dir="${ivy.jar.dir}" includes="*.jar" />
</path>
<taskdef resource="org/apache/ivy/ant/antlib.xml"
uri="antlib:org.apache.ivy.ant"
classpathref="ivy.lib.path" />
</target>
Now that you have ant-contrib & ivy, everything else should be a simple ivy.xml & ivy-resolve away:
<target name="resolve" depends="init-ivy">
<ivy:retrieve />
</target>
I'm sure you can find similar methods of installing whatever ant task you might need.

Resources