Environment: Mac OSX 10.7.5, Eclipse 4.2.1, Apache Ant version 1.8.2
I created a sample Hello World program in Eclipse then exported the project into a build.xml file.
At the prompt i type in:
$ant
Buildfile: /Users/cspam/Documents/workspace/TestHelloWorld/build.xml
build-subprojects:
init:
build-project:
[echo] TestHelloWorld: /Users/cspam/Documents/workspace/TestHelloWorld/build.xml
build:
BUILD SUCCESSFUL
but the build directory that i am looking for is not created. Here is my xml file:
<project basedir="." default="build" name="TestHelloWorld">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="TestHelloWorld.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 name="cleanall" depends="clean"/>
<target name="build" depends="build-subprojects,build-project"/>
<target name="build-subprojects"/>
<target name="build-project" depends="init">
<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="TestHelloWorld.classpath"/>
</javac>
</target>
<target name="build-refprojects" description="Build all projects which reference this project. Useful to propagate changes."/>
<target name="init-eclipse-compiler" description="copy Eclipse compiler jars to ant lib directory">
<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 name="build-eclipse-compiler" description="compile project with Eclipse compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
I am new to using Ant - so this is all new to me. I was told that it should build a "Build" directory but it is not created. I am not sure what I am doing so would appreciate any pointers/help/suggestions.
UPDATE: as #Cliff suggested. here is the output for ant after doing ant -clean
Buildfile: /Users/cspam/Documents/workspace/TestHelloWorld/build.xml
build-subprojects:
init:
[mkdir] Created dir: /Users/cspam/Documents/workspace/TestHelloWorld/bin
build-project:
[echo] TestHelloWorld: /Users/cspam/Documents/workspace/TestHelloWorld/build.xml
[javac] Compiling 2 source files to /Users/cspam/Documents/workspace/TestHelloWorld/bin
build:
BUILD SUCCESSFUL Total time: 0 seconds
looks like the .class files were indeed created after a clean. Right clicking the "build.xml" file from within Eclipse and doing Run As->Ant Build will also do the same successfully.
I am in the process of now pushing this xml file off to Bamboo to see if i can get a clean run. Is this a straight forward process? thanks again for help.
Related
I have a simple game implemented in eclipse. It consists of about 8 classes.
It is for my school assignment.
In the turn in specification, there is written:
"Send me all source codes, documentation and ant build file, which allows the project to be compiled and generate javadoc documentation".
I really do not understand how ant works. I googled some tutorials, but I cannot understand them either. I tried to generate build.xml file in eclipse, but the teacher said that this doesnt work either.
Could someone give me some simple steps or give me link to some really basic tutorial? Thanks for help.
This is the eclipse generated ant (export project as antbuildfile):
And it is kind of weird, because the class BasicPaint I deleted a long time ago.
<?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="Snakes_and_Adders">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.8"/>
<property name="source" value="1.8"/>
<path id="Snakes_and_Adders.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="Snakes_and_Adders.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target name="BasicPaint">
<java classname="snakes_and_adders.BasicPaint" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="Game">
<java classname="snakes_and_adders.Game" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="NewGame">
<java classname="snakes_and_adders.NewGame" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
<target name="PaintingExample">
<java classname="snakes_and_adders.PaintingExample" failonerror="true" fork="yes">
<classpath refid="Snakes_and_Adders.classpath"/>
</java>
</target>
Ant is used to perform tasks that are useful to build applications. You have tasks like <javac> <jar> etc.. To compile your classes and put them in a jar file.
I don't see why the build.xml generated file wouldn't work.. But you can take it as an example to understand how ant works. You can also adapt that build.xml file to make it work anywhere.
This tutorial looks well explained at first sight: http://www.javaworld.com/article/2076208/java-app-dev/automate-your-build-process-using-java-and-ant.html
I find that ant can be pretty complex easily, it'll take you time to understand it well but it's really doable.
This is my first time using ant and one of my first times I program in java so I'm a little lost, I'm trying to compile and run a program with ant, it seems that it compiles right, but I have problems make it run, it says that it Couldn't find Tarea2.Main.
So this is the structure of my project file:
Tarea 2/
src/tarea/pkg2
build.xml
3rd party .jar
Inside src/tarea/pkg2 I have all my .java, and the .java that has the main method is called Tarea2.java.
And this is my build.xml:
<project name="Tarea2" default="run" basedir=".">
<property name="src" location="src"/>
<property name="build" location="build"/>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
</target>
<path id="classpath">
<fileset dir=".">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath refid="classpath"/>
</javac>
</target>
<target name="run" depends="compile">
<java classname="Tarea2.Main" classpath="${build}">
<classpath refid="classpath"/>
</java>
</target>
</project>
So I write "ant" in cmd from windows and everything is ok, except in the "run" target, in which I got stuff like:
[java] Could not find Tarea2.Main. Make sure you have it in your classpath
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute
...
Somebody can help me pls?.
I have this question for the batch file. I have come to know that instead of this drudge I can automate this by Ant; which is what the tool is about I heard.
I want to run this two classes and I have some questions,
Do I need two run task because I need to run these programs separately one after the other?
How will I run this program if I jar it? Would I need two dist task to create separate jars? The thing is I have two entry points from these?
Here's a quick sample build.xml that will build, jar and run. Assuming you have Ant installed, then just run ant in the base folder and it will do the rest. My output shown below.
<project name="myproject" basedir="." default="all">
<property name="build.dir" value="${basedir}/build"/>
<property name="dist.dir" value="${basedir}/dist"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="jar.name" value="myjar.jar"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property file="${basedir}/build.properties"/>
<target name="all" depends="clean, compile, jar, run"/>
<target name="clean" description="cleans all build directories">
<delete dir="${build.dir}"/>
</target>
<target name="compile" description="compiles the project">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="on" deprecation="on" optimize="on" fork="true" memoryMaximumSize="256m">
<include name="**/*.java"/>
</javac>
</target>
<target name="jar" description="Jars the files and signs the jar file">
<jar jarfile="${build.dir}/${jar.name}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
</manifest>
<fileset dir="${classes.dir}">
<include name="**/*.*"/>
</fileset>
</jar>
</target>
<target name="run" description="runs tasks">
<echo>Running task 1</echo>
<java classname="test.Main1">
<classpath>
<pathelement location="${build.dir}/${jar.name}"/>
</classpath>
</java>
<echo>Running task 2</echo>
<java classname="test.Main2">
<classpath>
<pathelement location="${build.dir}/${jar.name}"/>
</classpath>
</java>
</target>
</project>
Here's src for Main1. Main2 just changes Main1 to Main2
package test;
public class Main1 {
public static void main(String[] args) {
System.out.println("Task 1...");
}
}
Output:
c:\Customers\StackOverflow>ant
Buildfile: build.xml
clean:
[delete] Deleting directory c:\Customers\StackOverflow\build
compile:
[mkdir] Created dir: c:\Customers\StackOverflow\build\classes
[javac] Compiling 2 source files to c:\Customers\StackOverflow\build\classes
jar:
[jar] Building jar: c:\Customers\StackOverflow\build\myjar.jar
run:
[echo] Running task 1
[java] Task 1...
[echo] Running task 2
[java] Task 2...
all:
BUILD SUCCESSFUL
Total time: 0 seconds
c:\Customers\StackOverflow>
use
<sequential>
<!-- call target1 -->
<!-- call target2 -->
</sequential>
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"/>
At present I have the following build.xml:
<project name="Bccn" default="help" basedir=".">
<!-- Define the properties used by the build -->
<property name="app.name" value="bccn" />
<property name="app.version" value="0.1-dev" />
<property name="tcserver.home" value="/home/abhishek/tomcat" />
<property name="work.home" value="${basedir}/work" />
<property name="dist.home" value="${basedir}/dist" />
<property name="src.home" value="${basedir}/src" />
<property name="web.home" value="${basedir}/web" />
<property name="lib.dir" value="${basedir}/lib" />
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now I included log4j as a local dependency and want to include it when I create my .war file. However, ANT is not able to find the dependency. Is there a way to get it working? Sorry for the basic question, I am a noob at it.
Update (and thanks for the help I got already):
I didn't want to add the "war" thing so I modified my build.xml as follows:
``
<target name="help">
<echo>You can use the following targets:</echo>
<echo>
</echo>
<echo> help : (default) Prints this message </echo>
<echo> all : Cleans, compiles, and packages application</echo>
<echo> clean : Deletes work directories</echo>
<echo> compile : Compiles servlets into class files</echo>
<echo> dist : Packages artifacts into a deployable WAR</echo>
<echo>
</echo>
<echo>For example, to clean, compile, and package all at once, run:</echo>
<echo>prompt> ant all </echo>
</target>
<!-- Define the CLASSPATH -->
<path id="compile.classpath">
<fileset dir="${tcserver.home}/bin">
<include name="*.jar" />
</fileset>
<pathelement location="${tcserver.home}/lib" />
<fileset dir="${tcserver.home}/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.home}" />
<delete dir="${dist.home}" />
</target>
<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
<mkdir dir="${dist.home}" />
<mkdir dir="${work.home}/WEB-INF/classes" />
<!-- Copy static HTML and JSP files to work dir -->
<copy todir="${work.home}">
<fileset dir="${web.home}" />
</copy>
</target>
<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${work.home}/WEB-INF/classes">
<fileset dir="${src.home}" excludes="**/*.java" />
<fileset dir="${lib.dir}" includes="*.jar" />
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />
</target>
Now ANT can find the dependencies and compile it. However when I deploy it to a Tomcat server, it fails to file the dependencies. Can you please provide some ideas as to how I can package the dependencies so its visible to Tomcat as well?
You should use the war task instead:
<war destfile="${warname}" webxml="war/WEB-INF/web.xml">
<fileset dir="${work.home}"/>
<lib dir="${lib.dir}/" includes="log4j.jar"/>
<classes dir = "${CLASSES}" />
</war>
You can configure it to find the dependency jars to be included in your war file and to specify the path to the web.xml folder. You shouldn't be coping the .class files to the destination in the war folder, let the war task do it for you.