Visual Studio 仅包含自定义构建步骤的项目(无默认构建)

Visual Studio project with a custom build step only (no default build)

我想创建一个 Visual Studio 项目,让我可以看到一堆 JavaScript 和其他文件并正常编辑它们,但也有一个构建步骤可以 运行 我想要的任何自定义命令(目前是一些 npm 命令,以后可能会更多)。基本上我想要 3 个功能组合:

  1. 能够像对任何 VS 项目(C#、C++ 等)一样浏览和编辑文件
  2. 能够通过在 Visual Studio 中选择“构建”来 运行 自定义构建步骤(包括构建整个解决方案)。
  3. 能够从命令行 (MSBuild) 运行 相同的自定义构建步骤。

使用“共享项目”(.shproj) 可以轻松查看和编辑文件,但上下文菜单中没有构建项,即使我手动添加构建目标也是如此:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Label="Globals">
    <ProjectGuid>...</ProjectGuid>
  </PropertyGroup>

  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />

  <Import Project="MyItems.projitems" Label="Shared" />

  <PropertyGroup>
    <Configuration>Debug</Configuration>
    <Platform>Any CPU</Platform>
  </PropertyGroup>

  <Target Name="Build">
    <Exec Command="ECHO My custom build!" />
  </Target>  
</Project>

我也尝试过使用精简的 VC++ 项目(因为我实际上不想 运行 C++ 编译器),这允许构建 运行 来自 VS,但打开项目会记录 error MSB4057: The target "GetProjectDirectories" does not exist in the project. 之类的警告,并尝试添加文件以失败并出现该错误或类似错误。

一定有更简单的方法来做到这一点!

根据你目前的描述,我认为你想在 VS 中创建一个 js 项目 IDE。

但是,VS IDE默认有node js project template。并且你应该在VS_Installer下安装workloadNode.jsdevelopment,这样你就可以使用它了。

之后就可以创建这样的项目了。

1)通过right-click在项目上添加js文件或其他文件-->Add-->现有项目 以便您可以在 VS 上修改文件 IDE.

2)如果你想执行一个不破坏整个构建的自定义构建步骤,你应该让自定义目标依赖于默认构建。

你可以使用这个:

<Target Name="CustomStep" AfterTargets="Build">
  <Exec Command="ECHO My custom build!" />
</Target>

<Target Name="CustomStep" BeforeTargets="Build">
  <Exec Command="ECHO My custom build!" />
</Target>

注意:如果使用

<Target Name="Build">
    <Exec Command="ECHO My custom build!" />
  </Target>  

它将覆盖系统构建过程,取而代之的是 运行 命令,这会破坏整个默认构建。

3)如果你想在msbuild命令上执行自定义构建,你应该指定自定义目标的名称:

msbuild xxx\xxx.proj -t: CustomStep(the name of the custom target)

============================================= ==

外,如果您仍想使用C++项目模板,您可以创建一个不包含任何clcompile的空c++项目文件,然后执行相同的步骤。

如果您不想使用 C++ 编译器,您应该只删除 vcxproj 文件中的任何 xml 节点,如下所示:

 <ClCompile Include="xxx.cpp" />
 <ClInclude Include="xxx.h" />

你使用空的C++项目时,你就不用担心了。

=========================================

更新 1

如果你想在没有VS的构建服务器上构建这个项目IDE,我建议你可以安装Build Tool for VS2019,这是一个独立的,轻量级的构建命令行(相当于dotnet cli).

Build Tool for VS2019

所有下载-->工具 Visual Studio 2019--> 构建工具Visual Studio 2019

然后,你必须安装相关的构建工作负载,例如Node.js构建工具,然后我们可以使用在构建服务器上构建 node.js 项目的命令行。

整个安装过程很快。

受 Perry Qian-MSFT 回答的启发,我设法将一个 Node.js 项目精简到最低限度,我需要 Visual Studio 来加载和构建它,但没有引用任何外部文件。

主要技巧是VS 需要定义一个名为“CoreCompile”的目标 来显示Build 菜单项! (它还需要一个“构建”目标,但那个更明显。)

我的项目现在看起来像这样:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>(some guid)</ProjectGuid>
    <ProjectHome>.</ProjectHome>
    <ProjectTypeGuids>{3AF33F2E-1136-4D97-BBB7-1795711AC8B8};{9092AA53-FB77-4645-B42D-1CCCA6BD08BD}</ProjectTypeGuids>
  </PropertyGroup>

  <!-- These property groups can be empty, but need to be defined for VS -->
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
  </PropertyGroup>

  <Import Project="My.Build.targets" />
  
  <!-- Define empty standard MSBuild targets, since this project doesn't have them. Doing it this way allows My.Build.targets to also be used in a project that does define them. -->
  <Target Name="Build" />
  <Target Name="ReBuild" />
  <Target Name="Clean" />
  <!-- NOTE: a target named "CoreCompile" is needed for VS to display the Build menu item. -->
  <Target Name="CoreCompile" />

  <!-- Files shown in Visual Studio - adding and removing these in the UI works as expected -->
  <ItemGroup>
    <Content Include="myfile..." />
  </ItemGroup>
</Project>

而 My.Build.targets 看起来像这样:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="MyBuild" AfterTargets="Build">(build steps)</Target>
  <Target Name="MyReBuild" AfterTargets="ReBuild">(re-build steps)</Target>
  <Target Name="MyClean" AfterTargets="Clean">(clean steps)</Target>

  <!-- This target is needed just to suppress "warning NU1503: Skipping restore for project '...'. The project file may be invalid or missing targets
  required for restore." -->
  <Target Name="_IsProjectRestoreSupported" Returns="@(_ValidProjectsForRestore)">
    <ItemGroup>
      <_ValidProjectsForRestore Include="$(MSBuildProjectFullPath)" />
    </ItemGroup>
  </Target>
</Project>