nant logs with data and time stamp - nant

How to run NAnt scripts in command line and get the timings of each task on the log file?
using nant <record> task or
NAnt -buildfile:testscript.build testnanttarget
This produces console output but I can't see any timing information.
All I want each log message prefixed with datatime.

You can use the tstamp task to display the current date/time. Just include it everywhere where you want timing information. It will not prefix each line with a timestamp, but at least you can time some strategic points.
<tstamp />

Here is a sample of tstamp
<echo>
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
TASK : INITIALIZE
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
</echo>
<loadtasks assembly="nantcontrib-0.85/bin/NAnt.Contrib.Tasks.dll" />
<!-- http://www.basilv.com/psd/blog/2007/how-to-add-logging-to-ant-builds -->
<tstamp>
<formatter property="timestamp" pattern="yyMMdd_HHmm"/>
</tstamp>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<echo message="build.log.filename: ${build.log.filename}" />
<record name="${build.log.dir}/${build.log.filename}" action="Start" level="Verbose"/>
<echo message="Build logged to ${build.log.filename}"/>
<echo message="Build Start at: ${datetime::now()}" />
</target>

Related

phing dbdeploy with postgres

Hiho,
I want to use Phing an dbdeploy with a postgres DB.
the problem is that I can not connect to the database with the connection string because I can not define a password.
Does any one has an solution for this?
The following is the deply xml:
`
<!-- load the dbdeploy task -->
<taskdef name="dbdeploy" classname="phing.tasks.ext.dbdeploy.DbDeployTask" />
<!-- these two filenames will contain the generated SQL to do the deploy and roll it back -->
<property name="build.dbdeploy.deployfile" value="scripts/deploy-${DSTAMP}${TSTAMP}.sql" />
<property name="build.dbdeploy.undofile" value="scripts/undo-${DSTAMP}${TSTAMP}.sql" />
<!-- generate the deployment scripts -->
<dbdeploy
url="pgsql:host=${db.host};dbname=${db.name}"
userid="${db.username}"
password=""
dir="${phing.dir}/sql"
outputfile="${build.dbdeploy.deployfile}"
undooutputfile="${build.dbdeploy.undofile}" />
<!-- execute the SQL - Use psql command line to avoid trouble with large
files or many statements and PDO -->
<trycatch>
<try>
<exec
command="pqsql -h${db.host} -U${db.username} -p${db.port} -d${db.name} < ${build.dbdeploy.deployfile} -W"
dir="${phing.dir}"
checkreturn="true"
output="${phing.dir}/cache/deploy-${DSTAMP}${TSTAMP}.log"
error="${phing.dir}/cache/deploy-error-${DSTAMP}${TSTAMP}.log"
/>
<echo msg="Datenbank erfolgreich ausgerollt"/>
</try>
<catch>
<echo msg="Fehler beim ausrollen der Datenbank, führe Rollback durch"/>
<exec
command="pgsql -h${db.host} -U${db.username} -p${db.port} -d${db.name} < ${build.dbdeploy.undofile}"
dir="${phing.dir}"
checkreturn="true"
/>
<echo msg="Rollback erfolgreich"/>
</catch>
</trycatch>
</target>
<!-- ============================================ -->
<!-- Target: writeFiles -->
<!-- ============================================ -->
<target name="writeFiles" description="Write all properties in files">
<copy
file="${dir}config/autoload/local.php.dist"
tofile="${dir}config/autoload/local.php"
overwrite="true"
>
<filterchain>
<replacetokens begintoken="#" endtoken="#">
<token key="db.host" value="${db.host}"/>
<token key="db.username" value="${db.username}"/>
<token key="db.password" value="${db.password}"/>
<token key="db.name" value="${db.name}"/>
<token key="db.port" value="${db.port}"/>
</replacetokens>
</filterchain>
</copy>
<echo>local.php geschrieben.</echo>
</target>`
You can create a password file for the system user (the user which will execute the phing script).

Nantcontrib <record> task

