首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MSBuild可以修改自己的项目文件吗?

MSBuild可以修改自己的项目文件吗?
EN

Stack Overflow用户
提问于 2014-09-08 19:18:15
回答 1查看 362关注 0票数 0

我已经编写了一个自定义任务,它通过创建一个具有不同扩展名的并行文件来构建文件。

当MSBuild去创建这样一个文件时,我想将它添加到项目文件本身中。我还想将构建的文件嵌套在源代码下(使用DependentUpon)。

MSBuild能做到这一点吗?它需要重新加载项目吗?我能自动完成所有这些吗?

以下是添加包时由NuGet安装的NuGet文件:

代码语言:javascript
复制
<UsingTask TaskName="PreCompiler" AssemblyFile="..\tools\Compiler.dll">
</UsingTask>

<UsingTask TaskName="GenerateDependencies" AssemblyFile="..\tools\Compiler.dll">
</UsingTask>

<PropertyGroup>
  <BuildDependsOn>
    $(BuildDependsOn);
    BuildCoffeeFiles;
    BuildLessFiles
  </BuildDependsOn>
  <ContentDir>Content\</ContentDir>
</PropertyGroup>

<ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'">
  <AvailableItemName Include="CoffeeScript" />
  <AvailableItemName Include="Less" />
</ItemGroup>

<Target Name="GenerateCoffeeDependencies">
  <GenerateDependencies Include="@(CoffeeScript->'%(FullPath)')">
    <Output TaskParameter="Dependencies" ItemName="InputCoffeeFiles"/>
  </GenerateDependencies>
  <ItemGroup>
    <InputCoffeeFiles
        Include="@(InputCoffeeFiles->Distinct())"
        KeepDuplicates='false' />
  </ItemGroup>
</Target>

<Target Name="BuildCoffeeFiles"
        DependsOnTargets="GenerateCoffeeDependencies"
        Inputs="@(InputCoffeeFiles)"
        Outputs="@(CoffeeScript->'%(RelativeDir)%(Filename).js')">
  <PreCompiler Include="@(CoffeeScript->'%(FullPath)')" />
</Target>

<Target Name="GenerateLessDependencies">
  <GenerateDependencies Include="@(Less->'%(FullPath)')">
    <Output TaskParameter="Dependencies" ItemName="InputLessFiles"/>
  </GenerateDependencies>
  <ItemGroup>
    <InputLessFiles
        Include="@(InputLessFiles->Distinct())"
        KeepDuplicates='false' />
  </ItemGroup>
</Target>

<Target Name="BuildLessFiles"
        DependsOnTargets="GenerateLessDependencies"
        Inputs="@(InputLessFiles)"
        Outputs="@(Less->'%(RelativeDir)%(Filename).css')">
  <PreCompiler Include="@(Less->'%(FullPath)')" />
</Target>
EN

回答 1

Stack Overflow用户

发布于 2014-09-10 13:52:03

您可以在项目中应用XslTransformation msbuild任务,然后再编译它,添加所需的文件。

这是示例转换文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <!-- Identity. -->
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
    </xsl:template>

    <xsl:template match="//ms:Compile[@Include='Program.cs']">
      <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
      <xsl:element name="DependentUpon"
         namespace="http://schemas.microsoft.com/developer/msbuild/2003">
           <xsl:text>Output.cs</xsl:text>
      </xsl:element>
    </xsl:template>

</xsl:stylesheet>

下面是msbuild目标示例文件:

代码语言:javascript
复制
<Project DefaultTargets="Build"
 xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

    <Target Name="Build">
        <XslTransformation
           XmlInputPaths="ConsoleApplication1.csproj"
           OutputPaths="ConsoleApplication1.transformed.csproj"
           XslInputPath="proj.xslt">
        </XslTransformation>

        <MSBuild Projects="ConsoleApplication1.transformed.csproj" Targets="Build" />
    </Target>
</Project>

您需要至少用 4.0构建.NET。为此,请在msbuild文件.中指定。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25731543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档