【问题标题】:How to add a linked file to a csproj file with MSBuild. (3.5 Framework)如何使用 MSBuild 将链接文件添加到 csproj 文件。 (3.5 框架)
【发布时间】:2012-12-01 18:31:45
【问题描述】:

我正在尝试使用 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

可用。

有什么具体的帮助吗?

use 'MSBuild.ExtensionPack.Xml.XmlFile' 这样的 liner cmets 不会有帮助。

但我非常感谢任何指针或编码示例。

【问题讨论】:

    标签: c# .net xml build msbuild


    【解决方案1】:

    好吧,我打开了“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>
    

    【讨论】:

      【解决方案2】:

      您可以使用以下方法吗?

      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);
              }
          }
      }
      

      【讨论】:

      • 好吧,如果我编写一个自定义的 MSBuild 任务(过去我已经完成了好几次),那么“超越极限”。我希望在没有自定义任务的情况下在正常的 .msbuild 文件逻辑中执行此操作。我非常感谢您的提交。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多