簡體   English   中英

如何使用MSBuild將鏈接文件添加到csproj文件。 (3.5框架)

[英]How to add a linked file to a csproj file with MSBuild. (3.5 Framework)

我正在嘗試使用MSBuild將鏈接文件添加到我的.csproj文件。

這是.Net Framework 3.5(而不是4.0)。 我之所以這樣說是因為我看到了一些4.0特定的東西試圖操縱XML。

這是我開始的內容:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core">
      <RequiredTargetFramework>3.5</RequiredTargetFramework>
    </Reference>
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>

  <ItemGroup>
    <Compile Include="MySuperCoolClass.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>

  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

</Project>

這就是我想要得到的:

   <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core">
          <RequiredTargetFramework>3.5</RequiredTargetFramework>
        </Reference>
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />
      </ItemGroup>

      <ItemGroup>
        <Compile Include="MySuperCoolClass.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
      </ItemGroup>


      <ItemGroup>
        <Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
          <Link>MyLinkFile.ext</Link>
        </Content>
      </ItemGroup>    

      <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
    </Project>  

我有:

  • MSBuild.Community.Tasks.dll

  • MSBuild.ExtensionPack.dll

可用。

有什么具體的幫助嗎?

使用“ MSBuild.ExtensionPack.Xml.XmlFile”之類的划線員注釋將無濟於事。

但我非常感謝任何指針或編碼示例。

好吧,我打開了“ MSBuild.ExtensionPack.Xml.XmlFile(.cs)”的代碼,環顧四周。 謝天謝地開源。

我想通了..你得“建立起來”。 而且我不得不添加一個伏都教技巧(如下所示的“ MyUniqueKeyHelper123”)。

我會在這里張貼。 如果您在使用“ MSBuild.ExtensionPack.Xml.XmlFile(.cs)”時遇到麻煩,請獲取源代碼並進行查看。 您可以通過查看方法來弄清楚如何設置屬性。 剛開始時有些棘手,但可以解決。

<PropertyGroup>
    <MSBuildExtensionPackPath Condition="'$(MSBuildExtensionPackPath)' == ''">.\ExtensionPackFiles</MSBuildExtensionPackPath>
    <MSBuildExtensionPackLib>$(MSBuildExtensionPackPath)\MSBuild.ExtensionPack.dll</MSBuildExtensionPackLib>
</PropertyGroup>    


<UsingTask AssemblyFile="$(MSBuildExtensionPackLib)" TaskName="MSBuild.ExtensionPack.Xml.XmlFile" />


<Target Name="XmlTest01Target">

    <Message Text="MSBuildExtensionPackLib = $(MSBuildExtensionPackLib)" />

    <!--

    The goal is:

    <ItemGroup>
    <Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
    <Link>MyLinkFile.ext</Link>
    </Content>
    </ItemGroup>    

    -->

    <!-- Define a custom namespace.  I used "peanut" just to show it is any name you give it  -->

    <ItemGroup>
        <Namespaces Include="Mynamespace">
            <Prefix>peanut</Prefix>
            <Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
        </Namespaces>       
    </ItemGroup>

    <!-- 
        Add the <ItemGroup> (new) Element.  HOWEVER, since there will probably be multiple <ItemGroup> nodes, tag it with some unique identifier.  Will Clean up later.
    -->
    <XmlFile 
        TaskAction="AddElement" 
                    Namespaces="@(Namespaces)" 
                 File=".\MyCSharpProjectFile.csproj"
                 Element="ItemGroup"
                 Key="MyUniqueKeyHelper123"
                 Value ="MyUniqueValueHelper123"
                 XPath="//peanut:Project" 
        />

    <!-- 
        Add the <Content> (new) Element.  With Attribute Value.
    -->         
    <XmlFile 
        TaskAction="AddElement" 
                 File=".\MyCSharpProjectFile.csproj"
                 Element="Content"
                 Key="Include"
                 Value ="..\..\SomeFunFolder\MyLinkFile.ext"
                                Namespaces="@(Namespaces)" 
                 XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']" 
        />

    <!-- 
        Add the <Content> (new) Element.  With Element Value Value.
    -->                 
    <XmlFile 
        TaskAction="AddElement" 
                 File=".\MyCSharpProjectFile.csproj"
                 Element="Link"
                 InnerText ="MyLinkFile.ext"
                    Namespaces="@(Namespaces)" 
                 XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']" 
        />

    <!-- 
        Clean up the "unique" attribute to leave clean xml.
    -->             
    <XmlFile 
        TaskAction="RemoveAttribute" 
                 File=".\MyCSharpProjectFile.csproj"
                 Element="Link"
                 Key="MyUniqueKeyHelper123"
                    Namespaces="@(Namespaces)" 
                 XPath="//peanut:Project/peanut:ItemGroup[@MyUniqueKeyHelper123='MyUniqueValueHelper123']" 
        />

</Target>

您使用以下方法可行嗎?

using System;
using System.Text;
using Microsoft.Build.BuildEngine;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {
            var fullPathName = @"PathToProjectFile\Project.csproj";

            Project project = new Project();
            project.Load(fullPathName);

            var itemGroup = project.AddNewItemGroup();

            var buildItem = itemGroup.AddNewItem("Content", @"..\..\SomeFunFolder\MyLinkFile.ext");
            buildItem.SetMetadata("Link", "MyLinkFile.ext");
            project.Save(fullPathName, Encoding.UTF8);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM