English 中文(简体)
Nant, SqlCmd -v switch and spaces in nant property fails build with invalid argument
原标题:

I have a nant script that ... 1. takes the content of disc-file 2. assigns that content to a nant property 3. and then calls sqlcmd with a -v passing in that property containg the content of the disc file 4. inside the sql script the contents of the file should be used by a stored proc.

The problem is that when the content of the file contains a space the nant build stops with a "Invalid argument" issue

Anone know a way around this ?

The top part of the nant script is ...

<?xml version="1.0"?>
<!-- the main name of this project -->
<project name="Hops" default="all">
  <!-- BuildHistory -->
  <property name="buildHistoryContents" value="" />
  <xmlpeek xpath="/" file="BuildNotes.xml" property="buildHistoryContents"></xmlpeek>
  <!-- <echo message="${buildHistoryContents}" /> -->

  <!-- ***************** -->
  <target name="ExecSql">
    <echo message="running sql script : ${SqlBuildScriptsDir}${sqlBuildFileName}" />
    <exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=${buildHistoryContents}  " />
  </target>

The sql script contains the line ...

exec lsp_SchemaVersionUpsert  1.4 , N $(vSchemaVersion) 

A disc file content that works is ...

<BuildNotes>
  <Note>
    <buildVer>HasNotSpace</buildVer>
  </Note>
</BuildNotes>

A disc file content that does not works is ...

<BuildNotes>
  <Note>
    <buildVer>Has Space</buildVer>
  </Note>
</BuildNotes>

The use of all this is pass xml build comments to a table logging version build history for the db schema.

Does anyone know an alternate method or know a way through this ?

The next part, added after Phillip Keeley correcty solved first part (the SPACE Problem) I simplified the original task to simplify the question.

There is also a Quoted Attribute Problem ; xml quoted attributes cause the nant build to fail with "Invalid Argument".

eg this will cause nant to choke but removing the dt attribute will enable the nant build to succeed ...

<BuildNotes>
  <Note>
    <buildVer>1.4</buildVer>
    <dateStarted>09/24/2009 11:25:42</dateStarted>
    <Item dt="20091008" >SpacesAndNoQuotedAttribute</Item>
  </Note>
</BuildNotes>

Any ideas ... ?

最佳回答

Your problem is (of course) in line

<exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=${buildHistoryContents}  " />

specifically, in

-v vSchemaVersion=${buildHistoryContents}

The NAnt expression replaces property ${buildHistoryContents} with the stored value--which will include any embedded spaces. Problem is, when calling SQLCMD (I"m assuming that s what ${SqlCmd} resolves to) from a command window, the values for any and all -v parameters are space delimited -- that is, the parser hits -v, reads the next characters through the "=" as the variable name, then reads all characters after the = and through the next space (or end of line) as the value to assign to the variable, and that embedded space will mess you up bigtime.

On the command line, the work-around is to wrap the variable value in quotes:

 - v MyVariable=Hello World

becomes

 - v MyVariable="Hello World"

That doesn t work here, because it s XML and you have to wrap the commandline attribute of the exec element with quotes... and embedded quotes will, once again, mess you up bigtime.

I believe the work-around here is to use XML macro substitution (I quite possibly have the formal titles of these concepts wrong) for those embedded quotes. This value should be

&quot;

Which means that the following should work:

<exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=&quot;${buildHistoryContents}&quot;  " />

Please try this and see -- I may have to do something like this myself some day soon.

问题回答

暂无回答




相关问题
Create directory using Nant

Not sure if I m just totally missing it but I couldn t not figure out how to create a new directory using Nant. Is there built in functionality to do this? Can I just use the command prompt? I tried ...

Setting write permissions on godaddy hosting

I have godaddy asp.net hosting. Every time I upload the build, it’s a manual laborious task of setting write permissions (to folders) through the web interface. Is this any way to automate this ...

What are solid NMaven or build servers for .NET alternatives?

Maven had a long history and is well supported in the Java world. NMaven has received a less successful start and has never become as popular in the C#/.NET world as its larger cousin was in the Java ...

Gallio and MbUnit in NAnt

I am trying to use Gallio (v3.1)/MbUnit/NCover to run a unit test in my C# code, as part of the build process for my continuous integration system. I can get Gallio.Echo.exe to execute the tests and ...

TeamCity NUnit test result visualisation

Is there any way to produce visual results of NUnit tests from within TeamCity s "Tests" tab, currently my NAnt script outputs an .xml file of the results using the following task: <nunit2 ...

building .net applications without Visual Studio

I m interested to hear about people working with building .net applications using MSBuild, NAnt or similar tools. What are you using, why are you using it instead of the VS IDE? I like to use ...

热门标签