【问题标题】:Can I reference files in a Visual Studio project file from a separate targets file in MSBuild?我可以从 MSBuild 中的单独目标文件中引用 Visual Studio 项目文件中的文件吗?
【发布时间】:2014-02-05 02:45:40
【问题描述】:

我正在使用 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>

这将返回解决方案目录中所有匹配的*.scss 文件,但我真的很想只返回项目文件中已经列出的将&lt;Compile&gt; 子节点设置为True 的文件。

这可能吗?

【问题讨论】:

    标签: visual-studio msbuild


    【解决方案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 的情况,我没有看到任何错误,我相信我看到了正确的输出。

    【讨论】:

    • 我得到:“在这种情况下,不允许在位置 1 引用内置元数据“扩展”“'%(Extension') == '.scss' AND '%(编译)' == 'true'"。
    • 更新为使用 @(Content) 而不是 @(Compile) 并包含我看到的完整脚本和输出。如果您的环境不同,请告诉我:)
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多