1

在我的 AfterBuild 脚本中,我使用以下方法将文件上传到部署服务器:

<MSBuild.ExtensionPack.Communication.Ftp
TaskAction="UploadFiles"
Host="localhost"
FileNames="$(SomeFolder)\$(FileToUpload)"
UserName="myUserName"
UserPassword="myPassword"
RemoteDirectoryName="/" />

如何从文本文件或外部源加载这些凭据?有哪些替代方案?我不想将 ftp 凭据硬编码到我的 cproj 文件中。

我使用 GranadaCoders 方法来回答我自己的问题:

<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" File="$(FTP_Credentials_File)" XPath="/parameters/setParameter[@name='host']/@value">
  <Output PropertyName="FtpHost" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>

<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" File="$(FTP_Credentials_File)" XPath="/parameters/setParameter[@name='username']/@value">
  <Output PropertyName="FtpUserName" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>

<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" File="$(FTP_Credentials_File)" XPath="/parameters/setParameter[@name='password']/@value">
  <Output PropertyName="FtpPassword" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>

<Message Text="Attempting to uploade $(GeneratedZipFile) to $(FtpHost) as read from $(FTP_Credentials_File) ..." Importance="high" />
<MSBuild.ExtensionPack.Communication.Ftp TaskAction="UploadFiles" Condition="Exists('$(FTP_Credentials_File)')"  Host="$(FtpHost)" FileNames="$(PublicFolderToDropZip)\$(GeneratedZipFile)" UserName="$(FtpUserName)" UserPassword="$(FtpPassword)" RemoteDirectoryName="/" />
4

1 回答 1

2

将值放在外部 xml 文件中。将 xml 文件中的值读取到变量中。

参数.xml

<?xml version="1.0" encoding="utf-8"?>
<parameters>
  <setParameter name="LineNumber1" value="PeanutsAreCool" />
  <setParameter name="LineNumber2" value="" />
</parameters>

MyMsbuild_MsBuildExtensions.proj

<?xml version="1.0" encoding="utf-8"?>
<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">

 <Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>

    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">
        <CallTarget Targets="ReadXmlPeekValue" />
    </Target>   


    <Target Name="ReadXmlPeekValue">

        <!--  ReadAttribute  -->
        <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute" File="$(WorkingCheckout)\Parameters.xml" XPath="/parameters/setParameter[@name='LineNumber1']/@value">
            <Output PropertyName="MyValue1" TaskParameter="Value"/>
        </MSBuild.ExtensionPack.Xml.XmlFile>
        <Message Text="MyValue1 = $(MyValue1)"/>        

    </Target>   

</Project>

或者

MyMsbuild_WithCommunityTasks.proj

<?xml version="1.0" encoding="utf-8"?>
<Project  ToolsVersion="4.0"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">

    <!--
  <UsingTask AssemblyFile="$(ProgramFiles)\MSBuild\MSBuild.Community.Tasks.dll" TaskName="Version"/>
  -->



    <Import Project="$(MSBuildExtensionsPath32)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />



    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">
        <CallTarget Targets="ReadXmlPeekValue" />

    </Target>   


    <Target Name="ReadXmlPeekValue">
        <!-- you do not need a namespace for this example, but I left it in for future reference -->
        <XmlPeek Namespaces="&lt;Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
             XmlInputPath=".\Parameters.xml" 
             Query="/parameters/setParameter[@name='LineNumber1']/@value">
            <Output TaskParameter="Result" ItemName="Peeked" />
        </XmlPeek>

        <Message Text="@(Peeked)"/>

        <XmlPeek Namespaces="&lt;Namespace Prefix='peanutNamespace' Uri='http://schemas.microsoft.com/developer/msbuild/2003'/&gt;"
             XmlInputPath=".\Parameters.xml" 
             Query="/parameters/setParameter[@name='LineNumber1']/@value">
            <Output TaskParameter="Result" PropertyName="PeekedSingle" />
        </XmlPeek>      

        <Message Text="PeekedSingle = $(PeekedSingle)   "/>
    </Target>   






</Project>

编辑:

您可以为这些值添加一些基本的错误检查。

请参阅此处的网址:

http://tutorials.csharp-online.net/MSBuild:_By_Example%E2%80%94Dealing_with_MSBuild_Errors

简短的例子..注意条件..以及它如何检查空字符串。

    <Error Text="Unable to connect to webserver" Code="Deploy" Condition=" '$(WebURL)' == '' "/>
于 2013-12-04T15:57:36.110 回答