How to include default package in Ant javac Task - java

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**

Related

Setting up Annotation Processor with Dependencies using Apache Ant

I have an annotation processor that is depended on other libraries e.g. Guava, Commons. I already set it up with Eclipse and Maven. However, I want to use it with an Apache Ant's build file. So, I created following target:
<target name="compile.frontend" depends="compile.main">
<mkdir dir="${target}target/frontend/classes" />
<javac encoding="utf-8" destdir="${target}target/frontend/classes" debug="on" nowarn="true" source="1.6" target="1.6" optimize="on" includeantruntime="false">
<src path="src/application/java"/>
<classpath>
<fileset dir="lib">
<include name="main/*.jar" />
</fileset>
<pathelement location="${target}target/main/classes" />
</classpath>
<compilerarg line="-processorpath ${basedir}/lib/main/MyProcessor-1.0.jar;${basedir}/lib/main/javapoet-1.7.0.jar;${basedir}/lib/main/guava-17.0.jar;${basedir}/lib/main/commons-lang-2.6.jar"/>
<compilerarg line="-processor com.me.MyProcessor"/>
<compilerarg line="-s ${target}target/frontend/classes/"/>
</javac>
</target>
The error I get is the processor-generated class I instantiated is missing, "cannot find symbol".
The dependencies I added with a ";" separated list are the same dependencies I made Eclipse use the processor properly. I suspect from the output folder <compilerarg line="-s..., since when I remove a dependency i.e. JavaPoet I get a "java.lang.NoClassDefFoundError" of JavaPoet. Any suggestion?
The problem was regarding some directory exclusion using exclude name under javac lines:
<exclude name="**/folder_name/**"/>
There is also no need to add compilerarg lines as I did at the original question. Having the libs under classpath is enough.

Compiling with ANT using external libs

I'm new with ANT and I'm trying to compile part of my Eclipse project. I have many classes but i need to pack only a part of it (please don't ask why). My problem is that one of these classes references to an external library placed in the <project_root>/libs folder and I did not find out how to link it. I found examples on the web but I was not able to arrange it.
<path id="classpath">
<fileset dir="libs" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="client/classes"/>
<javac srcdir="src" destdir="client/classes" sourcepath="classpath">
//include needed java files
</javac>
</target>
I'm using the annotation #Remote of EJB. It's in the javax.ejb package. I get the error:
package javax.ejb does not exist
[javac] import javax.Ejb.Remote;
If I understand your project structure correctly, the issue with your ant file is that you reference the classpath as beeing the sourcepath.
sourcepath / sourcepathref point to locations where source can be found. I suppose what you want is classpathref="classpath".
<javac srcdir="src" destdir="client/classes" classpathref="classpath">
//include needed java files
</javac>
try this one
<classpath>
<fileset dir="libs">
<include name="**/*.jar"/>
</fileset>
</classpath>

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

ant javac exclude everything not included

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.

Package Problem with Ant

I'm having a problem getting the javac used by Ant to find and use certain packages. When I invoke javac directly from the command line the packages are found and used.
The .jar files are located in my home directory under lib/java. This is my classpath:
/home/bliskovs/lib/java/*:/home/bliskovs/vendor/cytoscape-v2.7.0/cytoscape.jar
This is the relevant section in my build.xml:
<target name="compile">
<javac srcdir="." debug="true"/>
<javac srcdir="tools/" debug="true"/>
<javac srcdir="core/" debug="true"/>
</target>
How can I get Ant to recognize these packages?
Check out this.
<property name="build.classes.dir" location="build/classes"/>
<path id="compile.classpath">
<fileset dir="lib"/>
<pathelement location="/home/bliskovs/vendor/cytoscape-v2.7.0"/>
</path>
<target name="compile" description="Compile src dir">
<javac destdir="${build.classes.dir}" debug="true" includeantruntime="true">
<src location="src"/>
<classpath refid="compile.classpath"/>
</javac>
</target>
Define a classpath for the javac task. Relying on the CLASSPATH environment variable is a bad practice. It's even more true for the build process of a project, which should work without having to setup a whole lot of environment variables. If you start developing three or four projects at once, you'll understand why using a single CLASSPATH env variable is a bad idea.
See http://ant.apache.org/manual/Tasks/javac.html to know how to define a classpath inside the build.xml and use it in the javac task.

Categories