How to compile java project using Apache ANT with the JRE system libraries and other libraries i have.
The code i am using is
<path id="master-classpath">
<fileset dir="junit">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<target name="build" description="Compile source tree java files">
<mkdir dir="${build.dir}"/>
<javac destdir="${build.dir}" source="1.5" target="1.5" includeantruntime="false">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
</target>
its only taking the jar files in the junit folder.
I dont know how to compile with JRE system libraries.
Related
Currently, we define a path with
<path id="sources.production">
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
and then compile using
<javac ...>
<src refid="sources.production"/>
<classpath refid="classpath.production">
</javac>
Now we create some modified .java files (original files in src/module2) before this <javac> call and have put them before the other sources:
<path id="sources.production">
<pathelement path="generated-sources"/>
<pathelement path="src/module1"/>
<pathelement path="src/module2"/>
...
</path>
Unfortunately, the compile fails now because the original and the modified .java files are both fed to the javac task. How to exclude the original source files easily from the javac's sources without large charges?
you need an intermediate step which you copy all your source files to a new location and override them with generated ones, then use this new location as source folder for javac.
it may look something like this:
<path id="sources.4compile" location="all-sources" />
<target name="prepare-4compile">
<!-- clean -->
<delete dir="all-sources"/>
<mkdir dir="all-sources"/>
<copy todir="all-sources">
<fileset dir="src/module1"/>
<fileset dir="src/module2"/>
<fileset dir="generated-sources"/>
</copy>
</target>
<target name="compile" depends="prepare-4compile">
<javac ...>
<src refid="sources.4compile" />
<classpath refid="classpath.production" />
</javac>
</target>
other way, you may specify a fileset to javac disabling javac's default searching mechanism as suggested here.
The ant compile target below compiles all .java files in any of the src folders specified using a <src path="..."/> tag. You can see that a number of items are added to the classpath using <classpath refid=...> tags.
<target name="compile">
<echo message="${ant.project.name}: ${ant.file}"/>
<deps-load-path conf="core" pathid="core.ivy.classpath" />
<deps-load-path conf="test" pathid="test.ivy.classpath" />
<javac debug="true" includeantruntime="false" debuglevel="source,lines,vars" destdir="${bin.path}" source="1.8" target="1.8">
<src path="${xtext.project.path}/src"/>
<src path="${xtext.project.path}/src-gen"/>
<src path="${project.path}/src"/>
<src path="${project.path}/src-gen-umpletl"/>
<src path="${project.path}/src-gen-umple"/>
<src path="${project.path}/test"/>
<src path="${vendors.path}/jopt-simple/src"/>
<exclude name="**/.git"/>
<exclude name="**/*.ump" />
<exclude name="**/data" />
<classpath refid="project.classpath"/>
<classpath refid="validator.project.classpath"/>
<classpath refid="core.ivy.classpath" />
<classpath refid="test.ivy.classpath" />
<!-- Add compiler arguments here, see https://ant.apache.org/manual/using.html#arg for details, example below
<compilerarg value="-Xlint:deprecation" />
-->
</javac>
<copy todir="${bin.path}" overwrite="true">
<fileset dir="${project.path}/src"><include name="**/*.grammar"/></fileset>
<fileset dir="${project.path}/src"><include name="**/*.error"/></fileset>
</copy>
<delete file="cruise.umple/src/rules.grammar"/>
<delete file="cruise.umple/bin/rules.grammar"/>
</target>
One of these items is project.classpath, which is defined elsewhere in the file as the same location as ${bin.path}, which is where the compiled files are written to (see the <copy todir...> tag). How can ${bin.path} be both a classpath dependency and the location where .class files are written to? I know for a fact that ${bin.path} gets deleted before each build, so there isn't anything in the folder prior to the compile target being run. Are the .class files that are produced as the compile target executes added to the classpath dynamically?
When I'm using external libraries (lucene) and running my java application through eclipse (run application) all works fine with the libs in the classpath.
But when I'm using Ant, i got this error here:
java.lang.ClassNotFoundException: org.apache.lucene.store.Directory
I guess this error shows up, cause of an incorrect classpath. Stange that no compilation error occurs, when ant compiles the code. This is my ant file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build">
<path id="classpath">
<pathelement location="bin"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/analysis/common/lucene-analyzers-common-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/core/lucene-core-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/grouping/lucene-grouping-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queryparser/lucene-queryparser-6.2.0.jar"/>
<pathelement location="GUI_P/lucene-6.2.0/lucene-6.2.0/queries/lucene-queries-6.2.0.jar"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src">
<exclude name="**/*.launch"/>
<exclude name="**/*.java"/>
</fileset>
</copy>
</target>
<target depends="init" name="build">
<javac debug="true" destdir="bin" includeantruntime="true">
<src path="src"/>
<classpath refid="classpath"/>
</javac>
</target>
<target name="jar" depends="build">
<mkdir dir="GUI_P"/>
<jar destfile="GUI_P/GUI_P.jar" basedir="bin">
<manifest>
<attribute name="Main-Class" value="gui.Gui"/>
</manifest>
</jar>
</target>
<target name="copySamples" depends="jar">
<copy todir="GUI_P/samples">
<fileset dir="src/gui/samples"/>
</copy>
</target>
</project>
Can you please help me here out?
It's not that strange, because ClassNotFoundException arises always when trying a dynamic instantiation (through Class.forName()). So it is possible that the same classpath produces a right compilation, but it is not complete for an execution: It lacks the dynamic dependencies.
In your case, you must add to the execution classpath the lucene-core library (at least).
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
I have an ant build file for a java project the project tree looks like this :
DataBaseFidling.
|-->src (contains production code source)
|-->tests (contains tests code source)
|-->bin (contains .class)
|-->reports(contains junit xml reports)
|-->build.xml
Whenever I import this project with eclipse using "Java Project From Existing Ant Build File", eclipse does not reconize the tests folder as a source folder.
What to do to fix this?
Here is the ant build file :
The DatabaseFidling Project.
<property name="src.dir" location="./src/" />
<property name="tests.dir" location="./tests/" />
<property name="bin.dir" location="./bin/" />
<property name="lib.dir" location="/home/chedy/workspace/lib"/>
<target name="clean">
<delete verbose="true">
<fileset dir="${bin.dir}"/>
</delete>
</target>
<target name="compile">
<javac srcdir="${src.dir}" destdir="${bin.dir}">
</javac>
<javac srcdir="${tests.dir}" destdir="${bin.dir}">
<classpath>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
</classpath>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="yes" fork="true" >
<formatter type="xml"/>
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/junit4.jar"/>
<pathelement location="${lib.dir}/mockito-all-1.9.5.jar"/>
<pathelement location="${lib.dir}/SQLScriptRunner.jar"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
<batchtest todir="./report">
<fileset dir="${bin.dir}">
<include name="**/**Test*.*"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="run" depends="compile">
<java classname="com.esprit.is.Main" fork="true">
<classpath>
<pathelement path="${bin.dir}"/>
<pathelement location="${lib.dir}/mysql-connector-java-5.1.23-bin.jar" />
</classpath>
</java>
</target>
</project>
You can manually add the test folder as a source folder. Right click the project, Build Path -> Configure Build Path -> Java Build Path. In the Source tab, click Link Source then browse to your folder.
The compile target had to contain one javac task which compiles both the src and test folders.