I am trying to write a ant build which calls a Java class
<target name="validate" depends="forge-jar">
<taskdef name="spec" classname="SpecBuild" classpathref="java.classpath" />
<spec a="${forge}" mail="${mail}" dd="${dd}" wdd="${wdd}"/>
</target>
<target name="forge-jar" >
<path id="project.class.path">
<pathelement location="classes/" />
<pathelement path="classes/ant/tasks/SpecBuild.class" />
</path>
<path id="java.classpath">
<path refid="project.class.path" />
</path>
<javac includeantruntime="false" srcdir="src" destdir="classes" classpathref="java.classpath">
<filename name="**/SpecBuild.java" />
</javac>
</target>
In the SpecBuild.java class if I use slf4j logger I am getting this warning :-
CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
[taskdef] CLASSPATH element /Users/classes/ant/tasks/SpecBuild.class is not a JAR.
Can anyone please help me fix this
You can specify the 'classes' directory in the class path as below. Remove the 'SpecBuild.class' file from pathElement location. You can either use 'includes' property and mention the file name in it or just include the 'classes' directory as below (exclude the JUni test cases package, if you have).
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="${build.dir}">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>`enter code here`
</dirset>
</classpath>
Good luck.
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.
I am getting a java.lang.ClassNotFoundException when I run my target TranslatorWorkflow that is supposed to execute a JUnit test. I am running a build.xml file with the targets: build TranslatorWorkflow. It compiles but fails on the JUnit test immediately.
My TranslatorWorkflow.class file is in {basedir}/bin/testScripts/. My classpath and target are:
classpath:
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/testScripts/" />
</path>
TranslatorWorkflow target in my build.xml file:
<target name="TranslatorWorkflow">
<mkdir dir="${junit.output.dir}" />
<junit fork="yes" printsummary="withOutAndErr">
<formatter type="xml" />
<test name="testScripts.TranslatorWorkflow" todir="${junit.output.dir}" />
<classpath refid="classpath" />
</junit>
</target>
I attempted to emulate this answer to a similar question by adding the pathelement line shown in my classpath section above, but received the same exception. I've looked at this question as well as it seems like the same deal. I'd imagine there is something super obvious that I'm missing but alas I don't seem to be getting it.
The classpath should reference ${basedir}/bin not ${basedir}/bin/testScripts (i.e. it should reference the root of classes directory, not the package in which the class exists):
<path id="classpath">
<fileset dir="${basedir}/lib/binaries" includes="*.jar" />
<pathelement location="${basedir}/bin/" />
</path>
Dumpcats, try this...
<path id="base.path">
<pathelement path="${sun.boot.class.path}"/>
<pathelement path="${sun.boot.library.path}"/>
<fileset dir="${basedir}">
<include name="**.jar"/>
</fileset>
</path>
And then in the target element ...
<target name="runTest">
<mkdir dir="test_reports"/>
<junit
fork="true"
forkmode="once"
maxmemory="256m">
<formatter type="plain" usefile="false"/>
<formatter type="xml"/>
<classpath refid="base.path"/>
<batchtest
haltonerror="true" haltonfailure="true"
todir="test_reports">
<fileset dir="${test.build}">
<include name="**/*Test*.class"/>
<include name="**/Test*.class"/>
<include name="**/*Test.classp"/>
<!-- Exclusions -->
<exclude name="**/util/*.class"/>
</fileset>
</batchtest>
</junit>
</target>
At least is that is how i manage classpath reference and everything works like a charm.
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.
I am trying to write ant script for my project but at javac i am facing this issue:
80: package com.sun.xml.internal.fastinfoset.util does not exist
[javac] import com.sun.xml.internal.fastinfoset.util.StringArray;
[javac] ^
JAVA_HOME is set to jdk1.6
ANT_HOME is set to apache-ant-1.8.4
build file is
<?xml version="1.0" ?>
<project name="project" default="war">
<property name="location" location="D:\Project"></property>
<property name="project-location" location="${location}\project"></property>
<path id="Web App Libraries.libraryclasspath">
<fileset dir="${project-location}/web/WEB-INF/lib"/>
</path>
<path id="EAR Libraries.libraryclasspath"/>
<path id="compile.classpath">
<pathelement location="${project-location}/web/WEB-INF/classes"/>
<path refid="Web App Libraries.libraryclasspath"/>
<path refid="EAR Libraries.libraryclasspath"/>
</path>
<path id="Server Library [JBoss v4.2] (unbound).libraryclasspath"/>
<target name="init" >
<mkdir dir="D:/JBOSSHOME/build/classes"/>
<mkdir dir="D:/JBOSSHOME/dist" />
</target>
<target name="compile" depends="init" >
<javac destdir="D:/JBOSSHOME/build/classes" debug="true" srcdir="${project-location}/src">
<classpath refid="compile.classpath"/>
</javac>
</target>
<target name="war" depends="compile">
<war destfile="D:/JBOSSHOME/project.war" webxml="${project-location}/web/WEB-INF/web.xml">
<fileset dir="${project-location}/web"/>
<lib dir="${project-location}/web/WEB-INF/lib"/>
<classes dir="D:/JBOSSHOME/build/classes"/>
</war>
</target>
any help or pointer would be much appreciated ty!
com.sun.internal packages are NOT recommended to use , because
they are not guaranteed to be available across environments
support to those classes are also not guaranteed
I have following ant script to generate the jar file
<project name="myProject" basedir="." default="jar">
<property name="src" value="Java Source"/>
<property name="output" value="bin"/>
<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="myProject.classpath"/>
</javac>
</target>
<target name="jar" depends="compile">
<jar destfile="myProject.jar">
<fileset dir="bin"/>
</jar>
</target>
<target name="clean">
<delete dir="${output}"/>
</target>
<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>
When I run ant script i get following error
Reference myProject.classpath not found.
I am not sure how to solve this error. It requires path of .classpath file ?
I also tried with
refid="classpath"
and it didnt work.
Can anyone help please!
Thanks
You need to define first something like because right now MyProject.classpath is not defined:
<classpath>
<pathelement path="${classpath}"/>
</classpath>
assuming that your classpath has what you need.
If it does not, create another entry under classpath element that has references to jars or whatever you need, or you need to custom specify path:
<path id="MyProject.classpath">
<pathelement location="lib/"/>
<pathelement path="${classpath}/"/>
<pathelement path="${additional.path}"/>
</path>
http://ant.apache.org/manual/using.html#path