在 Visual Studio 中使用自动构建进行条件编译
这就是我想要做的:
- 单个构建脚本
- 该脚本从同一个 Visual Studio 项目构建两个可执行文件。
- 第一个编译的 .exe 有少量代码禁用。
- 另一个已编译的 .exe 已启用所有内容。
我一直在阅读条件编译,这可以解决我需要启用/禁用代码块。
我只是不知道如何使用 msbuild 从构建脚本控制条件编译。
有没有办法从构建脚本或其他方式操纵条件编译变量来完成我想要做的事情?
Here's what I'm trying to do:
- A single build script
- That script builds two executables from the same Visual Studio project.
- The first compiled .exe has a small amount of code disabled.
- The other compiled .exe has everything enabled.
I've been reading up on conditional compilation and that takes care of my needs as far as enabling/disabling blocks of code.
I just can't figure out how to control conditional compilation from a build script using msbuild.
Is there a way to manipulate conditional compilation variables from a build script or some other way to accomplish what I'm trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在项目文件中使用构建配置。 在根据配置选择性包含的 PropertyGroup 中设置参数。 然后,该配置还可以定义程序集的两个不同版本的输出路径。
对于需要删除某些代码的版本,请使用包含 PropertyGroup 的配置。
然后使用 MSBuild 脚本调用项目 MSBuild 脚本两次,并使用 MSBuild 任务的 Properties 属性来指定要生成的配置:
Use build configurations in your project file. Set the parameters in a PropertyGroup that is optionally included based on the configuration. The configuration can then also define the output path for the two different versions of the assembly.
For the version that needs to remove some code use a configuration that includes the PropertyGroup.
Then use an MSBuild script that calls the project MSBuild script twice and uses the Properties attribute of the MSBuild task to specify the configuration to build:
哈米什比我先一步。
这是使用相同概念的替代解决方案:
在命令行:
第一个和第三个“msbuild -t:Clean”确保您没有以前构建中留下的粪便。 第二个“msbuild”在没有条件定义的情况下构建,而第四个“msbuild”则使用条件定义构建。
如果以上只是拍摄项目中的几个,那么批处理文件可能就足够了。 我建议学习一些 MSBuild,并像 Hamish 所做的那样,在 MSBuild 文件中实际编写所有内容的脚本。
Hamish beat me to it.
Here's an alternate solution using the same concepts:
At the command line:
The 1st and 3rd 'msbuild -t:Clean' ensures that you don't have left over turds from previous builds. The 2nd 'msbuild' builds without the conditional define, while the 4rth builds with the conditional define.
If the above are just a couple on shot items, then a batch file maybe enough. I recommend learning a bit of MSBuild and actually scripting everything in a MSBuild file as Hamish has done.
如果您不想为两次编译创建单独的目标,则可以通过在第二次调用构建时在 DefineConstants 属性中指定条件定义来实现:
请注意,如果您这样做,则需要还要覆盖 AssemblyName,否则您的第二个构建可能会从第一个构建中选择中间文件。
您还应该查看 MSDN 上的 MSBuild 任务 文档,其中有那里有一些有趣的花絮。
If you don't want to create a separate target for the two compilations, you can do it by specifying the conditional define in the DefineConstants property when you call the build the second time:
Note that if you do it this way, you need to also overwrite the AssemblyName, otherwise your second build might pick intermediate files from the first build.
You should also look at the MSBuild task docs on MSDN, there are some interesting tidbits there.