Java ANT to combine tasks - java

I am trying to write a task that will combine all the tasks in by build.xml
<project name="HW4_Build">
<target name="mkedir">
<mkdir dir="bld/class/cscie55/hw4"/>
</target>
<target name="clean">
<delete dir="bld"/>
</target>
<target name="compile" depends="clean, mkedir">
<javac destdir="bld/class" srcdir="src/cscie55/hw4"/>
</target>
<target name="jar" depends="compile">
<jar destfile="HW4.jar" basedir="bld/class"/>
</target>
</project>
Is following method right way to go about this?
<ant antfile="build"/>

In this build.xml you might just want to try:
<project name="HW4_Build" default="jar">
Because the jar target already includes the other targets via the dependency chain.
<ant antfile="build"/>
Is used for invoking another build file. Not sure that's what you want.

<antcall target="target"/>
Use this to define a target that combines all your other targets.
Although this won't exactly work in your case since you are using depends... So you only need to call your last target in the dependency chain.

Related

How to get class files even when the build fails with ant

I have a java project with java files more than 2500 and some of them may have compilation issue. I need to generate classfiles and route to a particular folder. Even with some of the compilation error, rest of jave turned to class files, with eclipse.
But I need to compile with build tools like ANT but it stops as build failed.
Hence no classfiles generated. Is there a way to compile and generated when project has some compilation error using ANT. The sample code is like
<?xml version="1.0"?>
<project name="REL854" basedir="." default="compile">
<target name="create" depends="delete">
<mkdir dir="src"/>
</target>
<target name="delete">
<delete dir ="src" />
<echo>exclude not working</echo>
</target>
<target name="copy" depends="create">
<copydir src="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\src" dest="C:\Users\username\neon\ANTBuildFor854\build\src" excludes="Samples"></copydir>
<copy todir ="C:\Users\username\neon\ANTBuildFor854\build\lib" overwrite="true">
<fileset dir="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\lib" ></fileset>
</copy>
</target>
<target name="compile" depends="copy"></target>
<javac failonerror="false" includeantruntime="false" srcdir="C:\Users\username\neon\ANTBuildFor854\build\src" destdir="C:\Users\username\neon\ANTBuildFor854\build\bin" includes="**/*.java"></javac>
</project>

Ant Download a Jar at Build

I'm trying to integrate forbiddenapis check into my project. I've defined that:
<target name="forbidden-checks" depends="clean, runtime, test">
<ivy:cachepath organisation="de.thetaphi" module="forbiddenapis" revision="2.2" inline="true" pathid="classpath"/>
<taskdef uri="antlib:de.thetaphi.forbiddenapis" classpathref="classpath"/>
<forbiddenapis classpathref="all-lib-classpath" dir="${build.dir}" targetVersion="${javac.version}">
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</forbiddenapis>
</target>
all-lib-classpath includes all files to be checked by forbiddenapis plugin. I think that forbiddenapis jar will go into ${build.dir}. However I get that error:
Problem: failed to create task or type forbiddenapis
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.
The files don't get downloaded into your workspace. The cachpath task will do two things, download and cache jars into the default directory "~/.ivy2/cache" and then create an Ant path based on those cached jars.
Secondly, as #Denis Kurochkin pointed out, the task you're using apparently requires a namespace to be declared, not unusual with modern Ant tasks.
Finally I couldn't resist demonstrating how you can also configure your ANT build to install the ivy jar if it is missing, making your build even more stand-alone.
Example
build.xml
<project name="demo" default="forbidden-checks" xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:fa="antlib:de.thetaphi.forbiddenapis">
<available classname="org.apache.ivy.Main" property="ivy.installed"/>
<target name="resolve" depends="install-ivy">
<ivy:cachepath pathid="classpath">
<dependency org="de.thetaphi" name="forbiddenapis" rev="2.2" />
</ivy:cachepath>
<ivy:cachepath pathid="all-lib-classpath">
<dependency .... />
<dependency .... />
<dependency .... />
</ivy:cachepath>
</target>
<target name="forbidden-checks" depends="resolve">
<taskdef uri="antlib:de.thetaphi.forbiddenapis" classpathref="classpath"/>
<fa:forbiddenapis classpathref="all-lib-classpath" dir="${build.dir}" targetVersion="${javac.version}">
<bundledsignatures name="jdk-unsafe"/>
<bundledsignatures name="jdk-deprecated"/>
<bundledsignatures name="jdk-non-portable"/>
</fa:forbiddenapis>
</target>
<target name="install-ivy" unless="ivy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
<fail message="Ivy has been installed. Run the build again"/>
</target>
</project>
You need to declare namespace for forbiddenapis task from Ivy:
<project xmlns:ivy="antlib:org.apache.ivy.ant" xmlns:fa="antlib:de.thetaphi.forbiddenapis">
...
<fa:forbiddenapis ... >
Or declare task name explicitly:
<taskdef name="forbiddenapis"
classname="de.thetaphi.forbiddenapis.ant.AntTask"
classpath="path/to/forbiddenapis.jar"/>
Anyway look at the documentation https://github.com/policeman-tools/forbidden-apis/wiki/AntUsage

