如何链接文件添加到具有的MSBuild的csproj文件。 (3.5框架)文件、有的、框架、链接

2023-09-02 21:39:27 作者:眸中一星辰

我想我们的MSBuild为链接文件添加到我的 .csproj的文件。

I'm trying to us MSBuild to add a linked file to my .csproj file.

这是.NET Framework 3.5的(而不是4.0)。我提到,因为我看到了一些4.0具体的东西试图操纵XML。

This is .Net Framework 3.5 (and not 4.0). I mention that because I'm seen some 4.0 specific stuff trying to manipulate the 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="PropertiesAssemblyInfo.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="PropertiesAssemblyInfo.cs" />
      </ItemGroup>


      <ItemGroup>
        <Content Include="....SomeFunFolderMyLinkFile.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单行注释的不会有帮助。

不过,我AP preciate任何指针或codeD例子极大。

But I appreciate any pointers or coded examples immensely.

推荐答案

嗯,我打开了$ C $下MSBuild.ExtensionPack.Xml.XmlFile(的.cs),环顾四周。 谢天谢地开源。

Well, I opened up the code for "MSBuild.ExtensionPack.Xml.XmlFile(.cs)" and looked around. Thank goodness for open source.

我想通out..you得建立起来。 而我却增添了几分巫毒把戏(与MyUniqueKeyHelper123下面)。

I figured out..you gotta "build it up". And I had to add a little voodoo trick (with the "MyUniqueKeyHelper123" seen below).

我会后在这里。 如果您在使用MSBuild.ExtensionPack.Xml.XmlFile(的.cs)的麻烦,获取源$ C ​​$ c和看待它。你可以找出如何通过观察方法来设置属性。 这是一个有点棘手在第一,但图中取能。

I'll post here. If you're having trouble with "MSBuild.ExtensionPack.Xml.XmlFile(.cs)", get the source code and look at it. 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="....SomeFunFolderMyLinkFile.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 ="....SomeFunFolderMyLinkFile.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>
 
精彩推荐