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"/>
Related
probably this is a very basic question but I am new to java, so please bear with me.
I am trying to run java task in ant and I want to know if there is any way to get all the name of the classes inside a folder in ant?
This is the build.xml file
<project default='run-test' basedir=".">
<property name="src" value="src"/>
<property name="test" value="test"/>
<property name="build" value="build"/>
<property name="build-classes" value="build/classes"/>
<property name="build-test-classes" value="build/test/classes"/>
<property name="junit4" value="/usr/share/java/junit4.jar"/>
<target name="init">
<mkdir dir="${build-classes}"/>
<mkdir dir="${build-test-classes}"/>
</target>
<target name="compile" depends="init">
<javac includeantruntime="false"
srcdir="${src}"
destdir="${build-classes}"/>
</target>
<target name="compile-test" depends="compile">
<javac includeantruntime="false"
classpath="${junit4}:${build-classes}"
srcdir="${test}"
destdir="${build-test-classes}"/>
</target>
<target name="run-test" depends="compile-test">
<java classpath="${junit4}:${build-classes}:${build-test-classes}"
classname="org.junit.runner.JUnitCore"
args="myclass.MyClassTest"/> <---This is my problem
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
</project>
I have to manually mention the name of the class in the arguments.
Is there any way where I can only mention the folder and all the class names will be taken up as arguments for the javatask automatically?
Ant ships with a task for running JUnit tests.
In the run-test target in your example, replace the <java> task with the following:
<junit>
<formatter type="plain" usefile="false"/>
<classpath>
<pathelement location="${junit4}"/>
<pathelement location="${build-classes}"/>
<pathelement location="${build-test-classes}"/>
</classpath>
<batchtest>
<fileset dir="${build-test-classes}">
<include name="**/*.class"/>
</fileset>
</batchtest>
</junit>
Hi I'm working on a project where i'm told to create dist ant target which creates JAR file and jrun target which depends on dist target ant should run dist created jar file. While ant jrun i get the folowing error : jrun
[java]: no main manifest attribute in *\build\jar\navus.jar
<?xml version="1.0" encoding="UTF-8"?>
<project name="POS" default="build" basedir=".">
<!-- Project properties-->
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main.class" value="ee.ut.math.tvt.navus.Intro"/>
<!-- Different classpaths form compiling and running-->
<path id="compile.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar"/>
</fileset>
</path>
<path id="run.classpath">
<pathelement location="${classes.dir}"/>
<path refid="compile.classpath"/>
</path>
<!-- Clean existing build-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!-- Builds Java code-->
<target name="build" depends="clean">
<mkdir dir="${build.dir}"/>
<mkdir dir="${classes.dir}"/>
<copy file="${src.dir}/log4j.properties" todir="${classes.dir}" overwrite="true" />
<javac
srcdir="${src.dir}"
destdir="${classes.dir}">
<classpath refid="compile.classpath"/>
</javac>
</target>
<!-- Creates jar file-->
<target name="dist" depends="build">
<mkdir dir="${jar.dir}"/>
<propertyfile file="version.properties">
<entry key="build.revision.number" type="int" default="0" operation="+"/>
</propertyfile>
<jar jarfile="${build.dir}/jar/navus.jar"
basedir="${build.dir}/classes">
<manifest>
<attribute name="mainClass" value="${main.class}"/>
</manifest>
<fileset dir="${basedir}" includes="${src.dir}"/>
</jar>
</target>
<!-- Executes application via class Intro-->
<target name="run" depends="build"
description="runs introUI via Intro" >
<java classname="${main.class}"
classpathref="run.classpath"
fork="yes">
</java>
</target>
<!-- Runs application using JAR file-->
<target name="jrun" depends="dist"
description=" Run JAR file">
<java jar="${jar.dir}/navus.jar"
fork="yes"/>
</target>
</project>
Looking at google returns https://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html. Maybe try something like this -
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="oata.HelloWorld"/>
</manifest>
</jar>
</target>
You need to create a manifest file (MANIFEST.MF), which goes inside the META-INF folder inside your jar. It resembles this:
Manifest-Version: 1.0
Application-Name: MyApp
Sealed: true
Main-Class: com.mysite.myappname.myMainClass
Codebase: www.mysite.com
I created an ant build file to compile my java src into a .jar file. It uses external jar files.
My files dir:
+src
----android
------------Address.java
------------HelloServer.java
------------HelloServerResource.java
+lib
------------org.codehaus.jackson.core.jar
------------org.codehaus.jackson.mapper.jar
------------org.json.jar
------------org.restlet.ext.jackson.jar
------------org.restlet.jar
build.xml
The external libraries are in lib folder.
The main class is in HelloServer.java
Here is my ant build file:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}" classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<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 fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
I execute run and all the dirs were created successfully, the .jar file was created and it was able to be executed.
I open terminal and go to the jar dir. I tried to execute through command line like this and it gave me the following error:
$java -jar helloworld.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/restlet/Server
at android.HelloServer.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.restlet.Server
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
Why does it say class file not found? I thought by creating the helloworld jar file, i actually included the external libraries.
You'll need to combine all the jars together using zipgroupfileset:
<target name="jar" depends="compile">
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipgroupfileset dir="${lib-dir}" includes="*.jar"/>
</jar>
</target>
See: Creating a bundle jar with ant
i found a solution. basically i compile my jar file and point it to the external jar files using manifest file.
here is my build file:
<project name="helloworld">
<property name="src-dir" location="src"/>
<property name="build-dir" location="build"/>
<property name="classes-dir" value="${build-dir}/classes"/>
<property name="dist-dir" location="dist"/>
<property name="lib-dir" value="lib"/>
<property name="jar-dir" value="${build-dir}/jar"/>
<property name="main-class" value="android.HelloServer"/>
<path id="classpath">
<fileset dir="${lib-dir}" includes="**/*.jar"/>
</path>
<target name="clean" description="compile the source">
<delete dir="${build-dir}" />
<delete dir="${dist-dir}" />
</target>
<target name="cleanall" depends="clean"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build-dir}"/>
<mkdir dir="${classes-dir}"/>
<mkdir dir="${jar-dir}"/>
<!--<mkdir dir="${dist-dir}"/>-->
</target>
<target name="compile" depends="init" description="compile the source " >
<javac srcdir="${src-dir}" destdir="${classes-dir}"
classpathref="classpath" includeantruntime="false" />
<!--<javac srcdir="${src-dir}" destdir="${build-dir}"/>-->
</target>
<target name="jar" depends="compile">
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<jar destfile="${jar-dir}/${ant.project.name}.jar" basedir="${classes-dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
<attribute name="Class-Path" value="${jar.classpath}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar-dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
</project>
The main difference is that i added the following into targer "jar":
<manifestclasspath property="jar.classpath" jarfile="${jar-dir}/${ant.project.name}.jar">
<classpath refid="classpath"/>
</manifestclasspath>
<attribute name="Class-Path" value="${jar.classpath}"/>
I have problem with my Ant script. I have to run junit test on ant run.
My current script looks like:
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="doc" location="doc"/>
<property name="dist" location="dest"/>
<property name="lib" location="lib"/>
<property name="app" value="${ant.project.name}.jar"/>
<presetdef name="javac">
<javac includeantruntime="false"/>
</presetdef>
<target name="clean">
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
<target name="compile" depends="clean" description="Compile">
<mkdir dir="${build}"/>
<javac srcdir="${src}"
destdir="${build}"
classpath="${lib}/junit-4.10.jar:${lib}/swing-layout-1.0.4.jar:${src}">
</javac>
<copy todir="${build}/checkers">
<fileset dir="${lib}">
<include name="resources/**" />
</fileset>
</copy>
</target>
<target name="run" depends="compile">
<echo>Running the junit tests...</echo>
<junit showoutput="no" fork="no">
<classpath>
<pathelement location="${build}"/>
<pathelement path="${build}:${lib}/junit-4.10.jar"/>
</classpath>
<formatter type="plain" usefile="false" />
<test name="checkers.CheckersTest"/>
</junit>
</target>
On my Linux box test runs fine and everything looks good. But on my Windows, Ant gives my nice:
java.lang.NoClassDefFoundError: junit/framework/TestListener
Ant in debug mode however told me that he loaded TestListener.class from suplied junit-4.10.jar file.
Try this answer http://youtrack.jetbrains.com/issue/TW-4882:
To fix the problem you should either use fork="true" attribute for
junit task (in this case classpath will be created correctly), or
to copy junit.jar to ANT_HOME/lib (to ensure correct class loading).
Here is also bug for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=36198. Last comment says JUnit is available in Ant via the org.eclipse.ant.optional.junit fragment
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.