简体   繁体   English

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

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

I'm trying to us MSBuild to add a linked file to my .csproj file. 我正在尝试使用MSBuild将链接文件添加到我的.csproj文件。

This is .Net Framework 3.5 (and not 4.0). 这是.Net Framework 3.5(而不是4.0)。 I mention that because I'm seen some 4.0 specific stuff trying to manipulate the XML. 我之所以这样说是因为我看到了一些4.0特定的东西试图操纵XML。

Here is what I'm starting with: 这是我开始的内容:

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

This is what I'm trying to get: 这就是我想要得到的:

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

I have: 我有:

  • MSBuild.Community.Tasks.dll

and

  • MSBuild.ExtensionPack.dll

available. 可用。

Any concrete help? 有什么具体的帮助吗?

One liner comments like use 'MSBuild.ExtensionPack.Xml.XmlFile' won't be helpful. 使用“ MSBuild.ExtensionPack.Xml.XmlFile”之类的划线员注释将无济于事。

But I appreciate any pointers or coded examples immensely. 但我非常感谢任何指针或编码示例。

Well, I opened up the code for "MSBuild.ExtensionPack.Xml.XmlFile(.cs)" and looked around. 好吧,我打开了“ MSBuild.ExtensionPack.Xml.XmlFile(.cs)”的代码,环顾四周。 Thank goodness for open source. 谢天谢地开源。

I figured out..you gotta "build it up". 我想通了..你得“建立起来”。 And I had to add a little voodoo trick (with the "MyUniqueKeyHelper123" seen below). 而且我不得不添加一个伏都教技巧(如下所示的“ MyUniqueKeyHelper123”)。

I'll post here. 我会在这里张贴。 If you're having trouble with "MSBuild.ExtensionPack.Xml.XmlFile(.cs)", get the source code and look at it. 如果您在使用“ MSBuild.ExtensionPack.Xml.XmlFile(.cs)”时遇到麻烦,请获取源代码并进行查看。 You can figure out how to set the properties by looking at the method. 您可以通过查看方法来弄清楚如何设置属性。 It was a little tricky at first, but figure-out-able. 刚开始时有些棘手,但可以解决。

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

Is it feasible for you to use the following? 您使用以下方法可行吗?

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