How to echo the contents of a fileset in Nant? - nant

Was trying to do something like this:
<copy>
<fileset id="mySet">
<include name="*.sql" />
</fileset>
</copy>
<echo message="Copied files: ${mySet} to directory: ${Folder}." />
But i get the following error:
'id' is an invalid attribute for a
tag. Datatypes can only be
declared at Project or Target level.
Thanks

You can do this by looping over the files in the set.
<fileset id="mySet">
<include name="*.sql" />
</fileset>
<copy>
<fileset refid="mySet" />
</copy>
<foreach item="File" property="filename">
<in>
<items refid="mySet" />
</in>
<do>
<echo message="Copied files: ${filename} to directory: ${Folder}." />
</do>
</foreach>
But depending on verbosity level the result of the copy action is echoed anyway.

Related

How do I delete a folders that match pattern in phing

I have a build_ folders in my directory like build_10320 or build_10321.
I need to write a target clean that deletes such a folder.
I am trying doing this
<target name="clean">
<echo msg="clean directory ./build_" />
<delete includeemptydirs="true" verbose="true" failonerror="false" >
<fileset dir="./">
<include name="./build_*" />
</fileset>
</delete>
</target>
But this doesn't work. Kindly help.
Phing still doesn't have the <dirset> feature working (which would be the natural choice). You can however make this work using <exec> & the relevant command for deleting files from your operating system.
For linux:
<exec command = "rm -rf ./build_*" passthru = "true" />
A <fileset> returns, as the name suggests, only files.
There is an undocumented <dirset> type that unfortunately cannot be used with <delete> at the moment.
With Phing 3.x you can use <dirset> inside the <delete> task.
<project name="delete-with-dirset" default="clean" basedir=".">
<target name="clean">
<echo msg="clean directory ./build_" />
<delete includeemptydirs="true" verbose="true" failonerror="false">
<dirset dir="./">
<include name="./build_*" />
</dirset>
</delete>
</target>
</project>

How to compile GWT project to jar file and add it to another GWT project?

I have Three GWT projects. 2 common GWT(Bootstrap & Data Transfer Objects) projects, it doesnt contain any UI part. So, I want to compile these two projects and create jar and want to include it in third GWT project. I have done in Eclipse[Project->Export->Jar file]. It works fine. I want do it command line or through ant build.xml. Please if you know anyone, can you help on this.
We have a similar case and here is the target we use for building an external jar for our task engine. This will build you a jar from command line that can be included in other projects. In our case we have a large project that contains all of our domain objects and some services. We wanted to include this into our task engine.
<target name="taskengine-jar" depends="enhance">
<mkdir dir="build" />
<mkdir dir="build/META-INF"/>
<copy todir="build/META-INF">
<fileset dir="src/META-INF">
<include name="persistence.xml" />
<include name="server.properties" />
<include name="crypto.properties" />
<include name="sqlDrivers.xml" />
</fileset>
</copy>
<jar destfile="taskengine-jobs.jar">
<fileset dir="war/WEB-INF/classes">
<include name="com/hp/vf/server/**" />
<include name="com/hp/vf/shared/**" />
</fileset>
<fileset dir="build">
<include name="META-INF/**" />
</fileset>
<zipgroupfileset dir="war/WEB-INF/lib">
<include name="axis2.jar" />
<include name="javamail-1.4.4.jar" />
<include name="commons-pool-1.6.jar" />
<include name="openjpa-all-2.2.0.jar" />
<include name="commons-logging-adapters-1.1.1.jar" />
<include name="commons-logging-api-1.1.1.jar" />
<include name="commons-vfs2-2.0.jar" />
<include name="postgresql-9.0-801.jdbc4.jar" />
<include name="poi-3.8-beta5-20111217.jar"/>
<include name="poi-ooxml-schemas-3.8-beta5-20111217.jar"/>
<include name="poi-ooxml-3.8-beta5-20111217.jar"/>
<include name="itext-2.1.7.jar"/>
<include name="REngine.jar" />
<include name="Rserve.jar" />
<include name="jsoup-1.6.1.jar" />
<include name="jfreechart-1.0.13.jar" />
<include name="jcommon-1.0.16.jar" />
</zipgroupfileset>
<fileset dir="war/WEB-INF/lib">
<include name="addressing-1.1.1.mar" />
<include name="rampart-1.1.mar" />
</fileset>
</jar>
<delete dir="build" />
</target>
I think, the most easy and flexible way is to use Maven. Each project represents the separate Maven artefact which is stored in your local repository. And only one thing you should do in your main project is to add couple of dependencies to your POM file. That's it. I use this schema for a long time.

Trying create db using ant

