<target name="compile" description="Compile the File">
<echo>Compile the File </echo>
<mkdir dir="${compilation-dir}" />
<javac srcdir="." classpath="another2" destdir="${compilation-dir}" />
</target>
I want to echo the description of the target. Is there a better way of doing this other than duplicating it?
I guess this is not a perfect solution, but at least you avoid duplicate descriptions.
<property name="testing.desc" value="this is the desc" />
<target name="testing" description="${testing.desc}">
<echo message="${testing.desc}" />
</target>
Related
I am pretty new to Ant but, I have a general understanding. I just cannot get this to work.
<?xml version="1.0"?>
<project name="Ser321 Assignment 3 Java Movie Library with Ant build file and API support."
default="targets" basedir="."
xmlns:dn="antlib:org.apache.ant.dotnet"
xmlns="antlib:org.apache.tools.ant"
xmlns:cpptasks="antlib:net.sf.antcontrib.cpptasks">
<property name="src.dir" value="src"/>
<property name="lib.dir" value="lib" />
<property name="build" value="classes"/>
<property name="bin" value="bin"/>
<property name="obj" value="obj"/>
<property environment="env"/>
<property name="user" value="${env.USERNAME}"/>
<target name="targets">
<echo message="Targets are clean, prepare, build, execute, and targets"/>
</target>
<path id="compile.classpath">
<pathelement location="${build}"/>
</path>
<path id="external.classpath">
<pathelement location="${lib.dir}/json.jar"/>
</path>
<target name="prepare">
<mkdir dir="${build}" />
<mkdir dir="${bin}"/>
<mkdir dir="${obj}"/>
</target>
<target name="clean">
<delete dir="${build}" failonerror="false"/>
<delete dir="${bin}" failonerror="false"/>
<delete dir="${obj}" failonerror="false"/>
</target>
<target name="build" depends="prepare">
<javac srcdir="${src.dir}"
includeantruntime="false"
destdir="${build}">
<classpath refid="external.classpath"/>
</javac>
</target>
<target name="execute.jar" depends="build"
description="Run the program">
<java classname="Main" fork="yes">
<classpath refid="compile.classpath"/>
</java>
</target>
</project>
This gives me my classes folder with movie as the next folder and then in there, I have my 3 classes. However, it keeps saying class not found and I have no idea what I am doing wrong.
Figured it out. I am an airhead sometimes.
<target name="execute.jar" depends="build"
description="Run the program">
<java classname="movie.Main" fork="yes"> //added movie.main instead of Main
<classpath refid="compile.classpath"/>
</java>
I have my ant build.xml file like this:
<!-- Properties -->
<property name="src" value="${basedir}" />
<property name="jars" value="${src}/jars" />
<property name="dest" value="${src}/dest" />
<property name="reports" value="${src}/reports" />
<path id="claspath">
<fileset dir="${jars}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<echo> removing the directories "dest" and "reports" </echo>
<delete dir="${dest}" />
<delete dir="${reports}" />
</target>
<target name="makedir" depends="clean">
<echo> creating directories "dest" and "reports" </echo>
<mkdir dir="dest" />
<mkdir dir="reports" />
</target>
<target name="complie" depends="makedir">
<javac srcdir="${src}" destdir="${dest}" />
<classpath refid="classpath"/>
</target>
When I type ant command in cmd prompt the compiling happens but the jar files are not loading so I am getting compiling errors. The jar folder that i have mentioned in the above code has only one jar file which is "testng-6.8.5.jar". Please let me know what is wrong in the above code.
You're closing the <javac> tag on the same line, it should be:
<javac srcdir="${src}" destdir="${dest}">
<classpath refid="classpath"/>
</javac>
I have this ant script:
<sequential>
<echo message="Deleting needed folder" />
<property name="bigPathToFolder" value="${basePath}/pathToFolder" />
<delete dir="bigPathToFolder" quiet="false"/>
<echo message="Delete success" />
</sequential>
In console I see Deleting needed folder and after Delete success, but the content of the folder and folder don't change.
What did I do wrong?
replace
<delete dir="bigPathToFolder" quiet="false"/>
to
<delete dir="${bigPathToFolder}" quiet="false"/>
You can use if task available from ant-contrib to do file existance check at path you are looking & run a delete like below:
<if>
<available file="${bigPathToFolder}"/>
<then>
<delete dir="${bigPathToFolder}" quiet="false"/>
<echo message="Delete success" />
</then>
<else>
<echo message="File Not found at path : ${bigPathToFolder}" />
</else>
</if>
I am new to ANT and to use it, I simply created a new java project in Eclipse that just print the word Welcome in the screen. I ran the program using Eclipse and "Welcome" was successfully printed on the screen. This is my program
public class welcome {
public static void main(String[] args)
{
System.out.println("Welcome!!!");
}
}
Then I just followed the usual way to build ANT file using Eclipse so I used Export feature and choose ANT buildfile.
This the buildfile I have got:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
Any modifications will be overwritten.
To include a user specific buildfile here, simply create one in the same
directory with the processing instruction <?eclipse.ant.import?>
as the first entry and export the buildfile again. --><project basedir="." default="build" name="test">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../usr/lib/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<path id="test.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="test.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="welcome">
<java classname="welcome" failonerror="true" fork="yes">
<classpath refid="test.classpath"/>
</java>
</target>
</project>
when I ran the program as Ant, it only gives me a message that build successful! without printing the "Welcome" word on screen!
This is the output
> Buildfile: /home/name/workspace/test/build.xml
> build-subprojects: init: build-project:
> [echo] test: /home/name/workspace/test/build.xml build: BUILD SUCCESSFUL Total time: 376 milliseconds
The default target of your ant file is "build" which will create a jar file of your project (which can be then executed later). If you want to run your project via ant, change the target to "welcome" (see the end of your ant file). That should execute the program as you expected.
Apache Ant is a Java library and command-line tool whose mission is to
drive processes described in build files as targets and extension
points dependent upon each other. The main known usage of Ant is the
build of Java applications
Also class names first letter should be CAPITALIZED ...
More or less this is how your ant should look like, but i still dont understand why you want to use ANT for whatever you are trying to do here.
<project name="Welcome" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="(packagename).(class name)"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
I want to do something like this
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes">
<if>
<isset property="dependent.files.available"/>
<then>
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</then>
<else>
<echo message="Either xx.jar or yy.jar not found"/>
</else>
</if>
</target>
When I tried to compile the code it gave me the following error
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place
It it the correct way of doing it?
You need to have the ant-contrib jar visible at runtime, or configure it correctly as described in the link.
The thing boils down to have Ant load the task definition, so if you put ant-contrib in ant/lib, you just need
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
Just a suggestion. You can do the same thing with ant 1.9.1 onwards using if/unless without ant-contrib (http://ant.apache.org/manual/targets.html)
Your targets would be
<target name="init" description="Create build directories.
>
<mkdir dir="bin" />
<mkdir dir="dist" />
<!-- Check for the presence of dependent JARs -->
<condition property="dependent.files.available">
<and>
<available file="../../xx.jar" />
<available file="../../yy.jar" />
</and>
</condition>
</target>
<!--- Compile -->
<target name="compile" depends="init" description="Compile java sources
and create classes" if="dependent.files.available">
<javac srcdir="src" destdir="bin" classpathref="ext_jar_classpath"/>
</target>