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>
Related
I have a java project with java files more than 2500 and some of them may have compilation issue. I need to generate classfiles and route to a particular folder. Even with some of the compilation error, rest of jave turned to class files, with eclipse.
But I need to compile with build tools like ANT but it stops as build failed.
Hence no classfiles generated. Is there a way to compile and generated when project has some compilation error using ANT. The sample code is like
<?xml version="1.0"?>
<project name="REL854" basedir="." default="compile">
<target name="create" depends="delete">
<mkdir dir="src"/>
</target>
<target name="delete">
<delete dir ="src" />
<echo>exclude not working</echo>
</target>
<target name="copy" depends="create">
<copydir src="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\src" dest="C:\Users\username\neon\ANTBuildFor854\build\src" excludes="Samples"></copydir>
<copy todir ="C:\Users\username\neon\ANTBuildFor854\build\lib" overwrite="true">
<fileset dir="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\lib" ></fileset>
</copy>
</target>
<target name="compile" depends="copy"></target>
<javac failonerror="false" includeantruntime="false" srcdir="C:\Users\username\neon\ANTBuildFor854\build\src" destdir="C:\Users\username\neon\ANTBuildFor854\build\bin" includes="**/*.java"></javac>
</project>
I am trying to write a task that will combine all the tasks in by build.xml
<project name="HW4_Build">
<target name="mkedir">
<mkdir dir="bld/class/cscie55/hw4"/>
</target>
<target name="clean">
<delete dir="bld"/>
</target>
<target name="compile" depends="clean, mkedir">
<javac destdir="bld/class" srcdir="src/cscie55/hw4"/>
</target>
<target name="jar" depends="compile">
<jar destfile="HW4.jar" basedir="bld/class"/>
</target>
</project>
Is following method right way to go about this?
<ant antfile="build"/>
In this build.xml you might just want to try:
<project name="HW4_Build" default="jar">
Because the jar target already includes the other targets via the dependency chain.
<ant antfile="build"/>
Is used for invoking another build file. Not sure that's what you want.
<antcall target="target"/>
Use this to define a target that combines all your other targets.
Although this won't exactly work in your case since you are using depends... So you only need to call your last target in the dependency chain.
How can I build a separate jar for a specific package in a project?
I have a project structure like this:
Project Something
|
|-> DifferentPackages
Or a jar for each package using a build.xml?
In your build xml, define one task each to produce one jar. With in the task, use fileset to include only specific package.
Once done, create a wrapper task and make it dependent on all the jars tasks created above or wrap all jar creation tasks in the new task itself. Make this a default target.
e.g.
<project name="project" default="AllJars" basedir=".">
<target name="AllJars" depends="compile">
<mkdir dir="dist"/>
<jar jarfile="dist/jar1.jar">
<fileset dir="build">
<include name="package1/*.class"/>
</fileset>
</jar>
<jar jarfile="dist/jar2.jar">
<fileset dir="build">
<include name="package2/*.class"/>
</fileset>
</jar>
</target>
</project>
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.
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.