仅具有自定义构建步骤的 Visual Studio 项目(无默认构建) [英] Visual Studio project with a custom build step only (no default build)

查看:48
本文介绍了仅具有自定义构建步骤的 Visual Studio 项目(无默认构建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a Visual Studio project that would allow me to see a bunch of JavaScript and other files and edit them as normal, but would also have a build step that can run any custom commands I want (currently some npm commands, possibly more later). Basically I want 3 features combined:

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

使用共享项目"(.shproj) 允许我轻松查看和编辑文件,但是上下文菜单中没有 Build 项,即使我手动添加了 Build 目标:

Using a "shared project" (.shproj) allows me to easily see and edit the files, but there is no Build item in the context menu, even if I manually add a Build target:

<?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 运行构建,但打开项目会记录警告,如 错误 MSB4057:目标GetProjectDirectories";项目中不存在. 并尝试添加文件失败,并出现该错误或类似错误.

I've also tried using a stripped-down VC++ project (since I don't actually want to run the C++ compiler) and this allows a build to be run from VS, but opening the project logs warnings like error MSB4057: The target "GetProjectDirectories" does not exist in the project. and trying to add files to fails with that error or similar ones.

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

There must be an easier way to do this!

推荐答案

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

Inspired by Perry Qian-MSFT's answer, I managed to strip down a Node.js project to the bare minimum that I needed to get Visual Studio to load and build it, but without referencing any external files.

主要技巧是 VS 需要一个名为CoreCompile"的目标;被定义以显示构建菜单项!(它还需要一个构建"目标,但那个更明显.)

The main trick was VS needs a target named "CoreCompile" to be defined to show the Build menu item! (It also needs a "Build" target, but that one is more obvious.)

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

My project now looks like this:

<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 看起来像这样:

And My.Build.targets looks like this:

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

这篇关于仅具有自定义构建步骤的 Visual Studio 项目(无默认构建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