[MSBuild]自定义属性Directory.Build.props

MSBuild 提供了注入一些自定义的变量,然后在工程文件中使用它。

 

默认Directory.Build.props这个文件需要放置到sln同级得目录下面

<Project>
 <PropertyGroup>
   <BuildFolder>Tester1\**\*.cs;Tester2\**\*.cs;</BuildFolder>
 </PropertyGroup>
</Project>

如上图,自定义一个BuildFolder的属性

紧接着,可以在工程文件中使用这个属性,使用这个属性主要是为了选择某些特定文件夹下面的代码,进行编译

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{63E2E581-BADB-4FEB-AC8E-EB11192CC431}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>AutoTestMock</RootNamespace>
    <AssemblyName>AutoTestMock</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Base.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <CSFile Include="$(BuildFolder)" />
  </ItemGroup>
  <Target Name="Compile" DependsOnTargets="PreBuild">
    <Csc Sources="@(CSFile)" References="@(Reference)" OutputAssembly="$(builtdir)\$(MSBuildProjectName).exe" TargetType="exe" />
  </Target>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

核心代码在这一句

<CSFile Include="$(BuildFolder)" />

 

posted @ 2022-08-18 14:52  内心澎湃的水晶侠  阅读(321)  评论(0编辑  收藏  举报