I have a web app in Eclipse that is working fine. I have built a war file using the ant script below, it works until the program attempts to access Hibernate:
<?xml version="1.0" encoding="UTF-8"?>
<!-- !DOCTYPE removes the warning:
No grammar constraints (DTD or XML schema) detected for the document.
-->
<!DOCTYPE project>
<project name="Audiclave" default="Deploy" basedir=".">
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="build.classes" location="build/classes" />
<property name="docs.dir" location="docs" />
<property name="web.dir" location="WebContent" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.classes}" />
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.classes}" />
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<path id="compile.classpath">
<fileset dir="${web.dir}/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" debug="true" destdir="${build.classes}">
<classpath refid="compile.classpath" />
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<target name="Deploy">
<war destfile="${build.dir}/audiClave.war" webxml="${web.dir}/WEB-INF/web.xml">
<!-- Include the compiled classes from the compile target -->
<classes dir="${build.classes}" />
<!-- Include any configuration files (hibernate) -->
<fileset dir="${src.dir}" includes="**/*.xml" />
<!-- Include any web content -->
<fileset dir="${web.dir}" />
<lib dir="${web.dir}/WEB-INF/lib" />
</war>
</target>
</project>
The problem I am having is the positioning of the Hibernate config files. In my application they are in :
audiClave\src
but in the deployed app the xml files are in:
audiClave
whereas I think they need to be in :
audiClave\WEB-INF\classes
How do I make the statement:
<fileset dir="${src.dir}" includes="**/*.xml" />
send the files to the WEB-INF\classes directory (if that's where they need to be).
EDIT
Changed the script to include the following and it worked correctly:
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" debug="true" destdir="${build.classes}">
<classpath refid="compile.classpath" />
</javac>
<copy todir="${build.classes}">
<fileset dir="${src.dir}" includes="**/*.xml,**/*.properties"/>
</copy>
</target>
Ask Ant to <copy> the files into /WEB-INF/classes. You are correct - that's where they belong. Be sure to create the directory path.
Related
I'm receiving an error message from Ant when I try to run the Javadoc ant task.
"BUILD FAILED
/data/data/com.termux/files/home/LearnJava/Observer/build.xml:39: No source files, no packages and no modules have been specified."
The build files reside at:
https://github.com/Fernal73/LearnJava/blob/master/Observer/build.properties
version=1.0.0
main.class=com.javacodegeeks.patterns.observerpattern.TestObserver
main.class1=com.javacodegeeks.patterns.observerpattern.Test
cs.properties=../checkstyle.properties
gformat.properties=../gformat.properties
ant.build.javac.source=1.7
ant.build.javac.target=1.7
packages=com.javacodegeeks.patterns.observerpatern.*
and
https://github.com/Fernal73/LearnJava/blob/master/Observer/build.xml
<?xml version="1.0"?>
<project name="Observer" default="main"
basedir=".">
<property file = "build.properties"/>
<property file = "${cs.properties}"/>
<property file = "${gformat.properties}"/>
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="." />
<property name="build.dir" location="." />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<taskdef resource="${cs.taskdef.resource}"
classpath="../${cs.jar}"/>
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete>
<fileset dir="." includes="**/*.class"/>
</delete>
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir,gformat,checkstyle">
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
<compilerarg value="-Xlint:-options"/>
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="${packages}" additionalparam="-Xdoclint:none"
sourcepath="${src.dir}"
destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="*.java" />
</fileset>
</javadoc>
</target>
<target name="manifest">
<tstamp/>
<manifest file="manifest.mf">
<attribute name="Built-By" value="${user.name}"/>
<section name="common">
<attribute name="Specification-Title" value="${ant.project.name}"/>
<attribute name="Specification-Version" value="${version}"/>
<attribute name="Specification-Vendor" value=""/>
<attribute name="Implementation-Title" value=""/>
<attribute name="Implementation-Version" value="${build} ${TODAY}"/>
<attribute name="Implementation-Vendor" value=""/>
</section>
<attribute name="Main-Class" value="${main.class}" />
</manifest>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile,manifest">
<jar destfile="${dist.dir}\${ant.project.name}.jar" basedir="${build.dir}" includes="**/*.class"
manifest="manifest.mf">
</jar>
</target>
<target name="run" >
<description>Run target</description>
<java classname="${main.class}">
<classpath>
<pathelement location="${dist.dir}\${ant.project.name}.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
<target name="gformat">
<exec executable="find" dir="${basedir}"
failonerror="true" outputproperty="sources">
<arg line=" . -type f -name '*.java'"/>
</exec>
<echo message="About to format ...: ${sources}"/>
<java classname="${gformat.main.class}">
<arg line=" -i ${sources}"/>
<classpath>
<pathelement location="../${gformat.jar}"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
<target name="checkstyle">
<first id="checkstylefile">
<fileset dir=".." includes="${cs.config}"/>
</first>
<checkstyle config="${toString:checkstylefile}"
classpath="../${cs.jar}"
failOnViolation="false" properties="${cs.properties}">
<fileset dir="${src.dir}" includes="**/*.java"/>
<formatter type="plain"/>
<formatter type="plain" toFile="${cs.output}"/>
</checkstyle>
</target>
<target name="main" depends="compile, jar, docs">
<description>Main target</description>
</target>
</project>
respectively.
My other projects on the repository have similar configurations.
They work as expected.
Am I missing something obvious?
I did miss something obvious. An extra 't' in the property packages. Evidently, I'm going to need two sets of eyes for this or a fresh set, a few hours later!
NOW, how do I close this?
I think the problem is in following part:
<fileset dir="${src.dir}">
<include name="*.java" />
</fileset>
In Ant "*.java" means all files with names matching *.java. This does not search in subdirectories.
To include all subdirectories you must specify:
<fileset dir="${src.dir}">
<include name="**/*.java" />
</fileset>
But since you already specified the sourcePath attribute I'm wondering if you can't just remove the fileset element as ant will add **/*.java by default.
I'm trying to run Cobertura with Ant, however always get this error in command line,
Note I'm on OSX, I couldn't figure out my directories correctly in the build.xml. Why does the command line search in /Users/jia/Java/Cobertura/cobertura-2.0.3 when I have already set different paths on the build.xml
BUILD FAILED
/Users/weimqu/Documents/workspace/Coverage1/build.xml:23: /Users/jia/Java/Cobertura/cobertura-2.0.3 does not exist.
Here's my build.xml
<project name="MyProject" default="coverage" basedir=".">
<description>
Ant build file
</description>
<!-- The properties are set in build.properties -->
<property file="build.properties" />
<property name="cobertura" location="../cobertura-src/cobertura-2.1.1" />
<path id="junit.classpath">
<fileset dir="${junit}">
<include name="*.jar" />
</fileset>
</path>
<path id="cobertura.classpath">
<fileset dir="${cobertura}">
<include name="cobertura.jar" />
<include name="lib/**/*.jar" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory -->
<mkdir dir="${bin}"/>
<mkdir dir="${instrumented}" />
<mkdir dir="${reports.xml}" />
<mkdir dir="${reports.html}" />
<mkdir dir="${coverage.xml}" />
<mkdir dir="${coverage.summaryxml}" />
<mkdir dir="${coverage.html}" />
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${bin} -->
<javac includeantruntime="false"
srcdir="${src}"
destdir="${bin}"
debug="on">
<classpath refid="junit.classpath"/>
</javac>
</target>
<target name="test1" depends="compile">
<!-- Run junit tests -->
<junit printsummary="yes" fork="yes" haltonfailure="off">
<classpath location="${bin}"/>
<classpath refid="junit.classpath"/>
<formatter type="plain"/>
<test name="edu.depaul.se433.BinarySearchTest"/>
</junit>
</target>
<target name="instrument" depends="init,compile">
<!--
Remove the coverage data file and any old instrumentation.
-->
<delete file="cobertura.ser"/>
<delete dir="${instrumented}" />
<!--
Instrument the application classes, writing the
instrumented classes into ${instrumented}.
-->
<cobertura-instrument todir="${instrumented}">
<!--
The following line causes instrument to ignore any
source line containing a reference to log4j, for the
purposes of coverage reporting.
-->
<ignore regex="org.apache.log4j.*" />
<fileset dir="${bin}">
<!--
Instrument all the application classes, but
don't instrument the test classes.
-->
<include name="**/*.class" />
<exclude name="**/*Test.class" />
</fileset>
</cobertura-instrument>
</target>
<property name="testcase" value="edu.depaul.se433.BinarySearchTest" />
<target name="test2" depends="init,compile">
<junit fork="yes">
<!--
Note the classpath order: instrumented classes are before the
original (uninstrumented) classes. This is important.
-->
<classpath location="${instrumented}" />
<classpath location="${bin}" />
<classpath refid="junit.classpath" />
<!--
The instrumented classes reference classes used by the
Cobertura runtime, so Cobertura and its dependencies
must be on your classpath.
-->
<classpath refid="cobertura.classpath" />
<formatter type="xml" />
<test name="${testcase}" todir="${reports.xml}" if="testcase" />
<batchtest todir="${reports.xml}" unless="testcase">
<fileset dir="${src}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
<junitreport todir="${reports.xml}">
<fileset dir="${reports.xml}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.html}" />
</junitreport>
</target>
<target name="coverage-report-xml">
<!--
Generate an XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src}" destdir="${coverage.xml}" format="xml" />
</target>
<target name="summary-coverage-report">
<!--
Generate an summary XML file containing the coverage data using
the "srcdir" attribute.
-->
<cobertura-report srcdir="${src}" destdir="${coverage.summaryxml}" format="summaryXml" />
</target>
<target name="coverage-report-html">
<!--
Generate a series of HTML files containing the coverage
data in a user-readable form using nested source filesets.
-->
<cobertura-report destdir="${coverage.html}">
<fileset dir="${src}">
<include name="**/*.java"/>
<exclude name="**/*Test.java"/>
</fileset>
</cobertura-report>
</target>
<target name="coverage"
depends="compile,instrument,test2,coverage-report-xml,summary-coverage-report,coverage-report-html"
description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
<target name="clean">
<!-- Delete the ${bin} folder -->
<delete dir="${bin}"/>
<delete dir="${instrumented}" />
<delete dir="${reports}" />
<delete file="cobertura.log" />
<delete file="cobertura.ser" />
</target>
</project>
And this is my files directory:
myProject:
build.properties
build.xml
cobertura-src/cobertura-2.1.1/conbertura files
junit/
src/edu/depaul/se433/
I've just created an Authentication Provider for WebLogic Server Version: 12.1.3.0.0, put the jar in the lib of the domain, but does not appear in the List of Providers to select
<?xml version="1.0"?>
<!DOCTYPE MBeanType SYSTEM "commo.dtd">
<!-- MBean Definition File (MDF) for the MMR WS Authenticator See documentation at: http://docs.oracle.com/cd/E13212_01/wles/docs42/dvspisec/mdf_ref.html Since it is for an identity asserter, it must extend the weblogic.management.security.authentication.IdentityAsserter mbean. The Name and DisplayName must be the same. They specify the name that will appear on the console for this provider. Set the PeristPolicy to "OnUpdate" so that if an attribute value is changed, the new value is written to disk immediately. See the "Developing Security Services" manual for more info. Note that since this is an xml document, you can't use double quotes directly. Instead you need to use " Note that setting "Writeable" to "false" on an attribute makes the attribute read-only. The default is read-write. -->
-<MBeanType PersistPolicy="OnUpdate" Extends="weblogic.management.security.authentication.Authenticator" Package="fr.telecom.security.authentication.provider.mbean" Name="MMRWSAuthenticator">
<!-- You must set the value of the ProviderClassName attribute (inherited from the weblogic.management.security.Provider mbean) to the name of the java class you wrote that implements the weblogic.security.spi.AuthenticationProvider interface. You can think of the provider's mbean as the factory for your provider's runtime implementation. -->
<MBeanAttribute Name="ProviderClassName" Default=""fr.telecom.security.authentication.provider.MMRWSAuthenticationProviderImpl"" Preprocessor="weblogic.management.configuration.LegalHelper.checkClassName(value)" Writeable="false" Type="java.lang.String"/>
<!-- You must set the value of the Description attribute (inherited from the weblogic.management.security.Provider mbean) to a brief description of your provider. It is displayed in the console. -->
<MBeanAttribute Name="Description" Default=""MMR WS Authenticator"" Writeable="false" Type="java.lang.String"/>
<!-- You must set the value of the Version attribute (inherited from the weblogic.management.security.Provider mbean) to your provider's version. There is no required format. -->
<MBeanAttribute Name="Version" Default=""1.0"" Writeable="false" Type="java.lang.String"/>
</MBeanType>
This is my build.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!--
This project is not entirely handled by Maven, so be careful when generating the artifact, and use Ant with its default target.
-->
<project name="devices-ws-authentication-provider" default="all" basedir=".">
<echo>
Remember to execute the command below before doing an Ant:
%MW_HOME%\wlserver\server\bin\setWLSEnv.cmd
</echo>
<property name="src-java" value="src/main/java" />
<property name="mbean" value="src/mbean" />
<target name="all" depends="build" />
<target name="build" depends="clean,build.mdf,build.mjf" />
<target name="clean">
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${mbean}" includes="**/*"/>
</delete>
<delete file="target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" failonerror="false" />
<mkdir dir="target" />
<echo message="Clean finish" />
</target>
<!-- helper to build an MDF (mbean definition file) -->
<target name="build.mdf">
<java dir="${basedir}" fork="false" classname="weblogic.management.commo.WebLogicMBeanMaker">
<arg line="-files ${mbean}" />
<arg value="-createStubs" />
<arg line="-MDF src/main/resources/TntWSAuthenticator.xml" />
</java>
<echo message="Created Supporting Classes" />
<move todir="${src-java}/fr/telecom/security/authentication/provider/mbean" file="${mbean}/TntWSAuthenticatorImpl.java" />
<move todir="${src-java}/fr/telecom/security/authentication/provider/mbean" file="${mbean}//fr/telecom/security/authentication/provider/mbean/TntWSAuthenticatorMBean.java" />
</target>
<target name="build.mjf">
<mkdir dir="${mbean}" />
<copy todir="${mbean}">
<fileset dir="src/main/java">
<include name="**/*" />
</fileset>
<fileset dir="src/main/resources">
<include name="**/*" />
</fileset>
</copy>
<java dir="${basedir}" fork="false" classname="weblogic.management.commo.WebLogicMBeanMaker">
<arg line="-MJF target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" />
<arg line="-files ${mbean}" />
</java>
<echo message="Mbean JAR created." />
<!-- Do a small cleanup after building the JAR, in order to not have duplicated sources / classes in source paths -->
<!--
<delete includeemptydirs="true" failonerror="false">
<fileset dir="${mbean}" includes="fr/telecom/security/authentication/provider/*.java" />
<fileset dir="${mbean}" includes="fr/telecom/domain/**/*.java" />
<fileset dir="${mbean}" includes="commo.dtd" />
<fileset dir="${mbean}" includes="rebel.xml" />
<fileset dir="${mbean}" includes="TntWSAuthenticator.xml" />
</delete>
-->
</target>
<target name="install" depends="build">
<copy todir="${user.home}/devices/lib" file="target/devices-ws-authentication-provider-1.0.0-SNAPSHOT.jar" />
</target>
</project>
opening the generated ear I see that there is some missing folders and files, like
/schemacom_bea_xml/element
/schemacom_bea_xml/namespace
/schemacom_bea_xml/system
/schemacom_bea_xml/type
/weblogic/
WLS providers is a nightmare, I have already developed two and now I am using Spring Security :) for the shake of simplicity and portability.
Well, I read your build.xml and it result strange for me, but the most obvious thing I see is that you lack the following argument:
<arg line="-targetNameSpace your.namespace.here" />
inside WebLogicMBeanMake ant rule. Try it, but if it does not work I will recommend you to redo. I can provide you some samples.
Edit
In response to your comment, I will include here a sample that I have already working in a real project.
This is a build.xml file to be run with and. To run this sample it is required to have WLS 12c installed. Before run ant buildAndDeploy you have to run ${WLS_HOME}/server/bin/setWLSEnv.sh script to prepare environment.
You have to adapt global properties at the beginning of the file.
<project name="ldap-db-provider" default="buildAndDeploy" basedir=".">
<description>Custom Authentication Provider Build</description>
<!-- global properties -->
<property name="base.dir" location="." />
<property name="bea.home" location="/opt/weblogic/Oracle/Middleware/Oracle_Home" />
<property name="mbean.maker" location="${bea.home}/wlserver/modules/com.bea.core.mbean.maker_2.1.0.0.jar" />
<property name="wls.home" value="${bea.home}/wlserver" />
<property name="wls.lib.dir" value="${wls.home}/server/lib" />
<property name="mbeantypes.dir" value="${wls.lib.dir}/mbeantypes" />
<property name="jar.file.name" value="your-name-for-the-provider-package.jar" />
<property name="src.dir" value="${base.dir}/src" />
<property name="provider.src.dir" value="${src.dir}/es" />
<property name="build.dir" value="${base.dir}/build" />
<property name="targetNamespace" value="your.target.name.space"/>
<property name="mbean.impl.name" value="YouBeanName" />
<property name="mbean.package" value="your/mbean/pacakage/name" />
<target name="updateSources">
<copy todir="${src.dir}/${mbean.package}" flatten="true">
<fileset dir="${build.dir}">
<include name="${mbean.impl.name}Impl.java" />
</fileset>
</copy>
<copy todir="${src.dir}/${mbean.package}" flatten="true">
<fileset dir="${build.dir}/${mbean.package}">
<include name="**/*.java" />
</fileset>
</copy>
</target>
<target name="buildAndDeploy" depends="clean">
<mkdir dir="${build.dir}" />
<copy todir="${build.dir}" flatten="true">
<fileset dir="${wls.lib.dir}">
<include name="commo.dtd" />
</fileset>
</copy>
<copy todir="${build.dir}" flatten="true">
<fileset dir="${provider.src.dir}">
<include name="**/*.xml" />
<include name="**/*.java" />
</fileset>
</copy>
<java
classname="weblogic.management.commo.WebLogicMBeanMaker"
fork="true" failonerror="true">
<!-- <classpath refid="build.path"/> -->
<arg line="-files ${build.dir}" />
<arg value="-createStubs" />
<arg line="-MDF ${build.dir}/${mbean.impl.name}-Mbean.xml" />
</java>
<echo message="Created Supporting Classes" />
<java
classname="weblogic.management.commo.WebLogicMBeanMaker"
fork="true" failonerror="true">
<!-- <classpath refid="build.path"/> -->
<arg line="-MJF ${build.dir}/${jar.file.name}" />
<arg line="-targetNameSpace ${targetNamespace}" />
<arg line="-files ${build.dir}" />
</java>
<echo message="Created Mbean Jar" />
<!-- Deploy the sample security providers -->
<copy todir="${mbeantypes.dir}" flatten="true">
<fileset dir="${build.dir}">
<include name="${jar.file.name}" />
</fileset>
</copy>
</target>
<target name="clean">
<delete quiet="true" dir="${build.dir}" />
<delete>
<fileset dir="${src.dir}/${mbean.package}" includes="**/*.java" />
</delete>
<delete quiet="true" file="${mbeantypes.dir}/${jar.file.name}" />
</target>
</project>
Hope it helps!
Pretty new to ANT and building using it. I have a Java-Jersey rest project and i have included the Jersey libraries under WEB-INF/lib.
I have a build.xml for building/compiling the project.
<?xml version="1.0"?>
<project name="Ant-Test" default="Main" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="lib.dir" location="" />
<property name="build.dir" location="bin" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<!-- Deletes the existing build directory-->
<target name="clean">
<delete dir="${build.dir}" />
</target>
<!-- Creates the build directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
<!-- Compiles the java code -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" />
<jar destfile="${build.dir}/CrunchifyRESTJerseyExample.jar" basedir="${build.dir}"/>
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
</war>
</target>
<target name="Main" depends="compile">
<description>Main target</description>
</target>
</project>
With this, i am not getting the library files in the war file. What should i add to get it in the war?.
If you take a look on the Ant war task you can specify a <lib> element with the jars that are going to be put under WEB-INF/lib folder. So try this:
<war destfile="${build.dir}/CrunchifyRESTJerseyExample.war" webxml="WebContent/WEB-INF/web.xml">
<classes dir="${build.dir}"/>
<lib dir="${lib.dir}">
<exclude name="jdbc1.jar"/> <!-- Exclude here jars you don't want -->
</lib>
</war>
Note: You should set your property at the begining of your script for the above task to work properly:
<property name="lib.dir" location="lib" /> <!-- Or whatever you call your project folder with the jars-->
try including fileset element as child of war element
<fileset dir="${home.dir}/WEB-INF/libDirectory/*">
<include name="**/*"/>
</fileset>
I'm learning these days how to use ant to run automated test folowing this tutorial.
I have JUnit in the classpath of my project. All seem to work fine and I can include it in my classes:
import junit.framework.TestCase; //line20
public class SimpleLattice1DTest extends TestCase{
...
}
My build.xml is:
<?xml version="1.0"?>
<project name="Ant-Test" default="compile" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="." />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />
<property name="test.dir" location="jlife/tests" />
<property name="test.report.dir" location="test/report" />
<!-- Deletes the existing build, docs and dist directory-->
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${docs.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Creates the build, docs and dist directory-->
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${docs.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
</javac>
</target>
<!-- Creates Javadoc -->
<target name="docs" depends="compile">
<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
<!-- Define which files / directory should get included, we include all -->
<fileset dir="${src.dir}">
<include name="**" />
</fileset>
</javadoc>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\CoreTest.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Test" value="test.CoreTest" />
</manifest>
</jar>
</target>
<!-- Run the JUnit Tests -->
<!-- Output is XML, could also be plain-->
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
</project>
When i run it into eclipse I get the following error:
[javac] C:\Documents and
Settings\noname\Documenti\JLife_git\JLife_git\JLife\src\jlife\tests\SimpleLattice1DTest.java:20:
package junit.framework does not exist
[javac] import junit.framework.TestCase;
I suppose there's something wrong with it, but I have no idea. Could someone put me in the right direction?
Your javac target doesn't specify anything apart from the source and target directory - it doesn't add any classpath entries; you'll need to add an entry for the appropriate JUnit jar file. See the javac task documentation for more details. You may want to specify the path to JUnit as a classpath attribute, a nested element, or a reference to a path declared elsewhere.
The eclipse classpath is separate from your ant environment. In your build file, when you call javac you need to supply a classpath attribute.
You can define the classpath at the top of the file with the rest of your properties, like this:
<path id="classpath">
<fileset dir="[path to libraries]" includes="**/*.jar" />
</path>
and then use it in each call to javac by setting the classpathref attribute, like this:
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath" />
You need to specify the directory that contains your .class files and your external jars (like junit).
e.g.
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
Here's the complete file I took that excerpt from in case you need ideas for how to setup other common things (emma, javadoc, etc)
<project name="imp" default="dist" basedir="..">
<description>Buildscript for IMP</description>
<property name="source" location="src"/>
<property name="lib" location="lib"/>
<property name="history" location="test_history"/>
<property name="web-tests" location="/var/www/tests"/>
<property name="web-files" location="/var/www/files"/>
<property name="web-javadoc" location="/var/www/javadoc"/>
<property name="web-emma" location="/var/www/emma"/>
<property name="emma.dir" value="${lib}"/>
<property name="test" location="${source}/imp/unittest"/>
<property name="test.relative" value="imp/unittest"/>
<property name="javadoc-theme" value="tools/javadoc-theme"/>
<!-- directories for generated files -->
<property name="build" location="build"/>
<property name="build-debug" location="debug"/>
<property name="build-coverage" location="coverage"/>
<property name="dist" location="dist"/>
<property name="reports" location="reports"/>
<property name="coverage-emma" location="${reports}/coverage/emma"/>
<!-- Populates a class path containing our classes and jars -->
<path id="dist.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build}"/>
</path>
<path id="debug.classpath">
<fileset dir="${lib}"/>
<pathelement path="${build-debug}"/>
</path>
<!-- import emma. This classpath limits the coverage to just our classes -->
<path id="debug.imp.classpath">
<pathelement path="${build-debug}"/>
</path>
<taskdef resource="emma_ant.properties" classpathref="debug.classpath"/>
<!--
Shouldn't ever need to use this from the command line. IRC saith that the "private"
internal use only sort of targets are prefixed with '-'.
dirty because it's the opposite of the 'clean' target.
-->
<target name="-dirty">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build-debug}"/>
<mkdir dir="${build-coverage}"/>
<mkdir dir="${dist}"/>
<mkdir dir="${reports}"/>
<mkdir dir="${coverage-emma}"/>
</target>
<!-- clean up all the generated files and direcories -->
<target name="clean" description="Deletes all files and directories created by this script.">
<delete dir="${build}"/>
<delete dir="${build-debug}"/>
<delete dir="${build-coverage}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
<delete dir="${coverage-emma}"/>
</target>
<!-- Compile the java code place into ${build} -->
<target name="compile" depends="-dirty" description="Compile the source.">
<javac srcdir="${source}" destdir="${build}" includeantruntime="false">
<classpath refid="dist.classpath"/>
<exclude name="${test.relative}/**/*"/>
</javac>
</target>
<!-- Compile the java code with debug info place into ${build} -->
<target name="compile-debug" depends="-dirty" description="Compile the source with debug information.">
<javac
srcdir="${source}"
destdir="${build-debug}"
includeantruntime="false"
debug="true"
debuglevel="lines,vars,source"
>
<classpath refid="debug.classpath"/>
</javac>
</target>
<!-- roll up everyting into a single jar file -->
<target name="dist" depends="clean, compile" description="Generate the distribution file for IMP.">
<!-- Copy the library .jars to the directory where the IMP distribution will be located -->
<copy todir="${dist}">
<fileset dir="${lib}"/>
</copy>
<!-- TODO: Generate the MANIFEST.MF file on the fly -->
<jar jarfile="${dist}/imp.jar" basedir="${build}" manifest="tools/MANIFEST.MF"/>
<!-- dump to web server -->
<copy todir="${web-files}">
<fileset dir="${dist}"/>
</copy>
</target>
<!-- build and run the tests then report the results in HTML -->
<target name="test" depends="compile-debug" description="Run all the JUnit tests and outputs the results as HTML.">
<!-- run the tests -->
<junit printsummary="true" haltonerror="false" haltonfailure="false">
<classpath refid="debug.classpath"/>
<formatter type="xml"/>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
<!-- report the results -->
<junitreport todir="${reports}">
<fileset dir="${reports}" includes="TEST-*.xml"/>
<report todir="${reports}"/>
</junitreport>
<!-- update the latest results file to be commited -->
<copy file="${reports}/TESTS-TestSuites.xml" tofile="${history}/test-results-latest.xml"/>
<!-- dump to webserver -->
<copy todir="${web-tests}">
<fileset dir="${reports}"/>
</copy>
</target>
<!-- run emma code coverage tool and publish results in HTML -->
<target name="emma" depends="compile-debug" description="Checks code coverage with Emma.">
<!-- put the magic emma juice into the classes -->
<emma>
<instr
instrpathref="debug.imp.classpath"
destdir="${coverage-emma}/instr"
metadatafile="${coverage-emma}/metadata.emma"
merge="true"
/>
</emma>
<!-- run the tests -->
<junit fork="true" printsummary="true" haltonerror="false" haltonfailure="false">
<classpath>
<pathelement location="${coverage-emma}/instr"/>
<path refid="debug.classpath"/>
</classpath>
<batchtest fork="true" todir="${reports}">
<fileset dir="${source}">
<include name="${test.relative}/**/*Test*.java"/>
<exclude name="${test.relative}/**/AllTests.java"/>
</fileset>
</batchtest>
<jvmarg value="-Demma.coverage.out.file=${coverage-emma}/coverage.emma"/>
<jvmarg value="-Demma.coverage.out.merge=true"/>
</junit>
<!-- publish the coverage report -->
<emma>
<report sourcepath="${source}" verbosity="verbose">
<fileset dir="${coverage-emma}">
<include name="*.emma"/>
</fileset>
<html outfile="${web-emma}/index.html"/>
</report>
</emma>
</target>
<!-- publish javadoc -->
<target name="javadoc" description="Creates javadoc for IMP.">
<delete dir="${web-javadoc}"/>
<javadoc
sourcepath="${source}"
defaultexcludes="no"
destdir="${web-javadoc}"
author="true"
version="true"
use="true"
windowtitle="IMP: Integrated Mechanisms Program"
overview="${source}/overview.html"
classpathref="debug.classpath"
stylesheetfile="${javadoc-theme}/stylesheet.css"
/>
<copy file="${javadoc-theme}/javadoc.jpg" tofile="${web-javadoc}/javadoc.jpg"/>
</target>
<target name="all" description="Runs test, emma, javadoc, and dist targets.">
<antcall target="test"/>
<antcall target="emma"/>
<antcall target="javadoc"/>
<antcall target="dist"/>
</target>
</project>
If you observe the error stack, you will find the following line, just above the error line you mentioned...
[javac] [search path for class files: C:\Program Files\Java\jre6\lib\resource...
This line shows all the jars available in the class path for this ant target execution.
You will definitely not find the desired jar over here i.e. junit-x.x.x.jar (junit-4.8.2.jar)
Now go to eclipse -> Window -> preferences -> Ant -> Runtime -> Global Entries -> Add Jars add junit-4.8.2jar (which you will find in your project lib directory)
If you play around the Ant -> Runtime -> classpath and the classpath related error line in the error stack, you will understand the issue.
Hope this solves your problem.