Nant、SqlCmd -v 开关和 nant 属性中的空格因参数无效而构建失败

发布于 2024-08-10 18:49:29 字数 1958 浏览 5 评论 0原文

我有一个 nant 脚本... 1. 获取光盘文件的内容 2. 将该内容分配给 nant 属性 3. 然后使用 -v 调用 sqlcmd,传入包含光盘文件内容的属性 4. 在 sql 脚本内,文件的内容应由存储过程使用。

问题是,当文件的内容包含空格时,nant 构建会因“无效参数”问题而停止,

有人知道解决这个问题的方法吗?

nant 脚本的顶部是 ...

<?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>

sql 脚本包含行 ...

exec lsp_SchemaVersionUpsert '1.4', N'$(vSchemaVersion)'

有效的光盘文件内容是 ...

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

无效的光盘文件内容是 ...

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

所有这些的使用是通过 xml为数据库模式的表记录版本构建历史构建注释。

有谁知道另一种方法或知道解决这个问题的方法吗?

下一部分是在 Phillip Keeley 正确解决第一部分(空间问题)后添加的 我简化了原来的任务以简化问题。

还有一个引用属性问题; xml 引用的属性会导致 nant 构建失败并显示“无效参数”。

例如,这将导致 nant 窒息,但删除 dt 属性将使 nant 构建成功......

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

有什么想法......?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2024-08-17 18:49:29

您的问题(当然)

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

特别符合,在

-v vSchemaVersion=${buildHistoryContents}

NAnt 表达式将属性 ${buildHistoryContents} 替换为存储的值 - 这将包括任何嵌入的空格。问题是,当从命令窗口调用 SQLCMD(我假设这就是 ${SqlCmd} 解析的内容)时,任何和所有 -v 参数的值都是空格分隔的 - 也就是说,解析器点击 -v,读取 的所有字符并通过下一个空格(或行尾)作为分配给变量的值,并且该嵌入的空格会让您大吃一惊。

通过“=”的下一个字符作为变量名称,然后读取 =后面 在命令行中,解决方法是将变量值括在引号中:

 - v MyVariable=Hello World

变得

 - v MyVariable="Hello World"

这在这里不起作用,因为它是 XML,并且您必须将 exec< 的 commandline 属性括起来/em> 带有引号的元素...并且嵌入的引号将再次让您陷入困境,

我相信这里的解决方法是使用 XML 宏替换(我很可能把这些概念的正式标题弄错了)。这个值应该是

"

这意味着以下应该可以工作:

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

请尝试这个并看看 - 我可能很快就会自己做这样的事情。

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

"

Which means that the following should work:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文