Ant: How to include library in javadoc? - java

I have made an Ant build file for my Java project. I'm using the matlabcontrol library, i.e. I have the following files:
lib: matlabcontrol-4.1.0.jar
lib/src: matlabcontrol-4.1.0-sources.jar
lib/docs: matlabcontrol-4.1.0-javadoc.jar
I have two questions regarding my build file:
<path id="project-classpath">
<fileset dir="${lib.dir}" includes="*.jar" />
</path>
Will this also include the source and javadoc file? Of course I don't want to include them.
Second, I'm creating the javadoc with the following code:
<target name="docs" depends="clean docs, compile">
<mkdir dir="${docs.dir}" />
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
Is there a possibility to also include the javadoc and sources of the matlabcontrol library or is this usually not done?

Related

Not able to create Javadoc using Ant

I am trying to build my project using Ant. While creating javadocs, I am getting one error as shown below :
If error is not clearly visible in below image refer this :
[javadoc] javadoc: error - Illegal package name: "E:\junoWrkspace\StrutsHelloWorld\src\com\igate\resources\ApplicationResources.properties"
Script which I am using in build.xml for creating javadocs is as follows :
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
I had a similar problem and exluding non .java files (like the .properties file) did the trick for me.
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
</javadoc>
Try replacing
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
with just
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}"/>
Please read the error message! (and #ReOffender answer)
[javadoc] javadoc: error - Illegal package name: "E:\junoWrkspace\StrutsHelloWorld\src\com\igate\resources\ApplicationResources.properties"
javadoc try to generate docs from .properties files that's why javadoc doesn't find "package directive", You MUST ask your target to only generate javadoc from .java file and anything else
So #ReOffender is right:
Change your <include name="**" /> with <include name="**/*.java" />

Include libs folder containing .jars for compilation

I have the following file structure:
ServerCode <- src , libs, bin
I am trying to compile all the code in src. Src has a couple of .java files at the top level and sub-directories. libs contains all the .jar files which are required to build my project.
I wrote the following build.xml but when I try to compile it, the compiler throws errors cannot find symbol errors for the libraries I am including.
<project default="compile">
<target name="compile">
<mkdir dir="bin"/>
<javac srcdir="src" destdir="bin" classpath="libs/*.jar">
</target>
</project>
Define class path to include all jars like this
<target name="compile" depends="" description="compile the java source files">
<javac srcdir="." destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${test_lib}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
I don't think you can use a pattern in the classpath attribute. I could be wrong about this. You should run ant in verbose mode (the -v option) to see how it's using your classpath attribute. I suspect it's passing it to javac as a literal string.
Here's what I do:
<javac target="${javac.target}" source="${javac.source}" destdir="${validator.output.dir}" debug="on"
nowarn="off"
memorymaximumsize="128m" fork="true">
<classpath refid="validator.module.production.classpath"/>
<src>
<dirset dir="Validator">
<include name="src"/>
</dirset>
</src>
</javac>
...
<path id="validator.module.production.classpath">
<fileset dir="Validator/lib/-validator" includes="**/*.jar"/>
</path>
Some of this code is generated by my IDE so it's a little verbose, but you get the idea.
Try this as mentioned at http://ant.apache.org/manual/using.html instead of giving classPath attribute alongwith javac
<classpath>
<pathelement location="libs/*.jar"/>
</classpath>
There are other ways also which you can glance thru the link mentioned above

Can't find added jar files

I want to add the Gson jar files, this is what I've done to build.xml (the important part):
<property name="libs.dir" location="web/WEB-INF/lib" />
<path id="build.classpath">
<fileset dir="${libs.dir}/gson">
<include name="*.jar" />
</fileset>
<fileset dir="${libs.dir}/tomcat">
<include name="*.jar" />
</fileset>
</path>
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath refid="build.classpath" />
</javac>
The compiling goes great, but when I'm trying to start the application, it keeps saying that it can't find com.google.gson
java.lang.NoClassDefFoundError: com/google/gson/Gson
I've tryed adding several other jars, and with every one of them it says no class found. However, the tomcat jar's are working. Is there perhaps something I'm missing?

Ant include external .jar

I want to include external jar to my java project. I'm using ant. External .jar is in folder lib. My build.xml looks something like that:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" classpathref="classpath" />
</target>
<target name="jar">
<mkdir dir="trash"/>
<jar destfile="trash/test.jar" basedir="build">
<zipgroupfileset dir="lib" includes="**/*.jar"/>
<manifest>
<attribute name="Main-Class" value="com.Test"/>
</manifest>
</jar>
</target>
<target name="run">
<java jar="trash/test.jar" fork="true"/>
</target>
</project>
But it doesn't work. When I want to import something from the external .jar, there is an error after command ant compile: package com.something does not exist.. What should I edit to get it working?
Exact error:
Compiling 23 source files to xy/build
xy/src/com/Test.java:5: package com.thoughtworks.xstream does not exist
import com.thoughtworks.xstream.*;
^
1 error
You should try without the includes attribute:
<fileset dir="lib" />
And in the jar part you include the classes like this:
<zipgroupfileset includes="*.jar" dir="lib"/>
You can't put external libraries into a jar and expect the classloader to use those jars. Unfortunately this is not supported.
There are ant tasks like one jar that help you, to create a jar file, that contains everything you need.
This bit is from the background information of one jar:
Unfortunately this is does not work. The Java Launcher$AppClassLoader
does not know how to load classes from a Jar inside a Jar with this
kind of Class-Path. Trying to use
jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end.
This approach will only work if you install (i.e. scatter) the
supporting Jar files into the directory where the jarname.jar file is
installed.
Another approach is to unpack all dependent Jar files and repack them
inside the jarname.jar file. This approach tends to be fragile and
slow, and can suffer from duplicate resource issues.
Other Alternative:
jarjar: Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution
I also use ant to include a number of dependency JARs in my JAR. My compile task looks like this. Perhaps something similar will work for you.
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}" includeantruntime="false">
<classpath>
<pathelement path="${classpath}" />
<fileset dir="${deps}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
<copy todir="${build}">
<fileset dir="${src}" excludes="**/*.java"/>
</copy>
</target>
sometimes u can use jar contents directly, just unzip
<unzip src="/Developer-Java/mysql-connector-java/mysql-connector-java-5.1.22-bin.jar" dest="${build_dir}" />

How to jar java source files from different (sub-)directories?

Consider the following directory structure:
./source/com/mypackage/../A.java
./extensions/extension1/source/com/mypackage/../T.java
./extensions/extension2/source/com/mypackage/../U.java
...
./extensions/extensionN/source/com/mypackage/../Z.java
I want to produce a source jar with the following contents:
com/mypackage/../A.java
com/mypackage/../T.java
com/mypackage/../U.java
...
com/mypackage/../Z.java
I know I could use a fileset for each source directory.
But is there an easy solution using ANT without having to refer to all extensions explicitly?
How about flattening all the files to be included in the archive into a single directory structure, then archiving from there?
Use a regexpmapper to do the flatten during copy, something like this:
<delete dir="merged" />
<mkdir dir="merged" />
<copy todir="${basedir}/merged">
<fileset dir="${basedir}">
<include name="source/**"/>
<include name="extension*/**"/>
</fileset>
<regexpmapper from=".*source/(.*)" to="\1" />
</copy>
<jar destfile="mypackage.jar" filesonly="yes">
<fileset dir="merged">
<include name="**" />
</fileset>
</jar>

Categories