ant javac exclude everything not included - java

I am trying to write a build script for a REST service which sits on top of our existing business logic layer, however, I only want to include the minimal amount of sources to keep the service small and only contain what it absolutely needs.
Below is my current compile target. I am able to either include everything or nothing. I assume I am making a simple mistake I can't seem to spot or find online.
<target name="compile">
<mkdir dir="${build.classes.dir}"/>
<javac source="1.6"
target="1.6"
encoding="UTF-8"
debug="true"
debuglevel="lines,vars,source"
srcdir="${basedir}"
destdir="${build.classes.dir}"
includeAntRuntime="false">
<src>
<dirset dir="${src.eai.dir}" errorOnMissingDir="true">
<include name="common/vo/MyPojo.java"/>
<include name="common/SomeException.java"/>
</dirset>
<dirset dir="${src.ets.dir}" errorOnMissingDir="true">
<include name="common/vo/AnotherPojo.java" />
<include name="price/vo/YetAnotherPojo.java" />
<include name="price/vo/OneMorePojo.java" />
</dirset>
<dirset dir="${src.java.dir}" errorOnMissingDir="true">
<include name="java"/>
</dirset>
</src>
<!-- this line ignores everything, without it it includes everything -->
<exclude name="**/*.java"/>
<classpath refid="classpath"/>
</javac>
</target>
Is there a way to only include the files specified above?

In place of exclude, try include and list your java files separated with comma(,) e.g:
<include name="common/vo/MyPojo.java,common/SomeException.java,common/vo/AnotherPojo.java,price/vo/YetAnotherPojo.java" />

Don't set both the srcdir attribute and the nested <src> element, as I imagine Ant is simply combining the two.

Related

Ant: How to include library in javadoc?

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?

How can I change the name of the jar file after siging it - Apache ANT

I am signing my jars using following ant commands.
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
signedjar="${dist.dir}/${jar.signed.fileName}"
lazy="true">
<fileset dir="${dist.dir}">
<include name="*.jar" />
<include name="lib/*.jar" />
</fileset>
</signjar>
For single file it is fine that I can change the name of the jars file after signing it,
but when I have multiple jars the above line is not useful, what i am trying to accomplish is ,
for example
If the unsigned jar file name is ab.jar after signing it I want to prependSigned_to it's name likeSigned_ab.jar` and so on for all the jars i have in my fileset.
Can anyone tell me how to do that ?
You can use the ant jar command/task like below in your build script :
<jar basedir="bin" destfile="Signed_${jar-name}.jar">
btw, you missed to type the command you're using, in your question.
The signjar task can take a mapper defining how to translate the input file name into an output file name:
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
destdir="${dist.dir}"
lazy="true">
<fileset dir="${dist.dir}">
<include name="*.jar" />
<include name="lib/*.jar" />
<!-- since we're dumping signed JARs in the same dir as the source
ones, we need to prevent already-signed JARs from being signed
again. A better approach might be to put the Signed_* JARs in
a different directory -->
<exclude name="**/Signed_*" />
</fileset>
<regexpmapper handledirsep="yes"
from="^(.*?)/([^/]*)$$" to="\1/Signed_\2" />
</signjar>
May be you can write couple of macros & call 'signJarsParallel' macro once for every directory for which you want to sign jars.
This is not tested code. You can give it a try. It makes use running tasks parallely & hence increasing speed & signs to temporary-prefixed name you want :
<macrodef name="signMyJars">
<element name="myJar" implicit="true"/>
<sequential>
<signjar alias="${alias}" keypass="${keypass}"
storepass="${storepass}"
keystore="${keystorefile}"
lazy="true">
<myJar/>
</signjar>
</sequential>
</macrodef>
Call only below macro inside your ant target.
<macrodef name="signJarsParallel">
<attribute name="dirName"/>
<sequential>
<for param="file" parallel="true" threadCount="5">
<path>
<fileset dir="#{dirName}">
<include name="*.jar"/>
<include name="lib/*.jar"/>
</fileset>
</path>
<signMyJars>
<fileset file="Signed_#{file}.jar"/>
</signMyJars>
</for>
</sequential>
</macrodef>

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?

How do I build a list of file names?

I need to write an Ant target that appends together (comma-delimited) a list of '.jar' file names from a folder into a variable, which is later used as input to an outside utility.
I am running into barriers of scope and immutability. I have access to ant-contrib, but unfortunately the version I am stuck with does not have access to the 'for' task. Here's what I have so far:
<target name="getPrependJars">
<var name="prependJars" value="" />
<foreach param="file" target="appendJarPath">
<path>
<fileset dir="${project.name}/hotfixes">
<include name="*.jar"/>
</fileset>
</path>
</foreach>
<echo message="result ${prependJars}" />
</target>
<target name="appendJarPath">
<if>
<equals arg1="${prependJars}" arg2="" />
<then>
<var name="prependJars" value="-prependJars ${file}" />
</then>
<else>
<var name="prependJars" value="${prependJars},${file}" />
</else>
</if>
</target>
It seems that 'appendJarPath' only modifies 'prependJars' within its own scope. As a test, I tried using 'antcallback' which works for a single target call, but does not help me very much with my list of files.
I realize that I am working somewhat against the grain, and lexical scope is desirable in the vast majority of instances, but i really would like to get this working one way or another. Does anybody have any creative ideas to solve this problem?
You might be able to use the pathconvert task, which allows you to specify the separator character as comma.
<target name="getPrependJars">
<fileset id="appendJars" dir="${project.name}/hotfixes">
<include name="*.jar" />
</fileset>
<pathconvert property="prependJars" refid="appendJars" pathsep="," />
<echo message="prependJars: ${prependJars}" />
</target>
I'd simply write a custom task in Java that (1) takes the folder name, (2) assembles the result string and (3) stores it to the ${prependJars} property.
In ant you just define the task (taskdef) and use like all other tasks afterwards.
I did it once when I was faced with a simliar problem and found that it was very, very easy.
Here's the tutorial.
If a system path format is useful to you, you can use the following:
<target name="getPrependJars">
<path id="prepend.jars.path">
<fileset dir="${project.name}/hotfixes">
<include name="*.jar"/>
</fileset>
</path>
<property name="prependJars" value="${toString:prepend.jars.path}" />
<echo message="result ${prependJars}" />
</target>

How to include default package in Ant javac Task

When I:
<javac srcdir="${src}" destdir="${build.classes.dir}" classpathref="classpath">
<include name="ObjectInDefaultPackage"/>
<include name="com/mypackage/**"/>
</javac>
It will no longer add compile and add the class ObjectInDefaultPackage that's in the default package (ie. it's not packaged, it's sitting on the man ${src} directory). If I remove the includes it adds it fine, but once I set at least one include/exclude, I can no longer find a way to add it.
If I do:
<javac srcdir="${src}" destdir="${build.classes.dir}" classpathref="classpath">
</javac>
Then it compiles the ObjectInDefaultPackage...
Use this:
<include name="ObjectInDefaultPackage*"/>
<include name="com/mypackage/**"/>
Without slashes, Ant will search in the target directory, which is src. Or use *.java.
It's not recommended to have classes in the default package though.
I just discovered the issue. Stupid mistake. It's what happens when you're so use to dealing with wildcards for so long.
The answer is:
<javac srcdir="${src}" destdir="${build.classes.dir}" classpathref="classpath">
<include name="ObjectInDefaultPackage.java"/>
<include name="com/mypackage/**"/>
</javac>
Notice ObjectInDefaultPackage has been changed to ObjectInDefaultPackage**.java**

Categories