Ant compile doesn't copy the resources - java

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.

Related

Refresh doesn't work before creating a war with Ant on Eclipse

On Eclipse I create war files by using ant.
The issue is that in the war file isn't included the right mypropfile.properties.
The file is properly copied, but also if I use <eclipse.refreshLocal resource="projectdir" depth="infinite"/> the old file is included. I have to refresh manually the project.
For Ant I use the "Run in the same JRE as the workspace" option.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" basedir=".">
<description>
My Project
</description>
<property name="workspace.dir" value="${basedir}/../../"/>
<property name="src" value="${basedir}/../src"/>
<property name="build" value="${basedir}/../build"/>
<property name="build.classes" value="${basedir}/../build/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="web.dir" value="${basedir}/WEB-INF"/>
<property environment="env"/>
<property name="real.dir" value="${basedir}/real"/>
<property name="real2.dir" value="${basedir}/real2"/>
<path id="classpath.server">
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"/>
<pathelement path="${build.classes}"/>
</path>
<path id="classpath.app">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="refreshResource" if="eclipse.refreshLocal">
<eclipse.refreshLocal resource="projectdir" depth="infinite"/>
</target>
<target name="clean">
<delete dir="${build}/classes"/>
<delete dir="${build}"/>
</target>
<target name="init" depends="clean, refreshResource">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/classes"/>
</target>
<target name="compile" depends="init">
<javac encoding="UTF8" srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<path refid="classpath.server.bin"/>
</classpath>
<classpath>
<path refid="classpath.server"/>
</classpath>
<classpath>
<path refid="classpath.app"/>
<fileset dir="${lib.dir}" includes="*.jar"/>
</classpath>
</javac>
</target>
<target name="deleteConfig">
<delete file="${src}/mypropfile.properties"/>
</target>
<target name="real" depends="deleteConfig">
<copy file="${real.dir}/realprop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="real2" depends="deleteConfig">
<copy file="${real2.dir}/real2prop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="war-real" depends="real, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
<target name="war-real2" depends="real2, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
EDIT
The target clean was wrong, so I've corrected it, but now build failed with error
BUILD FAILED ... Reference classpath.server.bin not found.
Ant doesn't care if Eclipse has refreshed the file or not. eclipse.refreshLocal is only relevant for editors and compilers inside of the IDE.
When you run the Ant build.xml, Ant copies the file in question in the real target into the source folder and compile copies it into ${build}/classes (at least it should do that). So before you create the WAR, you must make sure the compile step has done its work (i.e. look into each file to make sure that a change is visible in each copy).
What worries my is that you use different ways to access the classes:
${build}/classes
${build.classes}
${basedir}/../build/classes
So the first step should be to define a single way to locate the folder and then use this pattern everywhere.
If that doesn't solve your problem, you need to make sure Ant notices that the file has changed. Old filesystems like FAT support only timestamps which have second resolution. If you use an USB stick for your sources, it's possible to change the file and run Ant so fast that Ant thinks the file hasn't changed.
Lastly, you need to check your classpath. If one of the JAR dependencies also contains a file called mypropfile.properties, then Java resource loading can find either version.
This and other problems made me use a different solution to configure WAR files: I pass a system property with the absolute path of the config file. That way, the WAR file doesn't change when the config changes and I have full control over which config file is loaded.

Ant does not include generated class files to packaged jar, until I do a Project->Clean in Eclipse

I ran into some strange behaviour that seems to be connected to Eclipse and it's way to refresh the workspace.
I'm using an Ant build file to create my class-files and to create a package from that class-file. This functionality is working fine.
This is the target doing the work:
<target name="package" depends="compile,javaDoc">
<jar destfile="${dist.dir}/MyApplication.jar" basedir="${build.dir}" includes="**/*.class">
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="MyApplication" />
</manifest>
</jar>
</target>
Now I want to add functionality to first clean the directories where the class-files and the jar file are beeing generated.
This is the target code:
<target name="clean" description="Removes all *.class and *.jar files. Also deletes the java doc files.">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" /></target>
This target also works fine.
Now I would like the package target dependent on the clean target, so that the folders are first cleaned before the packaging is beeing done.
This would look like this:
<target name="package" depends="clean,compile,javaDoc">
<jar destfile="${dist.dir}/MyApplication.jar" basedir="${build.dir}" includes="**/*.class">
<zipgroupfileset dir="${lib.dir}" includes="*.jar" />
<manifest>
<attribute name="Main-Class" value="MyApplication" />
</manifest>
</jar>
</target>
The problem is, that this target does not include the generated class files into the jar. Even on the file system the generated class files are not displayed. Only after I go to Project->Clean in Eclipse and clean the project the class files become visible on the filesystem.
Any help is appreciated.
My compile target looks like this:
<target name="compile" depends="init" description="Compiles the source file to the created directory.">
<javac classpathref="classpath" srcdir="${dist.dir}" destdir="${build.dir}" includeantruntime="false" />
</target>
fixing the error in the compile target worked.
That is the working version:
<target name="compile" depends="init" description="Compiles the source file to the created directory.">
<javac classpathref="classpath" srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false" /></target>