Ant task for jar

Compiling works fine, but it's not packaging into a jar file.
The final message is BUILD SUCCESSFUL with compiled classes but I cannot find a jar.
<project name="thisIsMyProject" basedir="." default="build">
.
.
.
.
.
Load Classpath and such.....
.
.
.
.
<target depends="init" name="build-project">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="my.classpath"/>
</javac>
</target>
<mkdir dir="build"/>
<target name="jar" description="Make My Jar File" depends="init">
<jar destfile="myFile.jar" destdir="build" basedir="bin" compress="true" />
</target>
</project>
It looks like your target names are wrong.
Your default target is "build", but you don't actually have a target called "build" - perhaps you should change the default to "jar"
Your "jar" target depends on "init", but it should probably also depend on "build-project" if you want it to compile BEFORE it packages the jar.

How to make a JAR file from a subset of project classes?

I have a Java project that has just about every class and package depending on everything else. I am trying to untangle it. My current idea is to extract core classes, that shouldn't depend on anything else and package them as a separate JAR that the rest of the application will use. I am using Ant and Netbeans. I am a complete Ant newbie, thought I started reading a book.
I have tried to make an Ant build file that will compile and package the core classes in a JAR. However, no matter what I do, the JAR ends up containing every application class. I am not sure if this is because some of my core classes depend on other classes, that get automatically pulled into the JAR, or because I can't get the Ant directives right. Here is my current build file:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Core" default="jar">
<description>Compiles and packages Core packages</description>
<target name="init">
<mkdir dir="build/classes"/>
<mkdir dir="dist"/>
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes"/>
</target>
<target name="jar" depends="compile">
<jar destfile="dist/core.jar" basedir="build/classes">
<zipfileset dir="build/classes" includes="core/**,xml/**,file/**,exceptions/**"/>
</jar>
</target>
</project>
Any advice?
What is the use of zipfileset entry? Following should be enough to filter in select folders to the jar.
<target name="jar" depends="compile">
<jar destfile="dist/core.jar" basedir="build/classes" includes="core/**,xml/**,file/**,exceptions/**" />
</target>

Ant compile doesn't copy the resources

I created my own build.xml which has:
<target name="compile">
<mkdir dir="build"/>
<javac destdir="build">
<src path="src"/>
</javac>
</target>
<target name="build" depends="compile">
<mkdir dir="dist"/>
<jar destfile="dist/app.jar" basedir="build" />
</target>
<target name="run" depends="compile">
<java classname="webserver.Loader" classpath="build" fork="true" />
</target>
It works great. When I call ant run so it compiles and runs my application, but my application has a package with icons and it isn't moved to a folder "build" so my application ends with an exception that it couldn't locate my icons. When I move them by myself then it works.
I tried to use
<copy todir="build/app/icons">
<fileset dir="src/app/icons"/>
</copy>
It works, but I would like to do it without the copy command. Is there any parameter to javac? Or something else?
Thank you for answer.
There is no such parameter. You can copy all sorts of files between your directories with:
<copy todir="build">
<fileset dir="src"
includes="**/*.xml,**/*.properties,**/*.txt,**/*.ico" />
</copy>
Sorry, you will need to copy non-java files manually. Resources are technically not "source". The command-line javac will not copy resource files from your source directory to the output directory, neither will ant's javac task.
You can do this using the fileset element of the jar task instead of manually copying the files. For example:
<jar destfile="dist/app.jar" basedir="build">
<fileset dir="src" includes="app/icons/**" />
</jar>
This will copy everything in src/app/icons/ to the app/icons path in your .jar file.
No, there isn't. The copy task is the correct way to copy resources into your build folders.

Categories

Resources