Target Clean does not exist in the project dir - ant

I have installed Ant in my centos 6.3 , installed location are
/opt/ant and also ANT_HOME env are same
I have created build.xml to test by deleting testdir. This directory exist in the /opt/ant/testdir like this.
build.xml
<?xml version="1.0"?>
<project name="testdir" default="all" basedir=".">
<property name="src" value="src"/>
<property name="build" value="build"/>
<property name="lib" value="lib"/>
<target name="all" depends="clean, compile" description="Builds the whole project">
<echo>Doing all</echo>
</target>
<target name="clean">
<echo message="Deleting bin/java ..." />
<delete dir="testdir/test" />
</target>
</project>
Using Command :-
ant -buildfile build.xml Clean
getting error:-
BUILD FAILED
Target "Clean" does not exist in the project "testdir".
Any suggestion to make it work?

You mis-spelt the target name ? 'Clean' as against 'clean' ??

I have found solution. I missed target="compile" block in build.xml.
<target name="compile">
<echo message="Compiling source code"/>
</target>
Run command :-
ant clean

Related

deleting object tree with ant using java language

When I do "make clean" it deletes all the object files , in C distributions.
What would be the command "make clean" when using ant, in Java distributions?
Java based build tools generaly build everything in target directory. ANT builds typically have a "clean" target that works as follows:
<target name="clean" description="Cleanup build files">
<delete dir="${build.dir}"/>
</target>
My advice is not to fight this one :-) It might look dumb but it's how more advanced tools like Maven work by default.
Example
I would typically declare some standard properties at the top of my build file:
<property name="src.dir" location="src/main/java"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="${build.dir}/dist"/>
<property name="jar.main.class" value="org.demo.App"/>
<property name="jar.file" value="${dist.dir}/${ant.project.name}.jar"/>
Which are used to build my jar as follows:
<target name="compile" description="Compile code">
<mkdir dir="${build.dir}/classes"/>
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" includeantruntime="false" debug="true" classpathref="compile.path"/>
</target>
<target name="build" depends="compile" description="Create executable jar archive">
<jar destfile="${jar.file}" basedir="${build.dir}/classes">
<manifest>
<attribute name="Main-Class" value="${jar.main.class}" />
</manifest>
</jar>
</target>
If you look carefully you'll notice everything ANT does is created under the "build" subdirectory.

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=

build multiple projects and clean multiple projects in a single build file with ANT

I have a doubt, I made this build file in order to build 3 different projects
<?xml version="1.0" encoding="UTF-8"?>
<project name="Trinity" basedir="." default="buildall">
<target name="project1">
<ant dir="C:/work/project1"/>
</target>
<target name="project2" depends="project1">
<ant dir="C:/work/project2"/>
</target>
<target name="project3" depends="project1, project2">
<ant dir="C:/work/project3"/>
</target>
<target name="buildall" depends="project3"/>
</project>
This is working now. But I wan to also clean the project before doing the build.
In fact I want to acomplish this:
C:/work/project1 ant clean build
C:/work/project2 ant clean build
C:/work/project3 ant clean build
Thanks in advance.
update: Thanks to the quick response from Alex I did a new build.xml file with the following. And I believe is working well, what do you think?.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Trinity" basedir="." default="buildall">
<target name="project1">
<ant dir="C:/work/project1" target="clean"/>
<ant dir="C:/work/project1" target="build"/>
</target>
<target name="project2" depends="project1">
<ant dir="C:/work/project2" target="clean"/>
<ant dir="C:/work/project2" target="build"/>
</target>
<target name="project3" depends="project1, project2">
<ant dir="C:/work/project3" target="clean"/>
<ant dir="C:/work/project3" target="build"/>
</target>
<target name="buildall" depends="project3"/>
</project>
Thanks.
According to the ant task, you can specify the targets of the external ant build files
<ant dir="C:/work/project1" target="clean build">
Edit:
According to the ant documentation:
You can specify multiple targets using nested elements instead of using the target attribute. These will be executed as if Ant had been invoked with a single target whose dependencies are the targets so specified, in the order specified.
So you can list out multiple targets this way:
<ant dir="C:/work/project1">
<target name="clean" />
<target name="build" />
</ant>
Alternatively you can define a new target in the Project1,2,3 build.xml files called cleanBuild which will in turn call clean followed by build if you want to keep it as a single xml element <ant dir="C:/work/project1" target="cleanBuild">

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>

Ant not creating tar files

