Need to make a ant build file for eclipse project - java

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.

Related

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's classpath in Eclipse

I have an Ant build.xml file that works just fine on the command line: it compiles, builds the JAR, and I am able to execute the main method from the JAR just fine. The build.xml file references several thirdparty libraries that are scattered here and there. When building the JAR, the script doesn't include all the thirdparty libraries into the JAR itself. Instead, it puts their path into the JAR's manifest. This helps to keep my JAR slim and tidy.
I'd like to be able to edit and debug my project in Eclipse, but I can't find an easy way to do so. I can have my project use the Ant file to build the project, and that seems to work. However, Eclipse is having trouble finding the thirdparty libaries, and thus Eclipse is having two problems:
it shows (in the text editor) lots of compile errors, because
lots of classes are undefined, and
it can't execute the JAR.
I can solve both of the above problems by specifying by hand, in two difference places (i.e., the build path via Properties->Java Build Path->Libraries, and the execution classpath via Run Configurations->Classpath), all the third party libraries. But it seems like I shouldn't have to do this manually, since all the third party libraries are already listed in my JAR's manifest. What am I doing wrong?
Here's my build.xml file:
<!-- Set global properties for this build -->
<property name="src" location="./src" />
<property name="build" location="./build"/>
<property name="dist" location="./dist"/>
<property name="logs" location="./logs"/>
<property name="docs" location="./docs"/>
<property name="jar" location="${dist}/dynamic_analyzer.jar"/>
<property name="lib" location="../../thirdparty/lib"/>
<property name="hive-util" location="../../hive-utils/dist"/>
<property name="hpdb" location="../../hive-db/hpdb/dist"/>
<property name="static" location="../../hive-backend/static_analyzer/dist"/>
<property name="mainclass" value="com.datawarellc.main.DynamicMain"/>
<path id="dep.runtime">
<fileset dir="${lib}" includes="**/*.jar"/>
<fileset dir="${hive-util}" includes="**/*.jar"/>
<fileset dir="${hpdb}" includes="**/*.jar"/>
<fileset dir="${static}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${docs}"/>
<delete dir="${logs}"/>
</target>
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${dist}"/>
<mkdir dir="${logs}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" debug="on" includeantruntime="false">
<classpath refid="dep.runtime" />
</javac>
<!-- Debug output of classpath -->
<property name="myclasspath" refid="dep.runtime"/>
<echo message="Classpath = ${myclasspath}"/>
</target>
<target name="jar" depends="compile">
<!-- Put the classpath in the manifest -->
<manifestclasspath property="manifest_cp" jarfile="${jar}" maxParentLevels="10">
<classpath refid="dep.runtime" />
</manifestclasspath>
<jar jarfile="${jar}" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="${mainclass}"/>
<attribute name="Class-Path" value="${manifest_cp}"/>
</manifest>
<zipfileset dir="${src}" includes="**/*.xml" />
</jar>
</target>
You can see that I have third-party libraries in several directories (${lib}, ${hive-util}, ${hpdb}, and ${static}). I use these to create a path called dep.runtime. I then include dep.runtime in the manifest when building my jar. How can I get Eclipse to use the same dep.runtime for the build path and the classpath when executing?
An alternative to perl is to use an embedded groovy task:
<project name="demo" default="eclipse-files">
<property name="src.dir" location="src"/>
<property name="classes.dir" location="build/classes"/>
<path id="dep.runtime">
<fileset dir="${lib}" includes="**/*.jar"/>
<fileset dir="${hive-util}" includes="**/*.jar"/>
<fileset dir="${hpdb}" includes="**/*.jar"/>
<fileset dir="${static}" includes="**/*.jar"/>
</path>
<target name="bootstrap">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.4/groovy-all-2.1.4.jar"/>
</target>
<target name="eclipse-files">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
import groovy.xml.MarkupBuilder
project.log "Creating .classpath"
new File(".classpath").withWriter { writer ->
def xml = new MarkupBuilder(writer)
xml.classpath() {
classpathentry(kind:"src", path:properties["src.dir"])
classpathentry(kind:"output", path:properties["classes.dir"])
classpathentry(kind:"con", path:"org.eclipse.jdt.launching.JRE_CONTAINER")
project.references."dep.runtime".each {
classpathentry(kind:"lib", path:it)
}
}
}
</groovy>
</target>
<target name="clean">
<delete file=".classpath"/>
</target>
</project>
Notes:
The bootstrap target will download the 3rd party groovy jar (No dependency on perl)
Groovy can access the "dep.runtime" ANT path directly and iterate over its contents
Groovy has excellent support for writing XML files.
The following answer is similar and additionally generates the Eclipse .project file.
Using Apache Ivy with netbeans
I came up with the following workaround, inspired by the link provided by #leeand00.
First, I wrote a simple Perl script (called genClasspath.pl) that generates the .classpath file that Eclipse uses.
#!/usr/bin/perl
use strict;
if (#ARGV != 2) {
print STDERR "Usage: $0 OUTFILE CLASSPATHSTRING\n";
print STDERR "e.g., $0 .classpath path1:path2:path3\n";
exit 1;
}
my $OUTFILE = $ARGV[0];
my $CLASSPATHSTRING = $ARGV[1];
open my $out_fh, '>', $OUTFILE or die "Couldn't open output file: $!";
print $out_fh q{<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="build"/>
};
my #libs = split(":", $CLASSPATHSTRING);
foreach my $thisLib (#libs){
print $out_fh " <classpathentry kind=\"lib\" path=\"$thisLib\"/>\n";
}
print $out_fh "</classpath>\n";
Then, I have my build.xml file call this script with the content of dep.runtime:
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" debug="on" includeantruntime="false">
<classpath refid="dep.runtime" />
</javac>
<property name="myclasspath" refid="dep.runtime"/>
<exec dir="." executable="../../scripts/genClasspath.pl" os="Linux">
<arg value=".classpath"/>
<arg value="${myclasspath}"/>
</exec>
</target>
The only catch is that I need to run Ant on the command line at least once before I open the project in Eclipse. But when I do, Eclipse is able to compile and execute my project just fine, since the classpath is exactly the same as Ant's.

