I am struggling with maven-ant build with eclipse.
I did work like below steps.
[GUI] new java project
add build.xml in project top folder
run ant file and SUCCEED!
trying to code, but somehow auto completion does not work.(guessing eclipse can not read maven-ant dependency.path)
So I tried.
add ~/.m2/repository in build path as a External class folder - does not work - It looks weird to me to include whole this folder. My current project, I need little libraries, but it has whole libraries that I uses in other projects.
add builders with build.xml like Want an eclipse java project to run ant build files automatically - does not work neither.
How can I add this maven-ant libraries properly? Thanks for sharing your experiences and answers XD
=========== Extra Information ====================
This is my build.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project name="HibernateEx2" default="db" basedir="."
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
<property name="source.root" value="src"/>
<property name="class.root" value="classes"/>
<property name="data.dir" value="data"/>
<artifact:dependencies pathId="dependency.classpath">
<dependency groupId="hsqldb" artifactId="hsqldb" version="1.8.0.10"/>
<dependency groupId="org.hibernate" artifactId="hibernate-core" version="4.3.10.Final">
<exclusion groupId="javax.transaction" artifactId="jta"/>
</dependency>
<!-- 3.2.4.GA - After hibernate4 need upgrade hibernate-tools -->
<dependency groupId="org.hibernate" artifactId="hibernate-tools" version="4.3.1.CR1"/>
<dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-jta_1.1_spec" version="1.1.1"/>
<!-- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory -->
<dependency groupId="commons-logging" artifactId="commons-logging" version="1.2"/>
<dependency groupId="log4j" artifactId="log4j" version="1.2.17"/>
<!-- java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder -->
<dependency groupId="org.slf4j" artifactId="slf4j-log4j12" version="1.7.12"/>
</artifact:dependencies>
<path id="project.class.path">
<pathelement location="${class.root}"/>
<path refid="dependency.classpath" />
</path>
<!-- Explaining how to use hibernate -->
<taskdef name="hibernatetool"
classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="project.class.path"/>
<target name="db" description="Run HSQLDB database management UI against the database file -- use when application is not running">
<java classname="org.hsqldb.util.DatabaseManager" fork="yes">
<classpath refid="project.class.path"/>
<arg value="-driver"/>
<arg value="org.hsqldb.jdbcDriver"/>
<arg value="-url"/>
<arg value="jdbc:hsqldb:${data.dir}/music/"/>
<arg value="-user"/>
<arg value="sa"/>
</java>
</target>
<target name="print-classpath" description="Show the dependency class path">
<property name="class.path" refid="dependency.classpath"/>
<echo>${class.path}</echo>
</target>
<!-- Generate java code -->
<target name="codegen" description="Generate Java source from the OR mapping files">
<hibernatetool destdir="${source.root}">
<configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
<hbm2java/>
</hibernatetool>
</target>
<!-- Creating Sub drectories -->
<target name="prepare" description="Set up build structures">
<mkdir dir="${class.root}"/>
<copy todir="${class.root}">
<fileset dir="${source.root}">
<include name="**/*.properties"/>
<include name="**/*.xml"/>
</fileset>
</copy>
</target>
<!-- Creating Schema for mapping files -->
<target name="schema" depends="prepare" description="Generate DB schema from the OR mappinf files">
<hibernatetool destdir="${source.root}">
<configuration configurationfile="${source.root}/hibernate.cfg.xml"/>
<hbm2ddl drop="yes"/>
</hibernatetool>
</target>
<!-- Compile Java -->
<!-- added includeantruntime="false" to javac, since terminal compile warning -->
<target name="compile" depends="prepare">
<javac srcdir="${source.root}" destdir="${class.root}"
debug="on" optimize="off" deprecation="on" includeantruntime="false">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="ctest" depends="compile">
<java classname="org.owls.ht.CreateTest" fork="true">
<classpath refid="project.class.path"/>
</java>
</target>
</project>
and This is what my project looks like.
src
-- source codes (includes hibernate.cfg.xml)
classes
-- compiled classes
data
-- logs and queries
build.xml
FYI, I am doing this with a book named [[Harness Hibernate]] written by James Elliot from O'reilly.
Thanks again b
For what you are trying to do, you need the filesetId and versionsId="dependency.versions" in your declaration of:
<artifact:dependencies filesetId="dependency.fileset" versionsId="dependency.versions"
Then add a copy task like so:
<copy todir="${lib.dir}">
<fileset refid="dependency.fileset" />
<mapper classpathref="maven-ant-tasks.classpath"
classname="org.apache.maven.artifact.ant.VersionMapper"
from="${dependency.versions}" to="flatten" />
</copy>
The to="flatten" will flaten your dependencies into a single folder, then you can include that folder on the classpath of eclipse project or wherever you need it.
Related
I'm using Jacoco code coverage in my Ant build and build is success after using instrument for my junit classes as they have powermockito in it.
I'm getting errors in the junit classes when i ran through the ant and tests failed but build success.
I'm comipiling the src and test classes using WAS server jars from another build.xml and placed the class files in the dir.build folder. This folder is given as input for the instrument task to give me the instrumented classes. When the junit task is started it is saying that errors occured in the test classes and test failed. I used the printsummary ="on" and tried to generate logs using verbose.
I want to know what are the errors occuring while running the junit. Can somebody please tell me how can i see the errors in the Test classes.
<?xml version="1.0" encoding="UTF-8"?>
<project name = "JunitIntegration" default = "report" xmlns:jacoco="antlib:org.jacoco.ant">
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="./ant/lib/jacocoant.jar"/>
</taskdef>
<property name= "project.name" value = "Services"/>
<property name= "source.dir" value = "C:\\Thejesh\\Workspaces\\firstSetup\\Services"/>
<property name= "dir.build" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\build\\Services-classes"/>
<!-- Code coverage report -->
<property name= "result.dir" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\junitResults"/>
<property name= "result.classes.instr.dir" value = "${result.dir}/classes-instr"/>
<property name= "result.report.dir" value = "${result.dir}/site/jacoco"/>
<property name= "result.exec.file" value = "${result.dir}/jacoco.exec"/>
<import file="build-utils.xml"/>
<target name ="cleanL">
<delete dir ="${result.dir}"/>
</target>
<target name ="createL">
<mkdir dir ="${result.dir}"/>
<mkdir dir ="${result.classes.instr.dir}"/>
</target>
<target name ="instrument" depends = "cleanL, createL">
<jacoco:instrument destdir="${result.classes.instr.dir}">
<fileset dir="${dir.build}"/>
</jacoco:instrument>
</target>
<target name ="test" depends = "instrument">
<record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="start"/>
<jacoco:coverage destfile="${result.exec.file}" xmlns:jacoco="antlib:org.jacoco.ant"
exclclassloader = "sun.reflect.DelegatingClassLoader">
<junit fork="true" forkmode="once" printsummary="on">
<formatter type= "xml"/>
<classpath>
<pathelement path="${dir.build}" />
<pathelement path="./ant/lib/jacocoagent.jar" />
<fileset dir ="C:\\Thejesh\\PIF\\Jars\\Common">
<include name ="*.jar*" />
</fileset>
</classpath>
<batchtest todir ="${result.dir}">
<fileset dir= "${result.classes.instr.dir}">
<include name ="**/*Test.class*"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="stop"/>
</target>
<!-- Generating code coverage reports -->
<target name ="report" depends ="test">
<jacoco:report>
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<structure name="JUnit intergration report">
<classfiles>
<fileset dir="${dir.build}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${source.dir}"/>
</sourcefiles>
</structure>
<html destdir="${result.report.dir}"/>
<xml destfile ="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
<target name ="compiler" >
<junit.compile
projectdir="${source.dir}"
destfile="${dir.build}"
/>
</target>
</project>
One way that may help you. Hopefully someone can suggest an easier way - this is snippet from build.xml I used a few years ago before upgrade to JUNIT5.
<junit printsummary="on" showoutput="off" fork="true"
haltonerror="false" haltonfailure="false"
errorproperty="junit.errors" failureproperty="junit.failures" >
<formatter type="plain"/>
<formatter type="xml"/>
You'd need to check what outout files you get from JUNIT4 in your ${result.dir} and add ant action to grab and print out part of the junit output files to the console so that the final error lines were visible
<loadfile srcfile="${junit.stdout}" property="junit.summary">
<filterchain><tailfilter lines="30"/></filterchain>
</loadfile>
<echo>${junit.summary}</echo>
and then bail out of the build if there are errors:
<fail if="junit.failures">JUNIT failed - see ${result.dir}</fail>
<fail if="junit.errors" >JUNIT error - see ${result.dir}</fail>
I am new to jacoco and trying to add jacoco coverage for a web application. i saw some links and came to know that we have to add javaagent below are the things that i did.
build.xml
<?xml version="1.0" ?>
<project xmlns:jacoco="antlib:org.jacoco.ant" name="Example Ant Build with JaCoCo" default="rebuild">
<description>
Example Ant build file that demonstrates how a JaCoCo coverage report can be itegrated into an existing build in three simple steps.
</description>
<property name="src.dir" location="./src"/>
<property name="result.dir" location="./target"/>
<property name="result.classes.dir" location="${result.dir}/classes"/>
<property name="result.report.dir" location="${result.dir}/site/jacoco"/>
<property name="result.exec.file" location="${result.dir}/jacoco.exec"/>
<!-- Step 1: Import JaCoCo Ant tasks -->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="../../../lib/jacocoant.jar"/>
</taskdef>
<target name="clean">
<delete dir="${result.dir}"/>
</target>
<target name="compile">
<mkdir dir="${result.classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${result.classes.dir}" debug="true" includeantruntime="false"/>
</target>
<target name="test" depends="compile">
<!--
Step 2: Wrap test execution with the JaCoCo coverage task
-->
<jacoco:coverage destfile="${result.exec.file}" includes="*">
<java classname="com.antspringmvc.exp.Math" fork="true" >
<classpath path="${result.classes.dir}"/>
</java>
</jacoco:coverage>
</target>
<target name="report" depends="test">
<!-- Step 3: Create coverage report -->
<jacoco:report>
<!--
This task needs the collected execution data and ...
-->
<executiondata>
<file file="${result.exec.file}"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Ant Example">
<classfiles>
<fileset dir="${result.classes.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${src.dir}"/>
</sourcefiles>
</structure>
<!-- to produce reports in different formats. -->
<html destdir="${result.report.dir}"/>
<csv destfile="${result.report.dir}/report.csv"/>
<xml destfile="${result.report.dir}/report.xml"/>
</jacoco:report>
</target>
<target name="rebuild" depends="clean,compile,test,report"/>
</project>
I have added the below code to my tomcat:
if ""%1"" == ""stop"" goto skip_agent
set "JAVA_OPTS=%JAVA_OPTS% -javaagent: C:\Users\kk\Desktop\Personal\jars\jacocoagent.jar=destfile=C:\Users\kk\Desktop\Personal\WorkSpace\JacocoExample\target\jacoco.exec,append=true,output=tcpserver,address=localhost,port=8080,includes=*"
i thought after stopping the server the report will be generated but that has not happened.
am i missing something in the configuration
From your build.xml seems that you expect JaCoCo agent to generate jacoco.exec file, however according to
if ""%1"" == ""stop"" goto skip_agent
set "JAVA_OPTS=%JAVA_OPTS% -javaagent: C:\Users\kk\Desktop\Personal\jars\jacocoagent.jar=destfile=C:\Users\kk\Desktop\Personal\WorkSpace\JacocoExample\target\jacoco.exec,append=true,output=tcpserver,address=localhost,port=8080,includes=*"
you configure agent with output=tcpserver, which means quoting documentation at http://www.jacoco.org/jacoco/trunk/doc/agent.html :
The agent listens for incoming connections on the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
I am trying to learn ant and found an example build file in the docs.
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up">
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
I'm assuming the clean step should run before the init step however neither step depends on the other. Should init depend on clean step? If not, how does ant know the proper order?
When this ant build file runs, the clean target will not be executed. It isn't in the dependency chain. You would have to explicitly trigger it form the command line, e.g.
ant -f _buildFile.xml clean
ant -f _buildFile.xml
I've done that within a bash file. This is an example file, though, so it isn't necessarily how your final build system will work.
Maybe doing a dist should do clean first (seems reasonable), but that should be part of the dist dependencies, not the init target. For instance, you might want to compile and not do a clean. So
<target name="dist" depends="clean, compile"...
Or you could add a new target, clean_dist, for instance and add the dependency there. Then you could do a quick distribution build and real distribution build by specifying the target on the command line.
On Eclipse I create war files by using ant.
The issue is that in the war file isn't included the right mypropfile.properties.
The file is properly copied, but also if I use <eclipse.refreshLocal resource="projectdir" depth="infinite"/> the old file is included. I have to refresh manually the project.
For Ant I use the "Run in the same JRE as the workspace" option.
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" basedir=".">
<description>
My Project
</description>
<property name="workspace.dir" value="${basedir}/../../"/>
<property name="src" value="${basedir}/../src"/>
<property name="build" value="${basedir}/../build"/>
<property name="build.classes" value="${basedir}/../build/classes"/>
<property name="lib.dir" value="${basedir}/WEB-INF/lib"/>
<property name="web.dir" value="${basedir}/WEB-INF"/>
<property environment="env"/>
<property name="real.dir" value="${basedir}/real"/>
<property name="real2.dir" value="${basedir}/real2"/>
<path id="classpath.server">
<fileset dir="${env.CATALINA_HOME}/lib" includes="*.jar"/>
<pathelement path="${build.classes}"/>
</path>
<path id="classpath.app">
<fileset dir="${lib.dir}" includes="*.jar"/>
</path>
<target name="refreshResource" if="eclipse.refreshLocal">
<eclipse.refreshLocal resource="projectdir" depth="infinite"/>
</target>
<target name="clean">
<delete dir="${build}/classes"/>
<delete dir="${build}"/>
</target>
<target name="init" depends="clean, refreshResource">
<tstamp/>
<mkdir dir="${build}"/>
<mkdir dir="${build}/classes"/>
</target>
<target name="compile" depends="init">
<javac encoding="UTF8" srcdir="${src}" destdir="${build}/classes" includeantruntime="false">
<compilerarg value="-Xlint:unchecked"/>
<classpath>
<path refid="classpath.server.bin"/>
</classpath>
<classpath>
<path refid="classpath.server"/>
</classpath>
<classpath>
<path refid="classpath.app"/>
<fileset dir="${lib.dir}" includes="*.jar"/>
</classpath>
</javac>
</target>
<target name="deleteConfig">
<delete file="${src}/mypropfile.properties"/>
</target>
<target name="real" depends="deleteConfig">
<copy file="${real.dir}/realprop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="real2" depends="deleteConfig">
<copy file="${real2.dir}/real2prop.properties" tofile="${src}/mypropfile.properties"/>
</target>
<target name="war-real" depends="real, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
<target name="war-real2" depends="real2, compile">
<input message="Warname (without .war):" addproperty="warname"/>
<war destfile="${workspace.dir}/${warname}.war" webxml="${web.dir}/web.xml">
<fileset dir="${basedir}">
<include name="**/*.*"/>
</fileset>
<classes dir="${build.classes}"/>
</war>
</target>
EDIT
The target clean was wrong, so I've corrected it, but now build failed with error
BUILD FAILED ... Reference classpath.server.bin not found.
Ant doesn't care if Eclipse has refreshed the file or not. eclipse.refreshLocal is only relevant for editors and compilers inside of the IDE.
When you run the Ant build.xml, Ant copies the file in question in the real target into the source folder and compile copies it into ${build}/classes (at least it should do that). So before you create the WAR, you must make sure the compile step has done its work (i.e. look into each file to make sure that a change is visible in each copy).
What worries my is that you use different ways to access the classes:
${build}/classes
${build.classes}
${basedir}/../build/classes
So the first step should be to define a single way to locate the folder and then use this pattern everywhere.
If that doesn't solve your problem, you need to make sure Ant notices that the file has changed. Old filesystems like FAT support only timestamps which have second resolution. If you use an USB stick for your sources, it's possible to change the file and run Ant so fast that Ant thinks the file hasn't changed.
Lastly, you need to check your classpath. If one of the JAR dependencies also contains a file called mypropfile.properties, then Java resource loading can find either version.
This and other problems made me use a different solution to configure WAR files: I pass a system property with the absolute path of the config file. That way, the WAR file doesn't change when the config changes and I have full control over which config file is loaded.
After doing so many trial and errors on "How ANT tag works ?", I decided to write my own custom build xml file for the testcases that was written in Java and integrated with JUnit. Unfortunately my build script failing with "ClassNotFoundException". And i can see the log in generated HTML file that you get to see after running ant build script.
Please see below
<project name="WebServices integrated with JUnit and generating report with ANT" default="test" basedir="." >
<description> REST Services integration with JUnit </description>
<!-- set global properties for this build -->
<property name="project_name" value="junit"/>
<property name="src" location="src"/>
<property name="build" location="bin"/>
<property name="dist" location="dist"/>
<property name="lib" location="${user.home}/My Documents/Mr/jars"/>
<property name="reports" location="reports"/>
<!-- the names of various distributable files. NOTE: Generating distribution file "target" is not used here-->
<!-- Delete the ${build} and ${dist} directory trees -->
<target name="clean" description="clean up">
<delete dir="${build}"/>
<delete dir="${dist}"/>
<delete dir="${reports}"/>
</target>
<!-- Top level targets -->
<target name="compile" depends="init" description="compile the source code">
<javac srcdir="${src}" destdir="${build}">
<classpath>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
</javac>
</target>
<!-- run your tests -->
<target name="run-tests" depends="compile" description="run your test suite">
<junit printsummary="yes" haltonfailure="no" showoutput="yes">
<classpath>
<pathelement path="${build}"/>
<fileset dir="${lib}">
<include name="**/*.jar"/>
</fileset>
</classpath>
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}/Test/Services" >
<exclude name="MyFile.java"/>
<include name="**/*.java"/> // <------ IMP***: Here I am saying include .java files that are based at "${src}/Test/Services".
</fileset>
</batchtest>
</junit>
</target>
<!-- generate report on tests -->
<target name="test" depends="run-tests">
<junitreport todir="${reports}">
<fileset dir="${reports}/raw/">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${reports}/html/"/>
</junitreport>
</target>
<target name="init" depends="clean" description="initialize the build envrionment">
<!--create the time stamp -->
<tstamp/>
<!-- Create directory structure -->
<mkdir dir="${build}"/> //<----dir for class files
<mkdir dir="${lib}"/> //<----dir for all my libraries
<mkdir dir="${dist}/lib"/> //<----not used
<mkdir dir="${reports}"/>
<mkdir dir="${reports}/raw/"/>
<mkdir dir="${reports}/html/"/> //<---- it will have output reports
</target>
<target name="all" depends="clean,test">
</target>
And I guessed ANT build will pick all the source files (.java) and then it will look for all the class files that are based in build folder and started running them, but then I see "classNotFoundException" in HTML report. please see below log :
CLASS : "getlieninfo"
> java.lang.ClassNotFoundException: getlieninfo
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
After a while I changed .java to .class in "include" tag of my target called "run-tests" . The reason for doing this is, I thought ANT is not able to look ".java" files in source folder (src/Test/Services) hence I changed to ".class" and then modified dir attribute value in "fileset" tag to "build" so it may easily look for ".class" in BUILD folder where I will have compiled files stored. But none of my trial and error succeeded and ended up with same "classNotFoundException".
I am not sure what went wrong , can someone help me, please?
OK guys, Thanks for what ever little help I got from you. I found the problem.
I had to change include tag attribute name to "name=/Test/Services/**/*.java"/>" and modify dir attribute in fileset tag to "${src}" ; see below :
<batchtest fork="yes" todir="${reports}/raw/">
<formatter type="xml"/>
<fileset dir="${src}" >
<exclude name="MyFile.java"/>
<include name="/Test/Services/**/*.java"/> //<----this where i had to modify.
NOTE: Under src directory I have Test/Services folder which has source ".java" files in it.
</fileset>
</batchtest>
It solved my problem, but then i don't understand why ant build was not able to identify source files when i gave dir attribute as dir="${src}/Test/Services" that is in fileset tag
and kept name attribute in include tag as name="**/*.java" . My understanding is fileset tag should build the path for for the given dir path , once the path is build include tag will include or look for mentioned source files i.e. ".java" .