如何使用 NAnt 修改源代码?
我想在构建解决方案之前使用 NAnt 修改 .h 文件中的字符串。
.h 文件中有一个宏:#define SERVER_ADDRESS "www.customserver.net",我想在部署软件之前修改该字符串,以便通过在命令行中传递地址来为自定义地址进行每个构建。
有谁知道如何做到这一点?
谢谢!
I would like to modify the string in a .h file with NAnt before building the solution.
There is a macro in .h file: #define SERVER_ADDRESS "www.customserver.net" and I would like to modify the string before deploying software so each build could be made for custom address by passing the address in command line.
Does anyone know how this could be done?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
人们可以使用 loadfile 任务来帮助解决此问题。 此任务将给定文件加载到属性中。 真正有用的是当您应用带有 filterchain 时href="http://nant.sourceforge.net/release/latest/help/filters/replacetokens.html" rel="noreferrer">replacetokens 替换文件的某些区域。 例如,如果要定义一个类似于模板的头文件,如下所示:
可以使用 loadfile 任务将 @SERVER_ADDRESS_TOKEN@ 替换为任何字符串,然后使用 echo 任务来实际写入真实头文件退出。
这将生成一个 MyMacros.h 文件,其中包含修改后的 SERVER_ADDRESS 字符串。
One could use the loadfile task to help with this. This task loads the given file into a property. What is really useful is when you apply a filterchain with replacetokens to replace certain areas of the file. For example if one were to define a template-like header file that looked something like this:
One could the use the loadfile task to replace the @SERVER_ADDRESS_TOKEN@ with any string, and then use the echo task to actually write the real header file back out.
This will generate a MyMacros.h file with the modified string for the SERVER_ADDRESS.
我认为这不是使用 NAnt 的正确方法。 我不想那样修改文件内容。 我不相信这是可能的。
也许您可以为每种情况使用不同的文件,并根据输入参数指定它的路径。
就我个人而言,我认为这样的字符串不应该被硬编码到应用程序中。 如果它们要更改,最好将它们外部化到启动时读取的配置或属性文件中。 这样您就可以更改它们,而无需更改源或重新编译。
I don't think that's the proper way to be using NAnt. I wouldn't want to modify file contents that way. I don't believe it's possible.
Perhaps you can have a different file for each case and specify the path to it depending on an input parameter.
Personally, I think strings like that should not be hard-coded into the app. If they're going to change, better to externalize them into configuration or property files that are read on startup. That way you can change them without having to change source or recompile.