我编写了一个自定义任务,通过创建具有不同扩展名的并行文件来构建文件。
当MSBuild转到并创建这样一个文件时,我想将它添加到项目文件本身。我还想将构建的文件嵌套在源下(使用DependentUpon
)。
MSBuild可以这样做吗?它需要重新加载项目吗?我能自动完成所有这些吗?
以下是添加我的软件包时由NuGet安装的.targets文件:
<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>
答案 0 :(得分:1)
您可以在构建项目之前在项目中应用XslTransformation msbuild任务,添加所需的文件。
这是示例转换文件:
<?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目标示例文件:
<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>
您至少需要使用.NET 4.0构建。为此,请在msbuild文件中指定ToolsVersion。