I am trying to attach a log file to email.
My code is:
**<loadtasks assembly=".../tasks.dll" />
<record name="c:foo\log.txt" action="Start" />
<target name="email">
<mail
from="abc#foo.com"
tolist="xyz#foo.com"
subject="Build"
message="Build Failed"
mailhost="smtp.anywhere.net">
<files>
<include name="c:foo\log.txt" />
</files>
<attachment>
<include name="c:foo\log.txt" />
</attachment>
</mail>
</target>
<record name="c:foo\log.txt" action="Close" />**
My command is:
nant -f:filename.build email -l:c:foo\log.txt
I get following error:
System.Security.SecurityException: Request for the permission of type 'System.Se
curity.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMa
rk& stackMark, Boolean isPermSet)
at System.Security.CodeAccessPermission.Demand()
at System.IO.FileSystemInfo.get_FullName()
at NAnt.Contrib.Tasks.RecordTask.ExecuteTask()
at NAnt.Core.Task.Execute()
at NAnt.Core.Project.InitializeProjectDocument(XmlDocument doc)
at NAnt.Core.Project.Execute()
at NAnt.Core.Project.Run()
When i dont use <record> tasks it gives another error like, could not attach txt file. bcoz it being used by another process.
Why am I getting this error?
i do something like flush in record task , just before sending email then copy the file to attach in email , hope this workaround works
<echo>${emailsubject}</echo>
<echo>Sending Email</echo>
<echo>Attaching File : ${build.log.dir}/email_${build.log.filename}</echo>
<echo>Attaching File : ${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}</echo>
<record name="${build.log.dir}/${build.log.filename}" action="Flush" level="Verbose"/>
<sleep milliseconds="5000" />
<copy file= "${build.log.dir}/${build.log.filename}" tofile="${build.log.dir}/email_${build.log.filename}" />
<copy file= "${path.vsshelper.log}/logs/${build.log.getlistoffiles}" tofile="${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}" />
Make sure that the file is closed before you try to email it.

Why is NAnt executing my sql scripts in the wrong order?

Here is my Script:
<?xml version="1.0"?>
<project name="createAndPopulateDB" default="deploy">
<property name="sql.connstring" value="Provider=SQLOLEDB;Server=G-PC\sqlexpress;Integrated Security=SSPI" />
<property name="createDB" value="BuildTestDatabase.sql" />
<property name="populateDB" value="CreateTables.sql"/>
<target name="deploy">
<echo message="* Connecting to ${sql.connstring}"/>
<foreach item="File" property="sql.script">
<in>
<items>
<include name="${createDB}" />
<include name="${populateDB}" />
</items>
</in>
<do>
<echo message="* Executing ${path::get-file-name(sql.script)}"/>
<sql connstring="${sql.connstring}" delimiter="go" delimstyle="Line" batch="false" source="${sql.script}"/>
</do>
</foreach>
</target>
</project>
The NAnt script is supposed to call two tsql programs. The first tsql is designed to drop a database if it is present, and if it isn't, create it. The second checks to see if a table is present, and if so, delete it. Similarly if it isn't, it populates the created database with a simple table.
My question is why does it run the populateDB script first?
I found that the best way to determine the order in which the tsql programs are run is through a depends attribute attached to separate targets. This will run them in a predetermined order and is extremely easy to follow logically if the NAnt script is a part of a repository.

How make display several date/time with Ant , like your topic "output timestamp in ant"

