MSBuild 在构建项目时寻找 Main
MSBuild looks for Main when building a project
我正在处理一个包含多个可执行文件的大型项目。我想通过一次构建所有这些来自动化部署过程。这就是我求助于 msbuild
命令行实用程序的原因。
当我在 Visual Studio 中构建我的项目之一时,它构建正常。当我尝试使用 msbuild cmd 执行相同操作时,它失败并显示错误
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [pathToLibrary.csproj]
这是cmd代码:
msbuild MainProject.csproj -property:Configuration=Release -property:Platform="AnyCPU" -property:OutputType="WinExe" -target:"Rebuild"
pathToLibrary.csproj 确实是一个库所以我不知道为什么 msbuild
试图找到一个主要方法。有none。这是一个图书馆。
我在这里错过了什么?
pathToLibrary.csproj indeed is a library so I don't know why msbuild
is trying to find a main method. There is none. It is a library.
当你的主项目引用Add Reference
-->Projects
的项目时,它总是会同时构建主项目和被引用的项目。因此,当您通过 MSBuild 命令行构建该项目时,-property:OutputType=winexe
也将应用于这些引用的项目。当您在 VS IDE 和 MSBuild 中构建项目时,您将在输出日志中看到这些信息。
If there is no way to instruct msbuild to only overwrite OutputType
for main .csproj, editing every file separately will have to do
如果你只是想找到一种方法来指定 -property:OutputType=winexe
到主项目而不是 MSBuild 命令行引用的项目,我认为没有这样的功能。
或者你可以试试我的建议:
1)请去掉MSBuild命令行中的-property:OutputType=winexe
,在创建相关工程时,已经指定了工程的输出类型,所以不用不需要在 MSBuild 中指定它,这不是 MSBuild 的工作。
注意可以像<OutputType>WinExe</OutputType>
.
一样直接修改xxx.csproj
中的属性
2) 如果您在使用 MSBuild 命令行构建项目时仍然需要此功能,我建议您可以创建一个名为 Directory.Build.targets 的脚本,它可以覆盖OutputType
属性 然后用 MSBuild 单独构建项目。
~a) 请在 xxxx.csproj
文件存在的每个项目文件夹中创建一个名为 Directory.Build.targets
的文件。
~b)把项目相关的属性写在里面:(在console项目中使用Exe,在 windows 项目中使用 WinExe,在 class 库项目中使用 Library。)
<Project>
<Target Name="inputproperty" BeforeTargets="AssignProjectConfiguration">
<PropertyGroup>
<OutputType>WinExe</OutputType>
</PropertyGroup>
</Target>
</Project>
在你的 MainProject 项目中这样写,然后在你的 pathToLibrary 项目中创建另一个 Directory.Build.targets
以使用 <OutputType>Library</OutputType>
以达到您的期望。
我正在处理一个包含多个可执行文件的大型项目。我想通过一次构建所有这些来自动化部署过程。这就是我求助于 msbuild
命令行实用程序的原因。
当我在 Visual Studio 中构建我的项目之一时,它构建正常。当我尝试使用 msbuild cmd 执行相同操作时,它失败并显示错误
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [pathToLibrary.csproj]
这是cmd代码:
msbuild MainProject.csproj -property:Configuration=Release -property:Platform="AnyCPU" -property:OutputType="WinExe" -target:"Rebuild"
pathToLibrary.csproj 确实是一个库所以我不知道为什么 msbuild
试图找到一个主要方法。有none。这是一个图书馆。
我在这里错过了什么?
pathToLibrary.csproj indeed is a library so I don't know why msbuild is trying to find a main method. There is none. It is a library.
当你的主项目引用Add Reference
-->Projects
的项目时,它总是会同时构建主项目和被引用的项目。因此,当您通过 MSBuild 命令行构建该项目时,-property:OutputType=winexe
也将应用于这些引用的项目。当您在 VS IDE 和 MSBuild 中构建项目时,您将在输出日志中看到这些信息。
If there is no way to instruct msbuild to only overwrite OutputType for main .csproj, editing every file separately will have to do
如果你只是想找到一种方法来指定 -property:OutputType=winexe
到主项目而不是 MSBuild 命令行引用的项目,我认为没有这样的功能。
或者你可以试试我的建议:
1)请去掉MSBuild命令行中的-property:OutputType=winexe
,在创建相关工程时,已经指定了工程的输出类型,所以不用不需要在 MSBuild 中指定它,这不是 MSBuild 的工作。
注意可以像<OutputType>WinExe</OutputType>
.
xxx.csproj
中的属性
2) 如果您在使用 MSBuild 命令行构建项目时仍然需要此功能,我建议您可以创建一个名为 Directory.Build.targets 的脚本,它可以覆盖OutputType
属性 然后用 MSBuild 单独构建项目。
~a) 请在 xxxx.csproj
文件存在的每个项目文件夹中创建一个名为 Directory.Build.targets
的文件。
~b)把项目相关的属性写在里面:(在console项目中使用Exe,在 windows 项目中使用 WinExe,在 class 库项目中使用 Library。)
<Project>
<Target Name="inputproperty" BeforeTargets="AssignProjectConfiguration">
<PropertyGroup>
<OutputType>WinExe</OutputType>
</PropertyGroup>
</Target>
</Project>
在你的 MainProject 项目中这样写,然后在你的 pathToLibrary 项目中创建另一个 Directory.Build.targets
以使用 <OutputType>Library</OutputType>
以达到您的期望。