我可以从MSBuild中的单独目标文件引用Visual Studio项目文件中的文件吗?

时间:2014-02-03 18:12:32

标签: visual-studio msbuild

我正在使用Jenkins和MSBuild构建我们的解决方案,并希望使用与我们的一个Visual Studio插件捆绑在一起的命令行工具为我们生成CSS和JavaScript。由于我希望显而易见的原因,我想使用MSBuild和Visual Studio项目文件来执行此操作,而不是必须维护单独的批处理文件。

我已经创建了一个单独的.targets文件并且已经运行了,但是当我自己抓取文件时,我正在努力。我可以使用通配符从文件系统中获取文件,但我真正想做的是从项目文件本身获取文件列表。

以下是我们项目文件的简要版本 - 我删除了很多“默认”内容:

<?xml version="1.0" encoding="utf-8"?>  
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- I'll omit all the standard project bits and bobs for brevity -->
  <ItemGroup>
    <Content Include="css\scss\styles.scss">
      <Compile>True</Compile>
      <Minify>False</Compile>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
    <Content Include="css\scss\_imported-1.scss">
      <Compile>False</Compile>
      <Minify>False</Compile>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
    <Content Include="css\scss\_imported-2.scss">
      <Compile>False</Compile>
      <Minify>False</Compile>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
  </ItemGroup>
  <Import Project="$(SolutionDir)\.webcompile\WebCompile.targets" />
</Project>

WebCompile.targets看起来像这样:

<?xml version="1.0" encoding="utf-8"?>  
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <BuildDependsOn>
      $(BuildDependsOn);
      WebCompile
    </BuildDependsOn>
  </PropertyGroup>
  <ItemGroup>
    <SassFiles Include="**/*.scss" />
  </ItemGroup>
  <Target Name="WebCompile">
    <Message Text="SassFiles: @(SassFiles)" />
  </Target>
</Project>

这将返回Solution目录中的所有匹配的*.scss文件,但我真的很有兴趣只返回已在项目文件中列出的<Compile>子节点设置为True

这可能吗?

1 个答案:

答案 0 :(得分:1)

  

这将返回解决方案中所有匹配的* .scss文件   目录,但我真的很有兴趣只返回文件   已经在具有子节点的项目文件中列出   设为True。

您可以向SassFiles项目组添加过滤器,其中只显示具有相应条件的成员。

<ItemGroup>  
  <SassFiles Include= "@(Content)" Condition = "'%(Extension)' == '.scss' AND '%(Compile)' == 'true'"/>   
</ItemGroup>    

@Abit,这是我的MsBuild脚本(保存为“abit.msbuild”):

<?xml version="1.0" encoding="utf-8"?>  
<Project ToolsVersion="4.0" DefaultTargets="WebCompile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- I'll omit all the standard project bits and bobs for brevity -->
  <ItemGroup>
    <Content Include="css\scss\styles.scss">
      <Compile>True</Compile>
      <Minify>False</Minify>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
    <Content Include="css\scss\_imported-1.scss">
      <Compile>False</Compile>
      <Minify>False</Minify>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
    <Content Include="css\scss\_imported-2.scss">
      <Compile>False</Compile>
      <Minify>False</Minify>
      <CompileStyle>Nested</CompileStyle>
      <DebugInfo>False</DebugInfo>
    </Content>
  </ItemGroup>
  <Target Name="WebCompile">
    <ItemGroup>  
        <SassFiles Include="@(Content)" Condition="'%(Extension)' == '.scss' AND '%(Compile)' == 'true'" />   
    </ItemGroup>
    <Message Text="SassFiles: @(SassFiles)" />
  </Target>
</Project>

这是我正在运行的命令:

%WinDir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe abit.msbuild

这是输出:

Project "d:\msbuild\abit.msbuild" on node 1 (default targets).
WebCompile:
  SassFiles: css\scss\styles.scss
Done Building Project "d:\msbuild\abit.msbuild" (default targets).

由于@(SassFiles)ItemGroup上的条件,我没有看到任何错误,我相信我看到了正确的输出。

相关问题