Can I build multiple projects from one build-file. Example:
<project basedir="." default="all" name="app1">
...
</project>
<project basedir="." default="all" name="app2">
...
</project>
Currently I type ant -f build1.xml compile and it builds my application and I have to use two separate build files. Is there some way to get it running in a way that i have both the projects defined a common build-file and I can type something like ant app1 compile or ant app2 compile?
Here's what my build-file looks like:
<?xml version="1.0" encoding="UTF-8"?>
<project name="azebooster" default="dist" basedir=".">
<!-- Globals -->
<property name="src" location="src/com/aelitis"/>
<property name="build" location="build/azebooster"/>
<property name="jar" location="jar/azebooster"/>
<property name="resources" location="res/azebooster"/>
<!-- Paths -->
<path id="classpath">
<fileset dir="." includes="**/*.jar"/>
</path>
<!-- Start it -->
<target name="init">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${jar}"/>
</target>
<!-- Build it -->
<target name="compile" depends="init" description="compile the source" >
<javac srcdir="${src}" destdir="${build}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
<!-- Jar it -->
<target name="jar" depends="compile">
<jar destfile="${jar}/${ant.project.name}.jar">
<fileset dir="${build}"/>
<fileset dir="${resources}" />
</jar>
</target>
<!-- Clean it -->
<target name="clean" description="clean up" >
<tstamp/>
<delete dir="${build}"/>
<delete dir="${jar}"/>
</target>
</project>
Thank you.
Yes you can create a default.build file (in this way you don't need to specify the file, because it's used by default). On it you can create the following targets:
<target name="all" depends="app1, app2" />
<target name="app1">
<ant antfile=<app1file> target="compile" />
</target>
<target name="app2">
<ant antfile=<app2file> target="compile" />
</target>
On this way you can use both ant file from one unique file.
And you can do all in only one file, if you replace the app1 and app2 targets, with the needed targets to compile them that you have in the separate files.
EDITED:
You have different ways to include both of them in only one file, maybe the easiest one is include a suffix for each project on each target. And you can call the specific project target or the target for both.
I put you an example with the compile target (and for init target too).
You can use compile for compile both projects (I call the other project other), and compileazeboster to compile the azeboster project.
You can search the common things to avoid the innecesary duplicated code (common paths, targets and so on)
<property name="srcazebooster" location="src/com/aelitis"/>
<property name="buildazebooster" location="build/azebooster"/>
<property name="jarazebooster" location="jar/azebooster"/>
<property name="srcother" location="src/other"/>
<property name="buildother" location="build/other"/>
<property name="jarother" location="jar/other"/>
<!-- Start it -->
<target name="init" depends="initazebooster, initother"/>
<!-- Build it -->
<target name="compile" depends="compileazebooster, compileother" description="compile the source for all" />
<!-- Start azebooster-->
<target name="initazebooster">
<tstamp/>
<mkdir dir="${buildazebooster}"/>
<mkdir dir="${jarazebooster}"/>
</target>
<!-- Build azeboster-->
<target name="compileazebooster" depends="initazebooster" description="compile the source for azebooster" >
<javac srcdir="${srcazebooster}" destdir="${buildazebooster}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
<!-- Start other-->
<target name="initother">
<tstamp/>
<mkdir dir="${buildotherr}"/>
<mkdir dir="${jarother}"/>
</target>
<!-- Build other-->
<target name="compileother" depends="initother" description="compile the source for other" >
<javac srcdir="${srcother}" destdir="${buildother}">
<classpath>
<path refid="classpath"/>
</classpath>
</javac>
</target>
You will probably be able to do what you want using macros (though it depends precisely on what is common between your two projects), though your command is more likely to be
ant compile app1
or
ant compile app2
where compile is a target which calls a macro, using app1/app2 as a parameter to either decide which macro to call or to pass into the macro itself.
I would like to add to the excellent answer of Borja some important steps.
How to organize the common ant files?
Let's say we have some big Project consisting of many Eclipse projects. All of them are in a worspace folder. Thus, the workspace itself makes the global Project. (Notice P/p difference). Very often you already have an Ant build file in the workspace folder. Or you have created the common ant files (build and properties) in it by yourself.
Later you want to launch that global build file. How? Do you want to go into the workspace folder, change to the command line or shell window and run the ant from there, having the output in the external console and switching from Eclipse to that window and back? And the same problem for editing? Having two additional windows? No, surely you want to have all windows, editing and output, in Eclipse. But how can we reference those global build files from inside the Eclipse?
Go to Package Explorer.
Make an empty project(not Java one), let's name it _Global (we want to see it always on the top).
Right click on its name. Choose Import -> General -> File System.
Press Advanced. Check Create links in Workspace, Create virtual folders, create link locations. The last choose for WORKSPACE_LOC.
In From Directory go to the workspace or where you have created the common build. After all, it could be even the common one for several workspaces.
You will see a dialogue with file names on the right with checkfields. Check the files you need. (You will need to import every new common file you created thereafter).
Finish.
Now you see your project with references to the global files. As for properties files, you are ready. But for build files you need some more steps:
Right click your global build file, -> Run -> Run Configurations. You are in the Ant Build group of configurations.
Press the New Launch Configuration button with plus on it (above the launch configurations list). Now you have the run configuration for that global build.
Set its parameters and run/debug it.
Related
I have a problem with Ant Build Tool.
First, below you can see my project structure:
and the content of my build.xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<project name="addonGenerator" default="main" basedir=".">
<property name="projectName" value="addonGenerator"/>
<property name="src.dir" location="src"/>
<property name="build.dir" location="bin"/>
<property name="dist.dir" location="dist"/>
<target name="compile" description="compile the source ">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement path="lib/velocity-1.7.jar"/>
<pathelement path="lib/log4j-1.2.16.jar"/>
</classpath>
</javac>
</target>
<target name="dist" description="package, output to JAR">
<mkdir dir="${dist.dir}"/>
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<zipgroupfileset dir="lib" includes="velocity-1.7.jar" />
<zipgroupfileset dir="lib" includes="log4j-1.2.16.jar" />
<manifest>
<attribute name="${projectName}" value="main"/>
<attribute name="Main-Class" value="main.java.AddonGenerator"/>
</manifest>
</jar>
</target>
<target name="clean" description="clean up">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="main" depends="clean, compile, dist"/>
</project>
I don't know how setup the Ant build.xml to build and run my project with external libraries and the java property file generator.properties
To include your generator.properties file in the .jar file, add your resources directory when building the .jar:
<jar jarfile="${dist.dir}/${projectName}.jar" basedir="${build.dir}">
<fileset dir="src/main/java/resources"/>
Since you are currently building a “fat jar” (by directly including the contents of your library .jars in your application .jar), you can run by simply invoking your .jar file. Such a target obviously requires the .jar file to be built, so it makes sense to depend on the "dist" target:
<target name="run" depends="dist">
<java jar="${dist.dir}/${projectName}.jar"/>
</target>
On another note, I don’t think you want to pass src as your source directory, unless your classes actually declare themselves with ‘package main.java;’ (which they shouldn’t). You should pass the actual root of your packages to the javac task:
<property name="src.dir" location="src/main/java"/>
You should also make the "dist" target depend on "compile", since, well, it depends on having compiled classes available.
I also would suggest that your default target, "main", avoid calling the "clean" target. You should not clean before every single build; that defeats one of the most useful benefits of Ant, namely the ability to update only the things that need to be updated. You should only clean when you need to, with a command like ant clean compile or simply ant clean.
Note that once "dist" depends on "compile", and once "main" no longer calls "clean", you can simply remove the "main" target and change your project’s default target to "dist". When you think about it, this makes sense: the default action is to build and package the application.
I need to create build file for ant to build my JavaFX project, I have searched a lot, but nothing helped me. It still show errors but compiles. When I tries to run jar file - exceptions. I have tried different paths, but still nothing.
Here is my build.xml.
<?xml version="1.0" encoding="UTF-8" ?>
<project name="JDBC Ant Project" default="default" basedir="." xmlns:**fx="javafx:com.sun.javafx.tools.ant"**(Uri is not registered)>
<property name="src.dir" location="src"/>
<property name="build.dir" location="classes"/>
<property name="out.dir" location="out"/>
<property name="docs.dir" location="docs"/>
<property name="bin.dir" location="bin"/>
<property name="lib.dir" location="lib"/>
<property name="jar.name" value="javafxtest.jar"/>
<property name="sdk.dir" location="/usr/lib/jvm/java-8-oracle/lib"/>
<target name="default" depends="clean,compile">
<path id="fxant">
<filelist>
<file name="/usr/lib/jvm/java-8-oracle/lib/ant-javafx.jar"/>
<file name="/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar"/>
</filelist>
</path>
<*taskdef*(Failed to load types) resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpathref="fxant"/>
<fx:**application** id="HelloWorldID"
name="JDBC Java FX"
mainClass="Main"/>
<fx:resources id="appRes">
<fx:fileset dir="${out.dir}" includes="HelloWorld.jar"/>
</fx:resources>
<fx:jar destfile="${out.dir}/${jar.name}">
<fx:application refid="HelloWorldID"/>
<fx:resources refid="appRes"/>
<fileset dir="${build.dir}"/>
</fx:jar>
<fx:**deploy width**="300" **height**="250"
**outdir**="." **embedJNLP**="true"
**outfile**="helloworld">
<fx:**application** refId="HelloWorldID"/>
<**fx:resources** refid="appRes"/>
<**fx:info** title="JavaFX Hello World Application"
vendor="Oracle Corporation"/>
</fx:**deploy**>
</target>
<target name="clean">
<echo>Performing clean target</echo>
<delete dir="${build.dir}"/>
<delete dir="${docs.dir}"/>
<delete dir="${out.dir}"/>
</target>
<target name="init">
<echo>Performing init target</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${docs.dir}"/>
<mkdir dir="${out.dir}"/>
</target>
<target name="compile" depends="clean, init">
<echo>Performing compiling</echo>
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="build.classpath"/>
</javac>
</target>
<path id="build.classpath">
<fileset dir="${lib.dir}" casesensitive="no">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="javadoc" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**"/>
<exclude name="**/resources/**"/>
</fileset>
</javadoc>
</target>
<target name="build" depends="compile">
<jar destfile="${out.dir}/${jar.name}" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
</jar>
</target>
<target name="main" depends="compile, build, javadoc">
<description>Main target</description>
</target>
</project>
** is fully "red" in my IDEA (Intellij IDEA.
* is underlined .
But nevertheless it builds using ant -f build.xml. But when I tries to run jar file I am getting next exceptions.
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.....
.....
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
at Main.start(Unknown Source)
Update on copying resources using JavaFX ant tasks
There is no folder with fxml, css and other files in my output jar file. If put it manually everything works, how to say ant to exlictly include folder ?
Based on the Java Deployment Tutorial JavaFX Ant Tasks HelloWorld build.xml sample, and modifying it to add a resources directory (which is a sibling to the project src, classes and dist directories). Place your fxml and css in the resources directory to get them included in the jar. The directory structure of the copied files will match the directory structure of the resources directory, so if you just put them in the resources directory with no sub-directories, the files will show up in the root of the jar file (so when you use the resources reference them relative to the root (e.g. FXMLLoader.load(getResource("/main.fxml"))). I made these modifications without testing as I don't use ant for builds anymore.
<property name="build.src.dir" value="src"/>
<property name="build.resources.dir" value="resources"/>
<property name="build.classes.dir" value="classes"/>
<property name="build.dist.dir" value="dist"/>
<target name="default" depends="clean,compile">
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath="${JAVA_HOME}/lib/ant-javafx.jar"/>
<fx:application id="HelloWorldID"
name="JavaFXHelloWorldApp"
mainClass="HelloWorld"/>
<fx:resources id="appRes">
<fx:fileset dir="${build.dist.dir}" includes="HelloWorld.jar"/>
</fx:resources>
<fx:jar destfile="${build.dist.dir}/HelloWorld.jar">
<fx:application refid="HelloWorldID"/>
<fx:resources refid="appRes"/>
<fileset dir="${build.classes.dir}"/>
<fileset dir="${build.resources.dir}"/>
</fx:jar>
. . .
</target>
You likely have a runtime issue not a build issue
It would seem that the application builds fine and you are getting a runtime error trying to run your application, either due to an issue in your application code or because the resources required for execution are not present.
Try a simpler application which does not include any FXML and build and execute that - if that works than either your error was in your application code or in the code which copies the FXML resources to your build package.
On Intellij syntax highlighting of JavaFX Ant tasks
Regarding the "URI is not registered" error in Intellij, that is a bit of a red-herring. It just means that you haven't registered the schema for the fx namespace with Idea, so Idea cannot validate the document (and provide context sensitive code completion on XML tags). As long as you haven't made syntax or structure errors in your XML (which you probably haven't or ant would likely reject it), then you can ignore such error messages if you wish.
You can find more information on this here:
Intellij Idea Help - Referencing DTD or Schema
Note: I don't think Oracle provide a full XML schema for the JavaFX ant tasks, so it will probably not be possible for you to configure Idea to validate the JavaFX ant task elements of your ant build.xml file. However, that should not prevent you from building your application - your best policy is probably to configure Idea to ignore the JavaFX ant tasks XML schema, so it no longer displays annoying and misdirecting red highlights on your build.xml file.
Alternative Technology
You may (or may not) find using the JavaFX Maven plugin or JavaFX Gradle plugin a better solution for you than using the JavaFX Ant Tasks directly.
Hello all how can I create jar file of my project that is created in android using eclipse. I have done in this way project -> right-click->export --> select builder -->antbuilder-->ok then after this process build.xml will be created. On bulid.xml I will create new builder.
Right click on project -> properties --> select new -->give build.xml and your project path and press ok, new builder will be created.
Now select that builder from project properties and press ok.
Now build your project and your jar will be created in bin folder.
I have followed above process but my jar is not found in bin folder. I can see my build.xml is created and all process goes smooth but still jar is not created. Can any one tell me how can I do this?
My build.xml
<?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="testtttttttttt">
<property environment="env"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="Android 2.2.libraryclasspath">
<pathelement location="C:/Program Files (x86)/Android/android-sdk/platforms/android-8/android.jar"/>
</path>
<path id="com.android.ide.eclipse.adt.LIBRARIES.libraryclasspath"/>
<path id="testtttttttttt.classpath">
<pathelement location="bin/classes"/>
<path refid="Android 2.2.libraryclasspath"/>
<path refid="com.android.ide.eclipse.adt.LIBRARIES.libraryclasspath"/>
</path>
<target name="init">
<mkdir dir="bin/classes"/>
<copy includeemptydirs="false" todir="bin/classes">
<fileset dir="src">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy includeemptydirs="false" todir="bin/classes">
<fileset dir="gen">
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="bin/classes"/>
</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/classes" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="testtttttttttt.classpath"/>
</javac>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin/classes" source="${source}" target="${target}">
<src path="gen"/>
<classpath refid="testtttttttttt.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>
</project>
In Eclipse, select project, right click on the project, Select Export, From that select Jar. Then follow the simple wizard and give the destination where you want to save your jar, and finish. You can find your jar there.
Step 1. Create Android library project as explained here.
Step 2. Now you need to give reference of Android Library project created in step 1 to your client application. There are two ways to do so.
•Give reference of Android Library Project itself by client app
property -> Select 'Android' on left pane -> In Libraty Section, Add
Android Libraty project (This is explained at link given in Step 1
above)
•Give the reference of .jar file of Android Library project
(from Location of Android Library project -> bin -> .jar file).
Client Application -> Properties -> Click 'Java Build Path' in lefe
pane -> Go to 'Libraries' Tab -> Click 'Add External JARs' button and
select the .jar file -> Go to 'Order and Export' tab and select the
added .jar file reference and move it to top.
Hope this will help you
I am having a little trouble with my first ever ant build in eclipse, here is my build.xml build file.
<project name="Rutherford" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<property name="libs" value="libs"/>
<path id="classpath">
<fileset dir="${libs}" includes="**/*.jar"/>
</path>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" classpathref="classpath">
<compilerarg line="-encoding utf-8"/>
</javac>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/MyProject-${DSTAMP}.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="nat.rutherford.DesktopStarter"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="${dist}/MyProject-${DSTAMP}.jar" fork="true"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
It compiles ok with no warnings or errors, but when I try to run the .jar it says 'Could not find the main class: nat.rutherford.DesktopStarter. Program will now exit' =(
I have read a ton of pages on the matter but so far nothing conclusive.
I was able to compile it using Eclipse -> File -> Export ->Java -> Runnable Jar File. But I use some UTF-8 encoded .txt files that it seems not to be able to deal with that way and I need them! ie I have greek characters that should read...dσ/dΩ... but currently read... dÃ/d©... which isn't going to work ^^
So basically I need to make my Ant build work, baring in mind that it needs to be able to handle my UTF-8 encoded .txt files too.
The problem is in your task dist when you create your jar. If your compilation is right and there is no problem when you package your jar. Things that are wrong:
<mkdir dir="${dist}/lib"/> -> this don't have mean, you don't use it never
second you are not including your libraries in your jar, then when you try to execute your jar it doesn't work that why you are seeing the error message Could not find the main class: nat.rutherford.DesktopStarter. Program will now exit You could see that your libraries aren't within your jar using Winzip or similar. I suppose that you are seeing your problem when you try to execute the jar directly using windows or similar. A good way to see what is happening, seeing the problem printed in the console is executing your jar in the next way: java -jar MyProject-20120102.jar
See: How to include your libraries in you jar?
And if you want to know more about jar packaging using ant try this.
Another thing that you need to modify the Class-path attribute in your manifest to include the libraries within your ${libs} folder.
It looks like you've added a manifest to your executable JAR that spells out nat.rutherford.DesktopStarter as your main class.
I'd recommend that you open the JAR and verify that the manifest.mf appears and does indeed say what your Ant build.xml does.
I'd also verify that your DesktopStarted.class appears in a folder path nat.rutherford. If it doesn't, the JVM won't find it.
I have a spring web application in Eclipse Helios. I use Ant for my builds on other projects but am not familiar with initially setting up Ant build files. Is there a painless way to make the transition from using Eclipse to do my builds, to creating an Ant build file and any supporting files? I tried copying a basic Ant build file into my application but I'm not sure if it's even close to what I need. I've included it below. It's giving me this error when I run it.
BUILD FAILED
C:\..\..\workspace\..\build.xml:21: Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK.
My system JAVA_HOME path is set to 'C:\Program Files\Java\jdk1.7.0\bin'
<?xml version="1.0" encoding="UTF-8"?>
<project name="projectName" default="dist" basedir=".">
<description>
build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the News-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/ProjectName-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
Don't include the bin folder in your JAVA_HOME
Try this:
C:\Program Files\Java\jdk1.7.0