Ant build creates incorrect relative paths to jars when generating manifest

I'm using Ant to build my Java application and to generate the MANIFEST.MF file automatically so it includes all the jars in my lib directory.
This seems to work but the problem is that instead of writing them as lib/some.jar, it includes my Eclipse project's name: MyProject/lib/some.jar.
This is ofcourse incorrect and causes none of the jars to be found when run as a standalone app.
Build.xml (important part is at the end):
<?xml version="1.0"?>
<project name="fidea_migration" default="dist">
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="cleaning the old deliverables">
<delete includeemptydirs="true">
<fileset dir="bin" includes="**/*"/>
</delete>
<delete includeemptydirs="true">
<fileset dir="_deliverables" includes="**/*"/>
</delete>
</target>
<target name="prepare" description="preparing the deliverables folders">
<mkdir dir="_deliverables/lib"/>
</target>
<path id="jarlib">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
</path>
<manifestclasspath property="lib.list" jarfile=".">
<classpath refid="jarlib" />
</manifestclasspath>
<target name="compile" depends="clean, prepare" description="compiling java sources">
<mkdir dir="bin"/>
<javac srcdir="src/main/java" destdir="bin">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="dist" depends="compile" description="creating binary distribution">
<copy todir="_deliverables/lib">
<fileset dir="lib"/>
</copy>
<copy todir="_deliverables">
<fileset dir="src/main/resources">
</fileset>
</copy>
<jar jarfile="_deliverables/lib/app.jar" basedir="bin">
<manifest>
<attribute name="Class-Path" value="${lib.list}"/>
</manifest>
</jar>
</target>
</project>
Example of how my Manifest looks:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.4
Class-Path: MyProject/lib/All-MB.jar MyProject/lib/activation.jar MyProject/lib/aspectjrt.jar
Any idea on how to fix this so it just shows xxx.jar instead of MyProject/lib/xxx.jar (so without "MyProject/lib/")?
Cheers,
Bart
manifestclasspath expects the jarfile attribute to point to the location of the JAR file (which probably doesn't exist yet, but that's fine). Since you're creating the JAR file at _deliverables/lib/app.jar and you're also copying all the lib JARs from lib to _deliverables/lib then
<manifestclasspath property="lib.list" jarfile="lib/app.jar">
<classpath refid="jarlib" />
</manifestclasspath>
should do the trick, and will create an eventual Class-Path with the right relative paths, i.e. All-MB.jar activation.jar aspectjrt.jar etc. etc.
You must set your project Dir inside your build.xml as,
<property name="projectDir" value=".." />
After this, you must try to work on everything with relative path. Currently, the reason behind problem is that the absolute path is being used.

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>

How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse

I'm working on a small library for our in-company use, and have been heavily documenting it. Now I'm building my jar with the following code:
<project name="commonutils" default="compile" basedir=".">
<property name="src" location="src" />
<property name="build" location="buildDirecotry" />
<target name="compile">
<delete file="${ant.project.name}.jar" />
<mkdir dir="${build}"/>
<javac srcdir="${src}" destdir="${build}" debug="on" target="1.5">
<classpath>
<pathelement location="lib/build/server.zip" />
<pathelement path="${java.class.path}/"/>
</classpath>
</javac>
<jar basedir="${build}" destfile="${ant.project.name}.jar" />
<delete dir="${build}" />
</target>
</project>
Which works fine, it builds my jar file with all the src files in it, but when I include the jar file in another project I no-longer have any of my javadoc comments. Using JDDecompiler I cannot see the comments in the class file, although I'm not sure if its the java compiler that's stripping them or JD.
My question is: How can I build my jar file so that users who use the library will be able to see the javadoc in Eclipse.
If you include the source files in the jar (each class and java file in the same package-directory) it should work.
<target name="jar.noCompile.src">
<jar destfile="${ant.project.name}.jar">
<fileset dir="${build}"/>
<fileset dir="${src}" includes="**/*.java"/>
</jar>
</target>
AFAIK the documentation is an Eclipse feature. You have to configure it manually. In your build generate the documentation (usually into folder 'javadoc') and package it with the JAR. Once someone wants to use your library, he/she has to go into Java Build Path select libraries, add yours, click next to it to open the tree node and then double click on Javadoc location to configure it.

Categories

Resources