In one of your topic, named "output timestamp in ant", i've tested the 2 way explained in order to display several time in the same Ant script.
But, this 2 solutions don't work for me, Ant give me all the time the same date/time.
Anybody can explaine me why ?
I've found a solution (but note very perfet to my mind ) :
<tstamp>
<format property="date.etape1" pattern="dd/MM/yyyy HH:mm:ss" />
</tstamp>
<echo message="${date.etape1} bla bla bla...." />
<tstamp>
<format property="date.etape2" pattern="dd/MM/yyyy HH:mm:ss" />
</tstamp>
<echo message="${date.etape2} bla bla bla...." />
If somebody can explain me how make work one of the 2 solutions of the topic "output timestamp in ant", i would be very happy !!!
According to TSTAMP sources it should work (sources here) The Date object is always instantiated. Maybe there's some weird task recycle going on behind the surface ?
I'm trying with this ant build file
<project name="testTStamp" default="xxx" basedir=".">
<target name="testMe">
<MyTimestamp> </MyTimestamp>
<sleep seconds="5"> </sleep>
<MyTimestamp> </MyTimestamp>
</target>
<macrodef name="MyTimestamp">
<sequential >
<tstamp>
<format property="current.time" pattern="MM/dd/yyyy hh:mm:ss aa"/>
</tstamp>
<echo message="RUN_TIME: ${current.time}"/>
</sequential>
</macrodef>
<target name="default">
<tstamp>
<format property="current.time_one" pattern="MM/dd/yyyy hh:mm:ss.SSS aa"/>
</tstamp>
<echo message="RUN_TIME: ${current.time_one}"/>
<sleep seconds="5"> </sleep>
<tstamp>
<format property="current.time_two" pattern="MM/dd/yyyy hh:mm:ss.SSS aa"/>
</tstamp>
<echo message="RUN_TIME: ${current.time_two}"/>
</target>
<target name='xxx' depends='default, testMe' />
</project>
Okay, if in the default target, I do change the name of the format property, it works. So there's some caching magik behind.
Properties are immutable in Ant; they are not variables.
However, you can probably achieve what you want by using the <local> task. This task causes a property set afterwards in the target (or sequential, or any other block) to be only specified locally.
So if all of your timestamp echos are in separate targets, then it will work. Within a single target, you will still need to use multiple properties like "time_start", "time_end", etc.
In follow up to this thread, I've read that a macrodef is a more appropriate method, as it keeps instantiating every time it is called, so no caching problems. I've simplified their example. I included my entire file, so you can just save out the code below as clock.xml and run it directly from ant.
<project name="marktime" basedir=".">
<description>
logstamp is a simple macrodef for ANT to echo a time stamp out to an open log.
It is not written to pass back the current time, just simple echo to stdout.
You are welcome to add which file to output the results to, add blurbs to messages.
</description>
<macrodef name="logstamp">
<sequential>
<local name="logtime"/>
<tstamp>
<format property="logtime" pattern="yyyy.MM.dd : HH:mm:ss z"/>
</tstamp>
<echo message=" ### Current time now: ${logtime}"/>
</sequential>
</macrodef>
<target
name="init"
description="boilerplate for all targets">
<!-- Just run the Macrodef -->
<logstamp/>
<sleep seconds="5"> </sleep>
<logstamp/>
<sleep seconds="5"> </sleep>
<logstamp/>
<sleep seconds="5"> </sleep>
<logstamp/>
<sleep seconds="5"> </sleep>
<logstamp/>
</target>
</project>
The results are as follows:
C:\desktop\>ant -f clock.xml init
Buildfile: C:\desktop\clock.xml
init:
[echo] ### Current time now: 2014.02.14 : 16:41:06 PST
[echo] ### Current time now: 2014.02.14 : 16:41:11 PST
[echo] ### Current time now: 2014.02.14 : 16:41:16 PST
[echo] ### Current time now: 2014.02.14 : 16:41:21 PST
[echo] ### Current time now: 2014.02.14 : 16:41:26 PST
BUILD SUCCESSFUL
Total time: 20 seconds
Based on the answer by Bee Kay, this macro works with ANT version 1.7 by avoiding the use of 'local', using 'attribute' on the 'macrodef' instead:
<project name="echotime" basedir="." default="echotime">
<description>
echotime is a simple macrodef for ANT to echo a time stamp to an open log.
It is not written to pass back the current time, just simple echo to stdout.
</description>
<macrodef name="echotime">
<attribute name="logtime" default="" />
<sequential>
<tstamp>
<format property="logtime" pattern="yyyy.MM.dd HH:mm:ss z" />
</tstamp>
<echo message="Build finished at ${logtime}" />
</sequential>
</macrodef>
<target name="echotime">
<echo message="${ant.version}" />
<echotime />
</target>
The results are as follows:
>ant -buildfile echotime.xml
Buildfile: echotime.xml
echotime:
[echo] Apache Ant version 1.7.1 compiled on June 27 2008
[echo] Build finished at 2019.12.19 11:43:29 EST
BUILD SUCCESSFUL
Total time: 0 seconds

Nant: Find file by pattern

What I am trying to do, is to find a file with NAnt. This file could by anywhere in a directory structure of a given folder.
I tried to this with the NAnt-foreach task (this works) but I am not quite convinced of that:
<target name="find-file">
<fail message="Property param.dir must be set" unless="${property::exists('param.dir')}" />
<fail message="Property param.pattern must be set" unless="${property::exists('param.pattern')}" />
<property name="return.file" value="" />
<foreach item="File" property="iterator.file">
<in>
<items>
<include name="${param.dir}\**\${param.pattern}" />
</items>
</in>
<do>
<property name="return.file" value="${iterator.file}" if="${string::get-length(return.file) == 0}" />
</do>
</foreach>
</target>
Is there anybody aware of a better approach? If not how can I accomplish to exit the foreach-loop after the first element is found?
This nantcontrib function will put the matching filenames into a delimited string..
If you know that only one matching file will exist then it may get you what you want. If there are several then you could use the nant substring function to just get the first match by taking the substring up to the first delimiter.
The following nant script:
<?xml version="1.0" encoding="utf-8"?>
<project default="find-file2">
<property name="NantContrib.dir" value="C:\Program Files\nantcontrib-0.85\" readonly="true" />
<target name="LoadNantContrib">
<loadtasks assembly="${NantContrib.dir}bin\NAnt.Contrib.Tasks.dll" />
</target>
<target name="find-file2" depends="LoadNantContrib">
<fileset id="find.set">
<include name="${param.dir}\**\${param.pattern}" />
</fileset>
<property name="return.file" value="${fileset::to-string('find.set', ' | ')}" />
<echo message="return.file=${return.file}"/>
<echo message="Found ${fileset::get-file-count('find.set')} files"/>
</target>
</project>
...and the following folder structure:
\---folderroot
+---folder1
| dontfindme.txt
| findme.txt
|
+---folder2
| dontfindme.txt
|
\---folderempty
...works as expected. Searching for findme.txt finds one file. Searching for dontfindme.txt finds two files. Searching for *.txt finds three files.
Example call:
nant -D:param.dir=folderroot -D:param.pattern=findme.txt
Example output:
find-file2:
[echo] return.file=C:\Documents and Settings\rbaker\My Documents\nantfindfile\folderroot\folder1\findme.txt
[echo] Found 1 files
BUILD SUCCEEDED