using less utility in ant - java

I am trying to write a target in ant to display files using the unix utility less but for some reason it keeps failing to build because of this target. I have pasted my whole ant file below including this target. It keeps saying this:
Attribute name "PlayTour.java" associated with an element type "less" must be followed by the ' = ' character.
What would be causing this? I am rather new to ant so any help would be greatly appreciated.
Ant file:
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
<target name="view">
<less "PlayTour.java KnightsTour.java"/>
</target>
</project>

less isn't a valid ant task. As you've pointed out, it's a unix command.
You're probably looking for the exec task.
Try something like:
<target name="view">
<exec executable="less">
<arg value="PlayTour.java" />
<arg value="KnightsTour.java" />
</exec>
</target>

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.

Running java classes with ant

I am trying to run my java program using ant. The compile and jar part work perfectly fine. But when u try to run my created Jar, it shows me
Error: Could not find or load main class com.twu.biblioteca.Application.
Cannot seem to figure out the problem
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/Application.jar" basedir="build/classes/com/twu/biblioteca/">
<manifest>
<attribute name="Main-Class" value="com.twu.biblioteca.Application"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="build/jar/Application.jar" fork="true"/>
</target>
The problem is your jar task. From the documentation, basedir corresponds to:
the directory from which to jar the files.
As such, it needs to be
<jar destfile="build/jar/Application.jar" basedir="build/classes">
<!-- ... -->
</jar>
With your configuration, you are making a JAR of only the biblioteca folder, therefore, it doesn't match anymore the package declaration that contains com.twu.biblioteca.

ant build file terminating tag error

I am trying to run an ant build file for a java jar. For some reason, whenever I try to run ant run, it comes back and says, "Element of type target must be terminated by the matching </target> tag" This seems strange because I see that I do have a terminating tag for run. Would there be something else that could be causing this? Any help would be greatly appreciated!
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
<target name="view">
<exec executable="less">
<arg value="PlayTour.java" />
<arg value="KnightsTour.java" />
</exec>
</target>
<target name= "doc">
<mkdir dir="build/docs"/>
<javadoc sourcefiles="KnightsTour.java, PlayTour.java" destdir="build/docs"/>
</target>
</project>
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar"/>
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
You are closing the Java tag in the block above twice, first using /> in-line then writing </java>, which causes your error. This is what it looks like fixed:
<target name= "run" depends = "jar">
<java fork='yes' jar="build/jar/KnightsTour.jar" >
<arg value="${rows}"/>
<arg value="${columns}"/>
<arg value="${attempts}"/>
</java>
</target>
Whenever you suspect an XML validation error, you should use a free online XML validation utility to at least confirm that the structure of your file is correct. This is what I used to quickly locate your error: http://www.xmlvalidation.com/index.php?id=1&L=0

run target in ant that depends on jar

I have a jar target in ant that makes a jar out of two classes that I have. Then, I want to run this jar in another target that depends on the jar target. Would there be an easy way to do this in ant? I have my compile and jar targets pasted below.
<project>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="." destdir="build/classes"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/KnightsTour.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="PlayTour"/>
</manifest>
</jar>
</target>
</project
To run a Java application, use the java task (see https://ant.apache.org/manual/Tasks/java.html for its documentation). An example from the docs:
<java jar="dist/test.jar" fork="true" failonerror="true" maxmemory="128m">
<arg value="arg1"/>
<arg value="arg2"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>

how to add source code to jar file using ant build.xml

I want to add all my source code to jar file using ant while creating jar file from source code.
I want to have two files 1- myProject.jar 2-myproject_source.jar
What should i use and where should i put it?
<project name="myProject" >
<target name="clean">
<delete dir="./build"/>
</target>
<target name="compile">
<mkdir dir="./build/classes"/>
<javac srcdir="./src" destdir="./build/classes"/>
</target>
<target name="jar">
<mkdir dir="./build/jar"/>
<jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes">
<manifest>
<attribute name="DependencyFinder" value="main"/>
</manifest>
</jar>
</target>
</project>
Add this just before your <manifest> line:
<fileset dir="./src" includes="**/*.java"/>
You can add mulitple <fileset> statements if you want.

Categories