Ant script to generate Jar - Reference not found error

I have following ant script to generate the jar file
<project name="myProject" basedir="." default="jar">
<property name="src" value="Java Source"/>
<property name="output" value="bin"/>
<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="myProject.jar">
<fileset dir="bin"/>
</jar>
</target>
<target name="clean">
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>
When I run ant script i get following error
Reference myProject.classpath not found.
I am not sure how to solve this error. It requires path of .classpath file ?
I also tried with
refid="classpath"
and it didnt work.
Can anyone help please!
Thanks
You need to define first something like because right now MyProject.classpath is not defined:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
assuming that your classpath has what you need.
If it does not, create another entry under classpath element that has references to jars or whatever you need, or you need to custom specify path:
<path id="MyProject.classpath">
<pathelement location="lib/"/>
<pathelement path="${classpath}/"/>
<pathelement path="${additional.path}"/>
</path>
http://ant.apache.org/manual/using.html#path

Ant built does not generate class files

I'm using build.xml to build my src. However it failed to generate class files without any error message. The full script is
<?xml version="1.0"?>
<project name="auxiliary" basedir="." default="dist">
<property name="src.dir" value="../auxiliary-src/com/nextbio/drugbank"/>
<property name="dist.dir" value="dist"/>
<property name="lib.dir" value="../jboss_config/common_app_jars"/>
<property name="temp.dir" value="temp"/>
<property name="foo_dist.dir" value="../foo/dist"/>
<path id="libs-classpath">
<fileset dir="${foo_dist.dir}">
<include name="foo.jar"/>
</fileset>
</path>
<target name="dist" depends="auxiliary-dist" />
<target name="auxiliary-cleanup">
<delete dir="${temp.dir}"/>
<delete dir="${dist.dir}"/>
<echo message="cleaned up. ${temp.dir}, and ${dist.dir} have been deleted."/>
</target>
<target name ="auxiliary-dist">
<delete dir="${temp.dir}"/>
<echo message="delete ${temp.dir}" />
<mkdir dir="${temp.dir}"/>
<javac destdir="${temp.dir}" source="1.6" target="1.6" debug="on" fork="true" memorymaximumsize="1024m">
<src path="${src.dir}"/>
<classpath>
<path refid="libs-classpath"/>
</classpath>
<include name="com/car/**"/> <!-- troubled line -->
</javac>
<!--<copy overwrite="true" todir="${temp.dir}">
<fileset dir="${src.dir}">
<exclude name="**/*.java"/>
<exclude name="**/*.sql"/>
<exclude name="**/*.txt"/>
</fileset>
</copy>
<delete dir="${dist.dir}"/>
<mkdir dir="${dist.dir}"/>
<jar destfile="${dist.dir}/auxiliary.jar" basedir="${temp.dir}"/> -->
</target>
There is no class file in ${temp.dir} after this step, and no error message. I double checked it, and found it is because of the "troubled line". I tried to add some files to the classpath. I don't know why it is wrong.
The source path should point to the root of the package tree. You make it point to a specific package inside the sources : ../auxiliary-src/com/nextbio/drugbank.
And in the javac task, you ask it to compile all the files matching the pattern com/car/**. That means that it will compile the Java source files in ../auxiliary-src/com/nextbio/drugbank/com/car or in a subdirectory. If that's the case, you have very unconventional package names.
I had the same problem.
My project complilated well but the classes there weren't in nowhere and It didn't have any error message.
My problem was the classpath. The eclipse wizard added EclipseLink 2.5.1 jars.
I removed it and the problem is gone.
I suggest make a simple HelloWord and remove all jars
reference from the classpath and try again.
I encountered this "ant, javac, compile" problem related with the classpath to.
No debug or verbose message shown.
This behavior appear because in classpath exists not compatible (superior) version jar packages and that cause no output classes.

Categories