My build.xml
<?xml version='1.0'?>
<project xmlns:ivy="antlib:org.apache.ivy.ant" name='myTest' basedir='.' default='usage'>
<property file='build.properties' />
<property name='src.dir' value='src' />
<property name='web.dir' value='war' />
<property name='build.dir' value='${web.dir}/WEB-INF/classes' />
<property name='name' value='myTest' />
<property name="sql.driver" value="org.postgresql.Driver"/>
<property name="sql.url" value="jdbc:postgresql://localhost:5432/tbook"/>
<property name="sql.user" value="postgres"/>
<property name="sql.pass" value="admin"/>
<path id='master-classpath'>
<fileset dir='${web.dir}/WEB-INF/lib'>
<include name='*.jar' />
</fileset>
<!-- We need the servlet API classes: -->
<!-- * for Tomcat 5/6 use servlet-api.jar -->
<!-- * for other app servers - check the docs -->
<fileset dir='${appserver.lib}'>
<include name='servlet*.jar' />
</fileset>
<pathelement path='${build.dir}' />
</path>
<target name='usage'>
<echo message='' />
<echo message='${name} build file' />
<echo message='-----------------------------------' />
<echo message='' />
<echo message='Available targets are:' />
<echo message='' />
<echo message='build --> Build the application' />
<echo message='deploy --> Deploy application as directory' />
<echo message='deploywar --> Deploy application as a WAR file' />
<echo message='resolve --> retrieve dependencies with ivy' />
<echo message='' />
</target>
<target name="createDB_PostgreSQl">
<sql driver="${sql.driver}"
url="${sql.url}"
userid="${sql.user}"
password="${sql.pass}"
src="db.sql">
<classpath refid="master-classpath"/>
</sql>
</target>
<target name="dropDB_PostgreSQl">
<input message="Do you really want to delete this table (y/n)?" validargs="y,n" addproperty="do.delete" />
<condition property="do.abort">
<equals arg1="n" arg2="${do.delete}"/>
</condition>
<fail if="do.abort">Build aborted by user.</fail>
<sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" >
<classpath>
<pathelement location="postgresql-9.0-802.jdbc4.jar"/>
</classpath>
drop database sample_project;
</sql>
</target>
<target name="createTables_PostgreSQL">
<sql driver="${sql.driver}" url="${sql.url}" userid="${sql.user}" password="${sql.pass}" >
<classpath refid="master-classpath"/>
<transaction src="user.sql"/>
</sql>
</target>
<target name='build' description='Compile main source tree java files'>
<mkdir dir='${build.dir}' />
<javac destdir='${build.dir}' source='1.5' target='1.5' debug='true'
deprecation='false' optimize='false' failonerror='true'>
<src path='${src.dir}' />
<classpath refid='master-classpath' />
</javac>
</target>
<target name="clean">
<delete includeemptydirs="false">
<fileset dir="build/" includes="**/**"/>
</delete>
</target>
<target name='deploy' depends='build' description='Deploy application'>
<copy todir='${deploy.path}/${name}' preservelastmodified='true'>
<fileset dir='${web.dir}'>
<include name='**/*.*' />
</fileset>
</copy>
</target>
<target name="resolve" description="retrieve dependencies with ivy">
<ivy:retrieve />
</target>
<target name='deploywar' depends='build'
description='Deploy application as a WAR file'>
<war destfile='${name}.war' webxml='${web.dir}/WEB-INF/web.xml'>
<fileset dir='${web.dir}'>
<include name='**/*.*' />
</fileset>
</war>
<copy todir='${deploy.path}' preservelastmodified='true'>
<fileset dir='.'>
<include name='*.war' />
</fileset>
</copy>
</target>
</project>
Exception is:
org.postgresql.util.PSQLException:ERROR: CREATE DATABASE can not be executed inside a transaction block
Take a look at this thread from the past. http://archives.postgresql.org/pgsql-jdbc/2005-11/msg00132.php
Basically for the create db task add this autocommit="true"

Eclipse unable to compile ant script programmatically

