Ant not creating tar files - ant

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?

Related

If there is no properties file declared, what does the $ do in an Ant build file?

Essentially, I am wondering how ant knows that $ means to pull from the properties file.. what if there was no properties file named?
Take example this build file
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hello World Project" default="info">
<property file="build.properties"/>
<fileset dir="${build.dir}" >
<include name="**/*.java"/>
</fileset>
<target name="info">
<echo>${src}</echo>
</target>
</project>
I know that the $ sign is referencing a property in the build.properties file, however what if there is no properties file, what does the $ sign do in a regular Ant compilation?
Ant leaves references to non-existent properties unchanged. For example, the following Ant script...
<project name="ant-echo-missing-property" default="run">
<target name="run">
<property file="non-existent-file.properties"/>
<echo>${missing-property}</echo>
</target>
</project>
...outputs...
run:
[echo] ${missing-property}

Target Clean does not exist in the project dir

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

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 - Run a Build.xml for all subdirectories

I have a build.xml sitting at the top level and I want the script to run a target for each subdirectory and pass in the subdirectory name as a parameter to the ANT target.
Can you help ?/??
Thanks
Take a look at the subant task. From that page:
<project name="subant" default="subant1">
<property name="build.dir" value="subant.build"/>
<target name="subant1">
<subant target="">
<property name="build.dir" value="subant1.build"/>
<property name="not.overloaded" value="not.overloaded"/>
<fileset dir="." includes="*/build.xml"/>
</subant>
</target>
</project>
this snippet build file will run ant in each subdirectory of the project directory, where a file called build.xml can be found. The property build.dir will have the value subant1.build in the ant projects called by subant.
this is might be what you looking for,
put this as one of your target in your parent build.xml
<target name="executeChildBuild">
<ant antfile="sub1/build.xml" target="build" />
<ant antfile="sub2/build.xml" target="build" />
</target>
If you would like to do it in ant build file, you could use Ant Contrib's for task to iterate over list of subdirectories and execute ant task for each of them.
<for param="subdir">
<dirset dir="${build.dir}">
<include name="./**"/>
</dirset>
<sequential>
<subant target="${target}">
<property name="subdir.name" value="#{subdir}"/>
</subant>
</sequential>
</for>
I didn't test this code since don't have ant installed, but it is close to what you're trying to do I suppose.
If I read the question correctly, this may be what you are looking for instead.
So for your example...
<target name="do-all">
<antcall target="do-first">
<param name="dir-name" value="first"/>
<param name="intented-target" value="init"/>
</antcall>
<antcall target="do-first">
<param name="dir-name" value="second"/>
<param name="intented-target" value="build"/>
</antcall>
<antcall target="do-first">
<param name="dir-name" value="third"/>
<param name="intented-target" value="compile"/>
</antcall>
</target>
<target name="do-first">
<echo>Hello from ${dir-name} ${intented-target}</echo>
<ant antfile="${dir-name}/build.xml" target="${intented-target}"/>
</target>
When you are calling this from Ant, you would enter this at the command line:
ant do-all
and your output should look like this:
do-all:
do-first:
[echo] Hello from first init
do-first:
[echo] Hello from second build
do-first:
[echo] Hello from third compile
BUILD SUCCESSFUL
Total time: 1 second
You will of course need to make sure that the directory name that you are using as a param actually exists, or the build will fail.
You can also always feed the variable that you want to use by adding the value to the build.properties file.

ant large project. How to enforce common output directory structure - include,import and inhertAll=false?

I have a toplevel ant project and many subprojects under it.
./build.xml
./datamodel_src/src/build.xml
./datamodel_src/src/module1/build.xml
./datamodel_src/src/module2/build.xml
./infrastructure_src/src/build.xml
./interfaces_src/src/build.xml
Each of the subproject, I want to enforce a common output directory structure. Project will have a work area and each sub project will have its own work area under it. Each subproject should create its artifacts (lib, docs, classes etc) under a work area for the subproject.
So the output would be some thing like
c:/sandbox/mainprojectworkarea/subprojectworkarea/lib
c:/sandbox/mainprojectworkarea/subprojectworkarea/docs
c:/sandbox/mainprojectworkarea/subprojectworkarea/classes
Currently I do this as follows.
The toplevel build.xml is like below
<project name="toplevelproject" default="compile" basedir=".">
<target name="compile">
<ant dir="infrastructure_src/src" />
<ant dir="interfaces_src/src " /> <!--does not work-->
<ant dir="datamodel_src/src inhertAll=false" /> <!--works-->
</target>
</project>
common.xml is like below
<property environment="env" />
<property name="project.sandbox" value="${env.BUILD_HOME}/sandbox" />
<property name="sandbox" value="${project.sandbox}" />
<property name="pwa" value="${sandbox}/pwa" />
<property name="wa" value="${pwa}/${ant.project.name}" />
<property name="build" value="${wa}/build" />
<property name="lib" value="${wa}/lib" />
<property name="docs" value="${wa}/docs" />
<property name="exports" value="${wa}/exports" />
This is "included" into all projects. For example "datamodel_src/src/build.xml" is like below
<!DOCTYPE project [
<!ENTITY common SYSTEM "../../common.xml">
]>
<project name="dmodel" default="compile" basedir=".">
&common;
<target name="compile">
<echo message="will create lib in ${lib}"/>
<echo message="will create docs in ${docs}"/>
<ant dir="module1" inheritAll="false"/> <!--works fine-->
<ant dir="module2" /> <!--does not work -->
</target>
</project>
This works when I set inhertiAll=false for ant calls.
Is there a better and correct way to?
Expanding answer from Kevin to this question.
Using import the common.xml becomes a real project like below
<project name="toplevelproject" default="compile" basedir=".">
<property name="toplevel" value="settotrue"/>
<target name="compile">
<ant dir="infrastructure_src/src" />
<ant dir="interfaces_src/src" />
<ant dir="datamodel_src/src" />
</target>
</project>
The "datamodel_src/src/build.xml" is now some think like below.
<project name="dmodel" default="compile" basedir=".">
<import file="../../common.xml" />
<target name="compile">
<echo message="will create classes in ${build}"/>
<echo message="will create lib in ${lib}"/>
<ant dir="module1" inheritAll="false"/> <!--works fine-->
<ant dir="module2" /> <!--does not work -->
</target>
</project>
The import gives option to have common targets etc, hence I would go with it.
I'm doing something similar using imports rather than includes. All my common targets and properties are defined in a common build file and each subproject just imports the common file. When you import a file, the properties defined in that file become relative to the importing file.
So I would try doing the following:
Move your compile target from your subproject build files into your common.xml.
Import your common.xml into each subproject build.xml.

Resources