Ant Javac compile subpackage class files to the parent src directory - java

I would like Ant 1.9 to be able to compile servlet classes to the parent src directory, but the file system has the files in packages. No, the packages are not declared in the servlet files.
The code is deployed with all the servlets in the same directory. I supposed I can create an ant copy command to do this, but I would prefer there be an ant javac solution.
Manually this would be 'javac -d .. *.java' after changing to each subdirectory, not including the files already in the default package. This is highly irregular, but I cannot change the how the packages are defined nor can I change the urls from which the code is executed from.
<target name="compileServlet" description="Compiles Java source files.">
<javac srcdir="${servlets.dir}" destdir="$(servlets.dir}" debug="true" encoding="ISO-8859-1" source="1.7" target="1.7" failonerror="true">
<classpath path="${env.CLASSPATH}" />
<include name="**/*.java"/>
</javac>
</target>
Right now when I run this ant build.xml, no class files are generated. Any thoughts how I can solve this issue?

If you have source files in multiple root directories and want to compile them all into a single root directory, use the <src> element instead of the srcdir attribute.
Here is the example shown in the documentation:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>

Related

Compiling 1 file with Javac / Ant also compiles imported file/class in specified file

The task at hand is to separately compile Java classes and their associated JUnit 'Test' classes using Ant-script.
The regular classes are stored in 'src', while the test classes are stored in 'test/src'. Regular classes should be compiled to 'bin' and test classes to 'test/bin'. Both are in the same package.
My Ant script looks as follows:
<javac
includeantruntime="false"
classpathref="master-classpath"
destdir="${test.class.build.dir}"
>
<src path="${src.dir}"/>
<src path="${test.class.dir}"/>
<include name="**/*Test*.java"/>
</javac>
And running the script shows me only one file will be compiled:
[javac] Compiling 1 source file to C:\Users\AK_Flex\Desktop\HW01\test\bin
However, the test class as well as the regular class it imports (already compiled in 'bin') are being compiled and outputted to the 'test/bin' folder.
The regular classes do not import the test classes, so 'bin' looks as desired. (code not depicted)
Is there any way to circumvent this behavior of the compiler?
Since you want compiled classes in two different folders, you need two compilation steps.
<javac includeantruntime="false"
srcdir="src"
destdir="bin"
classpathref="master-classpath">
</javac>
<javac includeantruntime="false"
srcdir="test/src"
destdir="test/bin">
<classpath>
<pathelement location="bin"/>
<path refid="master-classpath"/>
</classpath>
</javac>

equivalent of mvn build-classpath in ant

I am beginner in Maven and Ant projects.I have a trouble with some Ant tasks.I don't know how to build class-path of dependencies in Ant; while it can be done in maven projects by:
mvn dependency:build-classpath
How can i do above task in Ant?
You can specify it using classpath tag in your build.xml you can refer example below
<target name="compile" depends="init" description="compile the source ">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<classpath>
<fileset dir="${hadoop.home}/">
<include name="**/*.jar"/>
</fileset>
<fileset dir="${hadoop.home}">
<include name="hadoop-common-*.jar"/>
</fileset>
</classpath>
</javac>
</target>
Ant is more like an imperative programming language. You don't have the amount of meta-information in your build.xml as you have in a pom.xml. The easiest thing would be to add some code to your ant "compile" target that writes the classpath to a file when you do a compile.
You can also externalise the classpath in a separate libraries.xml file (that was done in our company when we used Ant) which allows you to edit and read the data more easily. You could even write another ant target for that.

ant cannot identify my jar files at all

I have my jar files in Assignment2\lib folder and my build file is in Assignment2. The name of the jar file is Assignment1.jar The following is how I tried to compile my Assignment2 from build file through ant.
<project name="Assignment1" default="run" basedir=".">
<property name="classes" value="classes" />
<path id="project.class.path">
<pathelement location="src"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<path id="lib.jars">
<fileset dir="lib" includes="**/*.jar" />
</path>
......
<target name="compile" description="compaling java files with Assignment2">
<mkdir dir="build"/>
<javac srcdir="src" destdir="build" debug="on" failonerror="true">
<classpath refid="project.class.path"/>
<classpath refid="lib.jars"/>
</javac>
</target>
<Project/>
I am getting compile errors. I'm using windows. Is that the problem? Is there any way to compile?
Well,Everything looks good to me but, you can try the following to validate the same,
Check if the lib folder has all the necessary library jars required to compile you application
You can try assigning the relative path of the lib folder to a property and refer it in the classpath eg: Property lib value=".\Lib"
Try the following before you call the compile target to see the list of jars being included in the classpath
<pathconvert property="libjars" refid="lib.jars"/>
<echo>libjars is ${classpathProp}</echo>
hey guys thanx for the answers actually i found probelm. i was trying to use a jar file created by netbeans files. Apparently netbeans jar files can be used only by netbeans. at the end i made a new jar file of Assingment1 and used it. now everything is fine. Thanx again for the answers.

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>

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