How would I run these programs from ant - java

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>

Related

Need to make a ant build file for eclipse project

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.

Could not find .Main, error with Ant when I try to Run it

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?.

Ant does not work as I expect in Eclipse

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"/>

using ANT to automatic run JUNIT cases, setting up dependencies

I have the following setup for my application:
The project TestAll currently contain a java file that run all the TestAll javafiles in all my other projects and this works as expected. The problem im facing is that I want this TestAll.java to be run from a ant script and have it record the result in a report file. This javafile is dependent on all the other projects in my application.
This is what I have so far:
<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<property name="src.dir" location="" />
<property name="build.dir" location="../bin" />
<property name="dist.dir" location="../dist" />
<property name="lib.dir" location="../lib" />
<property name="test.dir" location="../src" />
<property name="test.report.dir" location="../testreport" />
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="junit.class.path">
<pathelement location="${lib.dir}/junit.jar" />
<pathelement location="${build.dir}" />
</path>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
</target>
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\se.testall.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, junit">
<description>Main target</description>
</target>
</project>
And the errors im getting is:
[javac] Compiling 1 source file to C:\Repositories\MyProject\TestAllProjects\bin
[javac] C:\Repositories\MyProject\TestAllProjects\src\se\testall\src\TestAllClass.java:6: error: package se.tlv.AProject.testall does not exist
[javac] import se.tlv.AnotherProject.testall.*;
[javac] ^
[javac] C:\Repositories\TestAllProjects\src\se\src\TestAllClass.java:7: error: package se.AnotherProject.testall does not exist
..and so on for all the internal imports in my TestAll project
This is most likley a classpath error where ANT in unable to find the files it needs, but I have no idea how to resolve it and have been trying for almost a full day. Any help is appreciated
The classpath provided to the javac task is: the junit jar, the build directory and the current directory.
Unless the current directory (where build.xml is located) is se, the javac task won't be able to find any java files to compile them.
Given that, the classpath for the javac task will need to include a path to each se directory in each project.
Edit:
Note: Unless you're planning on packaging the tests with the rest of the code, you should have two javac tasks that build to a build directory and a test build directory, then provide a path to each of those to junit so it can run the tests, but provide only the build directory path to the jar task.

Ant compile is not creating build directory

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.

Categories