I have a little ant script which should create 3 tar files.
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." >
<property name="dcc-shell.dir" value="${basedir}"/>
<property name="dcc-mdp.dir" value="${dcc-shell.dir}/eq-mo-drop-copy-converter-mdp"/>
<property name="mdp-code.dir" value="${dcc-mdp.dir}/src/main/*"/>
<property name="dcc-srv.dir" value="${dcc-shell.dir}/eq-mo-drop-copy-converter-server"/>
<property name="srv-code.dir" value="${dcc-srv.dir}/src/main/*"/>
<property name="dcc-trans.dir" value="${dcc-shell.dir}/eq-mo-drop-copy-converter-transformer"/>
<property name="trans-code.dir" value="${dcc-trans.dir}/src/main/*"/>
<target name="create MDP Tar">
<tar destfile="${dcc-shell.dir}/mdp.tar"
basedir="${dcc-mdp.dir}/**"
excludes="${dcc-mdp.dir}/target/*"
/>
</target>
<target name="create Trans Tar">
<tar destfile="${dcc-shell.dir}/trans.tar"
basedir="${dcc-trans.dir}/**"
excludes="${dcc-trans.dir}/target/*"
/>
</target>
<target name="create SRV Tar">
<tar destfile="${dcc-shell.dir}/srv.tar"
basedir="${dcc-srv.dir}/**"
excludes="${dcc-srv.dir}/target/*"
/>
</target>
</project>
The script runs fine:
Buildfile: C:\eq-Drop-Copy\eq-mo-drop-copy-converter-shell\build.xml
BUILD SUCCESSFUL
Total time: 94 milliseconds
However no tar files are created within the project. Somewhat of a mystery to myself
EDIT
I have been getting the following error!
<target name="create MDP.Tar">
<tar destfile="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/mdp.tar"
basedir="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/eq-mo-drop-copy-converter-mdp/*"
excludes="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/eq-mo-drop-copy-converter-mdp/target/*"
/>
</target>
I have changed the xml to the absoulet paths:
<target name="create MDP.Tar">
<tar destfile="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/mdp.tar"
basedir="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/eq-mo-drop-copy-converter-mdp/*"
excludes="C:/eq-Drop-Copy/eq-mo-drop-copy-converter-shell/eq-mo-drop-copy-converter-mdp/target/*"
/>
</target>
However still the same error how can the basedir not exist the build file is contained within it. The basedir within the MDP target is pointing to an absoulet path and tar all the files within that. why would this be giving an error?
Most likely you called it without giving a target. Your printout does not show any tar targets executed.
Try calling it with target name as argument to ant. Then you will also find out that using spaces in target names may not be such a good idea.
I corrected several issues:
basedir attribute shouldn't have "*" in it. It'll automatically do the whole tree.
Targets can't contain spaces
You probably didn't specify targets. Therefore, I simply added a default target "create_all_tars", and used <antcall> to call the needed targets.
<project basedir="." default="create_all_tars" >
<property name="dcc-shell.dir"
value="${basedir}"/>
<property name="dcc-mdp.dir"
value="${dcc-shell.dir}/eq-mo-drop-copy-converter-mdp"/>
<property name="mdp-code.dir"
value="${dcc-mdp.dir}/src/main/*"/>
<property name="dcc-srv.dir"
value="${dcc-shell.dir}/eq-mo-drop-copy-converter-server"/>
<property name="srv-code.dir"
value="${dcc-srv.dir}/src/main/*"/>
<property name="dcc-trans.dir"
value="${dcc-shell.dir}/eq-mo-drop-copy-converter-transformer"/>
<property name="trans-code.dir"
value="${dcc-trans.dir}/src/main/*"/>
<target name="create_all_tars">
<antcall target="create_MDP_Tar"/>
<antcall target="create_Trans_Tar"/>
<antcall target="create_SRV_tar"/>
</target>
<target name="create_MDP_Tar">
<tar destfile="${dcc-shell.dir}/mdp.tar"
basedir="${dcc-mdp.dir}"
excludes="${dcc-mdp.dir}/target/**"/>
</target>
<target name="create_Trans_Tar">
<tar destfile="${dcc-shell.dir}/trans.tar"
basedir="${dcc-trans.dir}"
excludes="${dcc-trans.dir}/target/**"/>
</target>
<target name="create_SRV_Tar">
<tar destfile="${dcc-shell.dir}/srv.tar"
basedir="${dcc-srv.dir}"
excludes="${dcc-srv.dir}/target/**"/>
</target>
Does this help?

Resources