Ant creates jar files in wrong destination - ant

Below is my build.xml file for ant.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Struts2ProductsDemo" default="jar">
<property name="src.dir" location="src" />
<property name="build.dir" location="L:\build" />
<property name="project.name" value="Struts2ProductsDemo" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" />
</target>
<target name="jar" depends="compile">
<jar destfile="${build.jar}/jars/${project.name}.jar" basedir="${build.dir}/classes" />
</target>
The jar files are created in: C:\Users\San\EclipseWS\Struts2ProductsDemo\${build.jar}\jars\Struts2ProductsDemo.jar
I am expecting the jar to be here: ${build.jar}/jars/${project.name}.jar

Property build.jar is never set. You have to define it. For example, you may add this to your build:
<property name="build.jar" location="target"/>

Related

PMD ANT script gives taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found using the classloader AntClassLoader[]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="info" name="MyProject">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="lib.dir" value="lib"/>
<property name="jar.name" value="${ant.project.name}"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" debug="true" nowarn="true" debuglevel="lines,vars,source"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${jar.name}.jar" basedir="${classes.dir}">
<exclude name="**/Main.class" />
<fileset dir="${src.dir}" includes="**/*.java">
<exclude name="**/Main.java" />
</fileset>
<zipgroupfileset dir="${lib.dir}" includes="*.jar">
<exclude name="**/Utils.jar" />
</zipgroupfileset>
</jar>
</target>
<target name="build" depends="jar"/>
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" />
<target name="pmd">
<pmd shortFilenames="true" failuresPropertyName="failures.count" rulesetfiles="\path\pmd\ruleSet.xml">
<formatter type="html" toFile="pmd_report.html" toConsole="true"/>
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</pmd>
</target>
<target name="info">
<echo message="Available Targets:"/>
<echo message=" clean"/>
<echo message=" compile"/>
<echo message=" jar"/>
<echo message=" build"/>
<echo message=" pmd"/>
</target>
</project>
This script gives me this " taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found using the classloader AntClassLoader[]"
I have added PMD library jar file in lib folder of the project,where other libraries are present as jar files.
But if i change add path ref to the library not as a jarfile , it works well.
<path id="pmd.classpath">
<fileset dir="C:\Users\PMD\pmd-bin-5.5.2">
<include name="**/*.jar"/>
</fileset>
</path>
May I know what is the problem? I am pretty new to ANT and PMD , any help will be appreciated.
Thanks
First thing to check is the file pmd-core-*.jar in your lib directory?
Secondly add a reference to the path in the taskdef task:
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="classpath"/>

Ant build error : src does not exist

I was trying to run my build through Ant tool but console output always shows this error :
**E:\Automation\PowerElectronicsWorkShop\FreesunPortal\build.xml:31: srcdir "E:\Automation\PowerElectronicsWorkShop\FreesunPortal\${src.dir}" does not exist!
Build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="" basedir="." default="runTests">
<property name="ws.home" value="${basedir}"/>
<property name="ws.jars" value="E:\Automation\Jar files\Jars"/>
<property name="test.src" value="${ws.home}/src"/>
<property name="test.dest" value="${ws.home}/build"/>
<property name="ng.result" value="test-output" />
<presetdef name="javac">
<javac includeantruntime="false" />
</presetdef>
<target name="setClassPath">
<path id="classpath_jars">
<fileset dir="E:\Automation\Jar files">
<include name="*.jar" />
</fileset>
<pathelement path="${class.path}" />
</path>
<pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
</target>
<target name="clean" depends="setClassPath">
<echo message="deleting existing build directory"/>
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="clean">
<echo message="compiling.........."/>
<javac destdir="${build.dir}" debug="true" srcdir="${src.dir}" classpath="${test.classpath}"/>
</target>
<target name="runTests" depends="compile">
<taskdef resource="testngtasks" classpath="${test.classpath}"/>
<testng classpath="${test.classpath}:${build.dir}">
<xmlfileset dir="${basedir}" includes="testng.xml"/>
</testng>
</target>
</project>
I don't understand why this is occurring every time.
Add Following code under property tag
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
Now you can re build your code through ANT, it will work.

Unable to find images when running selenium/sikuli scripts using Ant

