简体   繁体   中英

Can I reference files in a Visual Studio project file from a separate targets file in MSBuild?

I'm using Jenkins and MSBuild to build our solution and would like to use the command line tool that is bundled with one of our Visual Studio plugins to generate CSS and JavaScript for us. For reasons that I hope are obvious, I'd like to do this using MSBuild and the Visual Studio project file as opposed to having to maintain a separate batch file.

I've got as far as creating a separate .targets file and have got that to run, but I'm struggling when it comes to grabbing the files themselves. I can use a wildcard to grab files from the filesystem, but what I'd really like to do is get a list of files from the project file itself.

Here's a brief version of our project file – I've removed a lot of the 'default' stuff:

<?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 looks like this:

<?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>

This returns all of the matching *.scss files from the Solution directory, but I'm really interested in just returning files that are already listed in the project file that have the <Compile> child node set to True .

Is this possible?

This returns all of the matching *.scss files from the Solution directory, but I'm really interested in just returning files that are already listed in the project file that have the child node set to True.

You could add a filter to the SassFiles itemgroup where only the members with the appropriate criteria would appear.

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

@Abit, here is my MsBuild script (saved as "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>

This is the command I am running:

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

Here is the output:

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).

I'm not seeing any errors due to the condition on the @(SassFiles) ItemGroup, and I believe I'm seeing the correct output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM