Nantcontrib <record> task - nant

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.

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).

Using xmlpeek in Nant script gives odd error

As part of a CI process I am trying to create a buildlabel which consists of the content of an xml element within an xml structure. For this purpose I am using nant and xmlpeek. My problem is that I get an odd error stating:
"Nodeindex '0' is out of range"
This is only the case if the xml file I am xmlpeeking contains a namespace definition in the root node.
Removing the namespace from the xml file gives me the output I expect.
The nant target that generates the error can be boild down to:
<target name="TDSLabel">
<property name="element" value=""/>
<echo message="Getting element" />
<xmlpeek file="C:\xxx\test1.xml" xpath="//Project/PropertyGroup/ProductVersion" property="element"/>
<echo message="The found element value was: ${element}" />
</target>
and the test1.xml file looks like this:
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProductVersion>9.0.21022</ProductVersion>
</PropertyGroup>
</Project>
You already gave the right hint yourself. It's about the namespace. This should fix it:
<target name="TDSLabel">
<property name="element" value=""/>
<echo message="Getting element" />
<xmlpeek
file="C:\xxx\test1.xml"
xpath="//x:Project/x:PropertyGroup/x:ProductVersion"
property="element"
verbose="true">
<namespaces>
<namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
</namespaces>
</xmlpeek>
<echo message="The found element value was: ${element}" />
</target>
Found a similar problem and the anwser to my problem here: XmlPoke and unique nodes. The problem was that I did not include the namespace definition within the xmlpeek element and afterwards omitted the necessary reference to the namespace in my xpath statement:
<xmlpeek file="C:\xxx\test1.xml" xpath="//x:Project/x:PropertyGroup/x:ProductVersion" property="element">
<namespaces>
<namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
</namespaces>
</xmlpeek>

Setting multiple Phing properties from command line

<?xml version="1.0" ?>
<project name="first" basedir="." default="build-skeleton">
<property name="dirName" value="module" />
<property name="fileName" value="config" />
<target name="build-skeleton" description="Making folders">
<mkdir dir="./${dirName}/Block" />
<touch file="./${dirName}/etc/${fileName}.xml" />
</target>
</project>
phing -f mage_module.xml -DdirName=moduleX,fileName=config
phing -f mage_module.xml -DdirName=moduleX fileName=config
Both throw an error - no surprise there.
Is it possible to set multiple properties in Phing via command line?
Just repeating the -D parameter should work:
phing -f mage_module.xml -DdirName=moduleX -DfileName=config

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

nant logs with data and time stamp

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>