I've written a couple selenium scripts (Java) and use sikuli to verify the images exist. It worked fine when I ran the tests through Eclipse/TestNG but with Ant I'm getting the following error:
[testng] [error] resources/x.png looks like a file, but can't be found on the disk. Assume it's text.
The following is my build.xml, please let me know if there's more information I can provide.
<project name="test" default="test">
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<property name="dist.dir" location="dist" />
<property name="lib.dir" location="lib" />
<path id="build.classpath">
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir" depends="clean">
<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"/>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\build.test.ant.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<taskdef resource="testngtasks" classpath="${lib.dir}/testng-6.5.2.jar"/>
<target name="test" depends="jar">
<testng
classpath="${build.dir}:${lib.dir}/selenium-java-2.24.1.jar:${lib.dir}/selenium-server-standalone-2.24.1.jar:${lib.dir}/sikuli-script.jar"
outputDir="${testng.report.dir}"
testname="test1">
<xmlfileset dir="." includes="testng.xml" />
</testng>
<fail message="BUILD FAILURE" if="failed" />
</target>
</project>

Ant Build file with junit test for java project

I am trying to write an android build file that compiles, creates a jar executes it and also runs a bunch of test files. This is what I have so far, but not sure how to proceed with writing the test block. I have looked around but an example of a build file with junit testing but haven't found any..an example of a ant file with junit would be helpful please
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="test.dir" value="${build.dir}/test"/>
<property name="main-class" value="com.arkangelx.classes.ATMLauncher"/>
<property name="TALK" value="true" />
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" verbose="${TALK}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="test" depends="run">
<mkdir dir="${test.dir}"/>
<test destfile="${test.dir}/${ant.project.name}.test" basedir="${build.dir}">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="TestExample" />
</junit>
</test>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
The test element must be a sub-element of the junit task. There are several exemples available in (surprinsingly) the documentation of the junit task.

ant build script issue

I have 2 ant build scripts named "build" and "tarne"
Build:
<?xml version="1.0" ?>
<project name="build" default="zip">
<property name="project.name" value="projectName"/>
<property name="version" value="default_version_value"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/build/ant-contrib.jar"/>
</classpath>
</taskdef>
<var name="version2" value="default_version_value"/>
<property name="tmp" value="tmp"/>
<property name="build.dir" location="${tmp}/component/${project.name}"/>
<property name="java.classes" location="${tmp}/component/${project.name}/classes"/>
<property name="weblayout.dir" location="${tmp}/weblayout/resources/${project.name}"/>
<path id="compile.classpath">
<fileset dir="lib" includes="**/*.jar" />
<fileset dir="lib/build" includes="*.zip" />
</path>
<target name="clean">
<delete dir="${tmp}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${java.classes}" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" source="1.5" target="1.5" encoding="utf-8" includes="**/*.java" destdir="${java.classes}" classpathref="compile.classpath" />
</target>
<target name="copy-resources" depends="compile">
//Lots of copying here
</target>
<target name="read.version" description="Parses the hda file for your version number">
<property file="${project.name}.hda" prefix="hda"/>
<propertyregex property="version" input="${hda.version}" regexp="\." replace="-" global="true" override="true"/>
<var name="version2" value="${version}"/>
<echo>${version}</echo>
<echo>${version2}</echo>
</target>
<target name="zip" depends="copy-resources, read.version" description="Package component">
<zip destfile="${project.name}-${version}.zip" basedir="${tmp}" />
<delete dir="${tmp}" />
</target>
</project>
Tarne:
<?xml version="1.0" ?>
<project default="tarne">
<include file="build.xml"/>
<property name="project.name" value="build.project.name"/>
<target name="tarne">
<antcall target="build.read.version" inheritRefs="true"></antcall>
<property name="version" value="build.version"/>
<property name="version2" value="build.version2"/>
<echo>${version}</echo>
<echo>${version2}</echo>
</target>
</project>
And the output I get when I run tarne.xml is:
Buildfile: tarne.xml
tarne:
build.read.version:
[echo] v1-0-1
[echo] v1-0-1
[echo] default_version_value
[echo] default_version_value
Where the first 2 lines (v1-0-1) are from inside the read.version target of build.xml and the next 2 lines are from tarne.xml. The general idea is that I should be able to access the version number in my tarne.xml build script.
Any ideas on what's going wrong?
Antcall does not support what you intend to do:
http://ant.apache.org/manual/Tasks/antcall.html :
The called target(s) are run in a new project; be aware that this means properties, references, etc. set by called targets will not persist back to the calling project.
you could try:
<target name="tarne" depends="build.read.version">
</target>
which would keep the new values.
Try
<property name="version" value="${build.version}"/>
<property name="version2" value="${build.version2}"/>

Resources