I have an Ant script that I would like to execute using a button that I made. The problem now is that I would get an error saying:
BUILD FAILED
X:\eclipseMT\runtime-workspace\testTest\diagram1_Sc2_TestScript.xml:68:
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the
classpath. Perhaps JAVA_HOME does not
point to the JDK. It is currently set
to "C:\Program
Files\Java\jdk1.6.0_12\jre"
I'm pretty sure that my classpath is correct and I can execute the same script using Eclipse internal Ant executor.
This is my ant script:
<property name="src.dir" value="src" />
<property name="test.dir" value="test" />
<property name="test.wstest.dir" value="${test.dir}/wstest" />
<property name="junit.dir" location="X:\eclipseMT\plugins\org.junit_3.8.2.v3_8_2_v20100427-1100"/>
<property name="build.dir" value="build" />
<property name="build.classes.dir" value="${build.dir}/classes" />
<property name="build.test.dir" value="${build.dir}/test"/>
<property name="build.test.classes.dir" value="${build.test.dir}/classes" />
<property name="build.test.data.dir" value="${build.test.dir}/data" />
<property name="build.test.reports.dir" value="${build.test.dir}/reports" />
<property name="dist.dir" value="dist" />
<property name="lib.dir" value="lib" />
<property name="build.debug" value="true" />
<path id="compile.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<path id="test.compile.classpath">
<path refid="compile.classpath" />
<pathelement location="${build.classes.dir}"/>
</path>
<path id="test.classpath">
<path refid="test.compile.classpath" />
<pathelement location="${build.test.classes.dir}"/>
</path>
<target name="init" description="create dir desc">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.classes.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${lib.dir}" />
<copy todir="${lib.dir}">
<fileset dir="${junit.dir}"/>
</copy>
<echo>make init dir done</echo>
</target>
<target name="test-init" depends="init" description="create test dir">
<mkdir dir="${build.test.dir}" />
<mkdir dir="${build.test.classes.dir}" />
<mkdir dir="${build.test.data.dir}"/>
<mkdir dir="${build.test.reports.dir}"/>
<echo>make test init dir done</echo>
</target>
<target name="clean" depends="init, test-init" description="remove previous build">
<delete verbose="true">
<fileset dir="${build.classes.dir}" />
<fileset dir="${build.test.classes.dir}" />
<fileset dir="${build.test.data.dir}" />
<fileset dir="${build.test.reports.dir}" />
<fileset dir="${dist.dir}" />
</delete>
<echo>clean build dir done</echo>
</target>
<target name="compile" depends="clean" description="compile java source">
<javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpath="${build.classes.dir}" debug="on" fork="no" includeAntRuntime="false" />
<echo>compile source done</echo>
</target>
<target name="test-compile" depends="compile, test-init" description="compile test source">
<javac srcdir="${test.wstest.dir}" destdir="${build.test.classes.dir}" debug="true" includeAntRuntime="true">
<classpath refid="test.compile.classpath" />
</javac>
<echo>compile test src done</echo>
</target>
<target name="test-reporting" depends="test-compile" description="report even if fail">
<junit printsummary="false" errorproperty="test.failed" failureproperty="test.failed">
<classpath>
<path refid="test.classpath" />
</classpath>
<formatter type="brief" usefile="false" />
<formatter type="xml" />
<batchtest todir="${build.test.data.dir}" unless="testcase">
<fileset dir="${build.test.classes.dir}" />
<!--fileset dir="${build.test.classes.dir}" includes="Sc2TestClient*.class" /-->
</batchtest>
</junit>
<junitreport todir="${build.test.data.dir}">
<fileset dir="${build.test.data.dir}">
<include name="WSTEST-*.xml" />
</fileset>
<report format="frames" todir="${build.test.reports.dir}" />
</junitreport>
<fail if="test.failed">
Test failed. Check ${build.test.reports.dir}
</fail>
</target>
<target name="default" depends="test-reporting" description="test the whole suite">
<echo>all test done</echo>
<tstamp>
<format property="buildTime" pattern="yyyy-MM-dd' 'HH:mm:ss" />
</tstamp>
<echo>build time = ${buildTime}</echo>
</target>
This is my button code:
Project p = new Project();
try {
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);
p.fireBuildStarted();
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
p.addReference("ant.projectHelper", helper);
helper.parse(p, buildFile);
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null);
} catch (Throwable t) {
t.printStackTrace();
p.fireBuildFinished(t);
return false;
}
It's complaining about javac, unable to find javac.. your JAVA_HOME(C:\Program Files\Java\jdk1.6.0_12\jre) is pointing to only "jre" not to java compiler(javac). Set you JAVA_HOME to "C:\Program Files\Java\jdk1.6.0_12\bin"(which has both javac and jre) and it should work. :-)
--Nagesh Palathya
A friend give me a reference to this earlier question: Setting JAVA_HOME when running Ant from Java
The problem basicly is with ant because it has point java.home to 'jre' instead of 'jdk'. It got overwritten everytime ant was call to execute.
The solution is to add:
fork="yes"
to each compilation script and add:
p.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.6.0_26");
to the java execution code.
Hope this help others :D

nant exclude folder

I am looping though my folders and need to exclude the svn folders. I thought I could simply add the exclude element, though that doesnt seem to work.
<foreach item="Folder" property="foldername">
<in>
<items>
<include name="YOUR_FOLDER\**" />
<exlcude name="YOUR_FOLDER\**/_svn" />
</items>
</in>
<do>
<foreach item="File" property="filename" in="${foldername}">
<do>
<echo message="${filename}" />
</do>
</foreach>
</do>
</foreach>
Can somebody help?
It works when adding simply:
<exclude name="YOUR_FOLDER\**_svn**" />
You'll want to include the .svn directories and everything in them as well, otherwise the exclude mask will only hit the .svn directories, but not .svn/prop-base, .svn/props, .svn/text-base and so on.
<foreach item="Folder" property="foldername">
<in>
<items>
<include name="YOUR_FOLDER/**" />
<exclude name="YOUR_FOLDER/**/.svn/**" />
<exclude name="YOUR_FOLDER/**/_svn/**" />
</items>
</in>
<do>
<foreach item="File" property="filename" in="${foldername}">
<do>
<echo message="${filename}" />
</do>
</foreach>
</do>
</foreach>
Also, are you really using the alternative _svn naming scheme for Subversion's data directories?
<copy failonerror="true" overwrite="true" todir="${BusinessServicesTarget}">
<fileset basedir="${BusinessServicesPath}">
<exclude name="*.*" />
<include name="/**/*.as?x" />
<exclude name="/**/**_SVN**" />
You need to use it in copy function not in foreach.