Got my hands on an old script from year 2000.
It's all in Java and there is a build file which compiles it all:
<?xml version="1.0"?>
<project name="chat" default="compile" basedir=".">
<target name="init">
<property name="conf.dir" value="conf" />
<property name="src.dir" value="src" />
<property name="resource.dir" value="resource" />
<property name="build.dir" value="build" />
<property name="release.dir" value="release" />
<property name="etc.dir" value="etc" />
<property name="lib.dir" value="lib" />
</target>
<target name="clean" depends="init">
<delete dir="${build.dir}" />
<delete dir="${release.dir}" />
</target>
<target name="compile" depends="init">
<mkdir dir="${build.dir}" />
<javac srcdir="${src.dir}" destdir="${build.dir}" includeAntRuntime="false" debug="true" optimize="false" />
</target>
<target name="release" depends="compile">
<mkdir dir="${release.dir}" />
<jar destfile="${release.dir}/chat_service.jar" basedir="${build.dir}" includeantruntime="false" />
</target>
</project>
When I run it with ant in Windows the command prompt says:
Buildfile: build.xml
init:
compile: BUILD SUCCESSFUL
But it doesn't create a jar file.
All it does is copy the exact project and compile it into class files.
Does anyone know how I can create the right file?
It's a chat server and I need it to run my client.
The default target of the build file is compile:
<project name="chat" default="compile" basedir=".">
So if you invoke ant without specifying a target, the compile target will be executed. To execute the release target, use
ant release
You should add another target section for generating the jar file.
For instance :
<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}" />
<attribute name="Class-Path" value="config/ properties/ ${manifest.classpath}" />
</manifest>
</jar>
</target>
at the end, of course you should configure that block for your own file and properties.
Related
I am trying to build jar for my project using jdk 1.8.0_40 and ant version 1.9.0, jar is getting build but while converting this jar to .exe, it is not able to find main class as I have correctly mentioned my main class.I am using eclipse Mar Version: Mars.2 Release (4.5.2).
Earlier I was using jdk 1.7.0_79 version and ant 1.8.4 and everything was working fine.
I am facing issue while upgrading from jdk 1.7.0_79 to jdk 1.8.0_40.
Below is my ant xml which are using to build my jar.Let me know if any configuration is required for java 1.8 to build proper jar.
<property name="jar.name" value="SAFAL.jar" />
<property name="source.root" value="src" />
<property name="class.root" value="bin" />
<property name="lib.dir" value="lib" />
<property name="jar.dir" value="C:\D\SAFAL-Exe" />
<property name="Main-Class" value="com.sungard.ktt.main.SAFALEval" />
<property name="conf.pkj" value="com/sungard/ktt/business/configurations" />
<property name="img.pkj" value="com/sungard/ktt/business/images" />
<path id="project.class.path">
<pathelement location="${class.root}" />
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<target name="clean" description="cleans up build structures">
<delete dir="${class.root}" />
<delete file="${jar.dir}/${jar.name}" />
</target>
<target name="prepare" description="sets up build structures">
<mkdir dir="${class.root}" />
</target>
<target name="compile" depends="prepare" description="Compiles all java classes">
<javac srcdir="${source.root}" destdir="${class.root}" debug="on" optimize="off" deprecation="on" source="1.8" target="1.8" includeantruntime = "false">
<classpath refid="project.class.path" />
</javac>
<mkdir dir="${class.root}/${conf.pkj}" />
<mkdir dir="${class.root}/${img.pkj}" />
<copy todir="${class.root}/${conf.pkj}">
<fileset dir="${source.root}/${conf.pkj}" />
</copy>
<copy todir="${class.root}/${img.pkj}">
<fileset dir="${source.root}/${img.pkj}" />
</copy>
</target>
<target name="jar" depends="compile">
<delete file="${jar.dir}/${jar.name}" quiet="true" failonerror="false" />
<jar destfile="${jar.dir}/${jar.name}">
<fileset dir="${class.root}" includes="**/*.*" />
<fileset dir="${source.root}" includes="**/api/*.java,**/api/vo/*.java"/>
<zipgroupfileset dir="${lib.dir}" />
<manifest>
<attribute name="Main-Class" value="${Main-Class}" />
<attribute name="Class-Path" value="." />
</manifest>
</jar>
</target>
<target name="run">
<java fork="true" classname="${Main-Class}">
<classpath>
<path location="./${jar.name}" />
</classpath>
</java>
</target>
In your java task you are using ./${jar.name} as the location of your jar - this is relying on the current directory being set correctly. All the rest of your code uses ${jar.dir}/${jar.name} as the path for the jar, so change the java task to do the same:
<target name="run">
<java fork="true" classname="${Main-Class}">
<classpath>
<path location="${jar.dir}/${jar.name}" />
</classpath>
</java>
</target>
I was able to generate a jar file with manifest.mf. However, when i run the jar file in the command prompt, it looks for the oracle driver. I'm pretty sure that I have added this in eclipse project build path.
My build.xml:
<?xml version="1.0"?>
<project name="converter" default="main" basedir=".">
<property name="projectName" value="Converter" />
<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="lib.dir" value="lib"/>
<property name="dist.dir" location="dist" />
<property name="dist.lib.dir" location="dist/lib" />
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="name" value="converter"/>
<property name="main-class" value="com.ouc.mv90.conversion.CSVtoMV90Converter" />
<target name="init">
<mkdir dir="${build.dir}" />
</target>
<!-- external libraries classpath, we don't need sources and javadoc -->
<path id="classpath">
<fileset dir="${basedir}/">
<include name="${lib.dir}/*.jar" />
<exclude name="${lib.dir}/*sources.jar"/>
<exclude name="${lib.dir}/*javadoc.jar"/>
</fileset>
</path>
<!-- To work with external libraries, need classpath to compile -->
<target name="compile" depends="init" description="compile the source ">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
</target>
<!-- constructs the external libraries classpath name -->
<pathconvert property="classpath.name" pathsep=" ">
<path refid="classpath" />
<mapper>
<chainedmapper>
<flattenmapper />
<globmapper from="*.jar" to="lib/*.jar" />
</chainedmapper>
</mapper>
</pathconvert>
<target name="copy-dependencies">
<copy todir="${dist.lib.dir}">
<fileset dir="${lib.dir}" includes="**/*.jar" excludes="**/*sources.jar, **/*javadoc.jar" />
</copy>
</target>
<!-- jar it, and declares the ext libraries in manifest.mf file -->
<target name="jar" depends="compile, copy-dependencies" description="package, output to JAR">
<echo message="classpath.name : ${classpath.name} " />
<mkdir dir="${dist.dir}" />
<mkdir dir="${dist.lib.dir}" />
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
<attribute name="Class-Path" value="${classpath.name}" />
</manifest>
</jar>
</target>
<target name="clean" description="clean up">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Default, run this -->
<target name="main" depends="clean, compile, jar" />
</project>
MANIFEST.mf
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_43-b01 (Sun Microsystems Inc.)
Main-Class: com.ouc.mv90.conversion.CSVtoMV90Converter
Class-Path: lib/ojdbc6.jar
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
it's me again.
I've been trying to create java project into a runnable jar using ant script.
This is my build.xml
<project name="simple-app" 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="lib.dir" value="lib" />
<property name="main-class" value="app.App" />
<path id="classpath">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
<dirset dir="${build.dir}">
<include name="classes"/>
</dirset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac source="1.7" target="1.7" includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="${main-class}">
<classpath>
<path refid="classpath" />
<path location="${jar.dir}/${ant.project.name}.jar" />
</classpath>
</java>
</target>
<target name="main" depends="clean,run"/>
</project>
All goes well, but when it gets to "run" it gets stuck. Terminal says run: and nothing happens. Waited for 30 minutes, nothing. I tried many other options I found around the internet but those resulted either in the same lag, or threw ClassNotFoundException.
I seriously don't know what, to do. When I make the file with Eclipse, all works fine. Anyone can help me? It's propably something totally stupid, but I just don't see it.
Thank you very much.
To fix the ClassNotFoundException exception you need to include the classpath in the jar's manifest. The manifestclasspath task comes in very useful:
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<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}" includes="*.class">
<manifest>
<attribute name="Main-Class" value="${main-class}" />
<attribute name="Class-Path" value="${jar-classpath}" />
</manifest>
</jar>
</target>
This enables you to invoke the jar as follows:
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
or from the command-line:
java -jar /path/to/myproject.jar
In the end this may not explain why your build is hanging.... Is it possible the code is going into a waiting state?
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"/>