i used an ant build file to create a jar for my project.After the classes i got from build was not the same as of the classes of jar file exporting from eclipse.
because of this the jar from build is not working.
Here is my build.xml file
<javac destdir="${build.dir}" srcdir="${src.dir}" source="1.5" target="1.5" >
<compilerarg value="-Xlint:unchecked"/>
<classpath refid="master-classpath"/>
</javac>
Set target version in your ant-script. Something like:
<javac destdir="${build.dir}" srcdir="${src.dir}" source="1.6" target="1.6" >
Related
I have a java project with java files more than 2500 and some of them may have compilation issue. I need to generate classfiles and route to a particular folder. Even with some of the compilation error, rest of jave turned to class files, with eclipse.
But I need to compile with build tools like ANT but it stops as build failed.
Hence no classfiles generated. Is there a way to compile and generated when project has some compilation error using ANT. The sample code is like
<?xml version="1.0"?>
<project name="REL854" basedir="." default="compile">
<target name="create" depends="delete">
<mkdir dir="src"/>
</target>
<target name="delete">
<delete dir ="src" />
<echo>exclude not working</echo>
</target>
<target name="copy" depends="create">
<copydir src="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\src" dest="C:\Users\username\neon\ANTBuildFor854\build\src" excludes="Samples"></copydir>
<copy todir ="C:\Users\username\neon\ANTBuildFor854\build\lib" overwrite="true">
<fileset dir="C:\ClearCase_Storage\Views\Snapshot\username_view6\opensource\Selenium\REL854\lib" ></fileset>
</copy>
</target>
<target name="compile" depends="copy"></target>
<javac failonerror="false" includeantruntime="false" srcdir="C:\Users\username\neon\ANTBuildFor854\build\src" destdir="C:\Users\username\neon\ANTBuildFor854\build\bin" includes="**/*.java"></javac>
</project>
Compiling works fine, but it's not packaging into a jar file.
The final message is BUILD SUCCESSFUL with compiled classes but I cannot find a jar.
<project name="thisIsMyProject" basedir="." default="build">
.
.
.
.
.
Load Classpath and such.....
.
.
.
.
<target depends="init" name="build-project">
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="my.classpath"/>
</javac>
</target>
<mkdir dir="build"/>
<target name="jar" description="Make My Jar File" depends="init">
<jar destfile="myFile.jar" destdir="build" basedir="bin" compress="true" />
</target>
</project>
It looks like your target names are wrong.
Your default target is "build", but you don't actually have a target called "build" - perhaps you should change the default to "jar"
Your "jar" target depends on "init", but it should probably also depend on "build-project" if you want it to compile BEFORE it packages the jar.
Is there a way to set the value of the javac classpath property in Apache Ant (v1.9.6) so that I don't have to literally specify all the jars that I want to include e.g. a directory or file glob.
So, if I have something like:
<javac classpath="./lib/one.jar":./lib/two.jar:./lib/three.jar..."
...is there anyway to just specify my ./lib directory once, like the way you can do when you run a java application like:
java -cp ./lib/'*'
I've tried that, and just using ./lib, or./lib/* or ./lib/*.jar but they don't work.
And another solution using a reusable classpath reference:
<path id="compile.path">
<fileset dir="lib" includes="*.jar"/>
</path>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" debug="true" classpathref="compile.path"/>
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<path refid="compile.path"/>
<pathelement path="${classes.dir}"/>
</classpath>
..
..
Try putting a <classpath> element under <javac>:
<javac ...>
<classpath>
<fileset dir="lib" includes="*.jar"/>
</classpath>
</javac>
Our build.xml file contains the following:
<path id="our.classpath">
<fileset dir="${in.libs}"/>
<fileset file="/home/ouruser/fortify/Core/lib/sourceanalyzer.jar"/>
</path>
<target name="compile">
<pathconvert property="test" refid="our.classpath"/>
<echo message="CLASSPATH=${test}"/>
<javac debug="true"
debuglevel="source,lines,vars"
destdir="${out.classes}"
includeAntRuntime="no"
fork="false"
source="1.7" target="1.7">
<src path="${src1.dir}"/>
<src path="${src2.dir}"/>
<classpath refid="our.classpath"/>
<compilerarg value="-Xlint:-path"/>
<compilerarg line="-proc:none"/>
<compilerarg line="-s "${out.classes}""/>
</javac>
</target>
<target name="fortify">
<antcall target="compile">
<param name="build.compiler" value="com.fortify.dev.ant.SCACompiler"/>
</antcall>
</target>
When I run ant fortify, I get the following output:
fortify:
compile:
[echo] CLASSPATH=<a long list of jar files snipped>:/home/ouruser/fortify/Core/lib/sourceanalyzer.jar
BUILD FAILED
/home/ouruser/build.xml:542: The following error occurred while executing this line:
/home/ouruser/build.xml:230: Class not found: com.fortify.dev.ant.SCACompiler
As you can see from the echo output, the sourceanalyzer.jar file is in the classpath that is used by the javac task.
When I run jar -tvf /home/ouruser/fortify/Core/lib/sourceanalyzer.jar | grep SCACompiler.class, the SCACompiler class is listed:
8408 Fri Apr 04 11:17:26 EDT 2014 com/fortify/dev/ant/SCACompiler.class
So why does Ant say Class not found: com.fortify.dev.ant.SCACompiler?
In place of com/fortify/dev/ant/SCACompiler.class class, use
com.fortify.dev.ant.SourceanalyzerTask
I created my own build.xml which has:
<target name="compile">
<mkdir dir="build"/>
<javac destdir="build">
<src path="src"/>
</javac>
</target>
<target name="build" depends="compile">
<mkdir dir="dist"/>
<jar destfile="dist/app.jar" basedir="build" />
</target>
<target name="run" depends="compile">
<java classname="webserver.Loader" classpath="build" fork="true" />
</target>
It works great. When I call ant run so it compiles and runs my application, but my application has a package with icons and it isn't moved to a folder "build" so my application ends with an exception that it couldn't locate my icons. When I move them by myself then it works.
I tried to use
<copy todir="build/app/icons">
<fileset dir="src/app/icons"/>
</copy>
It works, but I would like to do it without the copy command. Is there any parameter to javac? Or something else?
Thank you for answer.
There is no such parameter. You can copy all sorts of files between your directories with:
<copy todir="build">
<fileset dir="src"
includes="**/*.xml,**/*.properties,**/*.txt,**/*.ico" />
</copy>
Sorry, you will need to copy non-java files manually. Resources are technically not "source". The command-line javac will not copy resource files from your source directory to the output directory, neither will ant's javac task.
You can do this using the fileset element of the jar task instead of manually copying the files. For example:
<jar destfile="dist/app.jar" basedir="build">
<fileset dir="src" includes="app/icons/**" />
</jar>
This will copy everything in src/app/icons/ to the app/icons path in your .jar file.
No, there isn't. The copy task is the correct way to copy resources into your build folders.