如何使csproj建筑物sln中指定的MSBuild建筑物自定义目标? [英] How make MSBuild build custom target specified in csproj building sln?

查看:122
本文介绍了如何使csproj建筑物sln中指定的MSBuild建筑物自定义目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正遇到MSBuild的问题,我一个人无法解决.结果,我依靠社区的智慧.

I am facing an issue with MSBuild I can't overcome it by myself. As a result I rely on community's wisdom.

我遇到的实际情况

我有一个解决方案文件,其中包含多个项目,这些项目与同一解决方案中的其他项目具有依赖性.我想将自定义目标附加到项目的csproj文件之一,并从命令行进行构建.这将使我能够为该项目制作所有必要的输出二进制文件,以便在自定义目标的构建过程中进行进一步处理.但是最主要的是我不知道该怎么做,谷歌搜索也无济于事.

I have a soluiton file containing several projects with dependencies to other projects in same solution. I'd like to append a custom target to one of the project's csproj file and build it from the command line. It will allow me to make all the necessary output binaries for this project for further processing during the building of the custom target. But the main thing is that I can't figure out how to do it, googling doesn't help either.

简化

为了简化操作,我决定创建一个新的C#控制台项目,向该项目的文件中添加一个简单的自定义目标,然后尝试使其构建.仍然没有成功!这是我到目前为止所做的:

To make thing simplier I decided to make a new C# console project, add a simple custom target to the project's file and try to make it build. Still no success! Here what I've done so far:

  1. 使用默认控制台项目coreapp创建了一个解决方案应用.这给了我至少两个文件:

  1. Created a solution app with a default console project coreapp. This gaves me at least two files:

  • app.sln
  • coreapp \ coreapp.csproj

修改后的coreapp.csproj,并在Project标记内添加了我的自定义目标

Modified coreapp.csproj with addition of my custom target inside of the Project tag

<Target Name="SampleTarget">
  <Message Text="This is a SampleTarget" />
</Target>

  • 在命令行上运行以下命令

  • Run on the command line the following command

    %windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp:SampleTarget

    甚至

    %windir%\Microsoft.NET\framework\v3.5\msbuild.exe app.sln /t:coreapp.csproj:SampleTarget

    结果

    没有运气,正面临错误

    MSB4057: The target "coreapp.csproj:SampleTarget" does not exist in the project.

    我怀疑MSBuild的想法与我想要的想法根本不同...

    I suspect that MSBuild thinks somehting fundamentally different from what I want it to think...

    此外,我还尝试在同一命令行上设置环境变量MSBuildEmitSolution = 1,以强制msbuild转储它在处理解决方案时创建的临时解决方案文件.实际上,在此文件中,没有这样的目标.但是我想这不是原因,因为我要求msbuild在目标SampleTarget真正驻留的地方构建coreapp.proj.

    BEsides that, I also tried to set on the same command line the environment variable MSBuildEmitSolution=1 to force msbuild dump a temporary solution file it creates while processing the solution. In this file, indeed, no such target. However I guess it isn't the reason because I asked msbuild to build coreapp.proj where target SampleTarget really resides.

    问题是如何在此简化方案中使用解决方案文件构建SampleTarget,因为它很可能包含包含此SampleTarget目标的项目的依赖项?

    The question is how to build SampleTarget in this simplified scenario using solution file since potencially it can contain dependencies for the project containing this SampleTarget target?

    我将竭诚为进一步的调查提供帮助或帮助!

    I'd be greatful for any sort of help or firection for further investigation!

    推荐答案

    您可以尝试创建新的独立msbuild文件,而不是在项目文件中插入自定义目标,

    Instead of inserting a custom target in your project file, you could try creating a new standalone msbuild file, which would:

    • 构建解决方案文件(用于构建项目)
    • 定义您的额外目标

    例如,将其称为app-custom-Debug.msbuild.

    Call it app-custom-Debug.msbuild , for example.

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <WorkingFolder>$(MSBuildProjectDirectory)</WorkingFolder>
        <Configuration>Debug</Configuration>
        <SolutionFile>app.sln</SolutionFile>
      </PropertyGroup>
    
      <Target Name="Build" DependsOnTargets="Compile" />  
    
      <Target Name="Compile">
        <Message Text="=== COMPILING $(Configuration) configuration ===" />
        <MSBuild Projects="$(SolutionFile)"
                 Properties="Configuration=$(Configuration)" />
      </Target>
    
      <Target Name="SampleTarget">
        <Message Text="This is a SampleTarget" />
      </Target>
    
    </Project>
    

    然后执行:

    msbuild.exe app-custom-Debug.msbuild /t:SampleTarget
    

    这篇关于如何使csproj建筑物sln中指定的MSBuild建筑物自定义目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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