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

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.

Related

How to build jar-file with ant?

According to the training task, I need to create .jar-file with ant. There are no problems with the Assembly of ordinary classes that are in src .
But I have in project present logger, libraries to him and .properties file.
because of this, errors fly out:
[javac] error: package org.apache.logging.log4j does not exist
and
error: cannot find symbol
[javac] error: private static final Logger logger = LogManager.getLogger(Hello.class);
I have build.xml
<?xml version="1.0"?>
<project name="Runner" default="run">
<!-- define names of directories -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="classes" location="${build}/classes"/>
<!-- define all targets -->
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false"/>
</target>
<target name="run" depends="compile">
<java classname="${ant.project.name}" classpath="${classes}"/>
</target>
<target name="clean">
<delete dir="${build}"/>
</target>
<target name="package" depends="compile">
<jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
<manifest>
<attribute name="Main–Class" value="${ant.project.name}"/>
</manifest>
</jar>
</target>
</project>
and this stracture
How and where to gather library and property in Java file to jar-file was executable?
I figured it out.
<path id="classpath">
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init" depends="clean" description="starts">
<tstamp/>
</target>
<target name="clean" depends="package-to-jar" description="clean up">
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-to-jar" depends="package-external-lib" description="packing all project into a jar-file">
<jar destfile="${jar}/${ant.project.name}.jar" basedir="${classes}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipfileset src="${external-lib}"/>
</jar>
<delete dir="${classes}"/>
<delete file="${external-lib}"/>
</target>
<target name="package-external-lib" depends="compile" description="packing external libraries into a jar-file">
<jar destfile="${external-lib}">
<zipgroupfileset dir="${lib}">
<include name="**/*.jar"/>
</zipgroupfileset>
</jar>
</target>
<target name="compile" description="compile the source">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" classpathref="classpath"/>
<copy todir="${classes}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
Firstly you need include required library folders to class path
<path id="class.path">
<fileset dir="lib">
<include name="**/*.jar" />
</fileset>
</path>
and add reference to javac task in compile target
<target name="compile">
<mkdir dir="${classes}"/>
<javac srcdir="${src}" destdir="${classes}" includeAntRuntime="false">
<classpath refid="class.path" />
</javac>
</target>
aftewards include all properties file to jar
<target name="package" depends="compile">
<jar destfile="${build}/${ant.project.name}.jar" basedir="${classes}">
<include name="*.properties"/>
<manifest>
<attribute name="Main–Class" value="${ant.project.name}"/>
</manifest>
</jar>
</target>

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.

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>

using less utility in ant

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>

Ant not able to locate external jar files. Showing java.lang.NoClassDefFound Error exception

I am creating a jar file using following target -
<target name="jar" depends="compile">
<jar destfile="${jar.dir}/TargetClass.jar"
basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
</target>
and I'm trying to execute the jar file using following target -
<target name="runjar">
<java jar="${jar.dir}/TargetClass.jar" fork="true"/>
</target>
I've used apache poi jar in my TragetClass which is located under D:/Jar directory. While executing it's not able to locate the poi jars and showing java.lang.NoClassDefFoundError exception.
You need to use a classpath. Something like:
<property name="poi.jars" value="path to poi jars"/>
<path id="run.classpath">
<pathelement location="${poi.jars}"/>
</path>
<target name="runjar">
<java
jar="${jar.dir}/TargetClass.jar"
fork="true"
<classpath refid="run.classpath"/>
/>
</target>
See Ant Java Task for more information.

Categories