在 Ant 或 MSBuild 中编写构建脚本的任何最佳实践
我正在尝试使用 Ant 和 MSBuild 自动化项目(java 和 .net)的构建过程。我已经阅读过它们并知道如何在 Ant 和 MSBuild 中编写构建脚本。但我想知道,是否有任何编写构建脚本的指南或最佳实践?这是我发现的两个,但我想听到其他开发人员的更多信息。
- 在编写构建脚本时,只需依赖位于 源代码管理并可在工作文件夹中使用 检查来源。不要编写以下构建脚本 依赖于未保留在源代码控制中的项目。
- 如果一个项目包含一组子系统,并且每个子系统都有 它自己的构建脚本,项目的构建文件应该只调用 子系统的构建脚本。此外,这些文件应该导入一个通用的构建文件,其中包含编译、测试、打包等目标。
我见过 这篇文章也是如此,但它详细介绍了编写任务的方式。如上所述,我需要更高级别的指导方针。
以下是我从答案中收集的指南:
- 在干净的环境中运行每个构建。这意味着每个构建脚本都需要一个
clean
目标。 - 构建脚本通常应包含
compile
、package
和test
目标。 - 如果一个产品有不同的开发线(例如 dev 、release), 它们都应该具有相同的构建脚本。但是,不同 应将参数传递给这些脚本。
- 在开发线上执行的构建通常包含编译、 打包、部署和安装步骤。但建立在发布的基础上 行包括进一步的步骤,例如标记产品和 生成变更日志/发行说明。
- 构建脚本也应该保留在源代码控制中。
- 尝试将所有构建信息保留在构建脚本中,而不是 持续集成服务器(Bamboo、TeamCity 等)
- 如果您的构建使用的参数将来可能会更改(例如 复制构建结果的网络地址),不要将其硬编码到 你的构建脚本。相反,使用构建参数来更多地控制它 容易地。
I am trying to automate the build process of my projects (both java and .net) using Ant and MSBuild. I’ve read about them and know how to write build scripts in Ant and MSBuild. But I wonder, is there any guidelines or best practices for writing build scripts in general? Here are two that I’ve found, but I would like to hear more from other developers.
- In writing a build script, just rely on the items that are located in
the source control and are available in the working folder while
checking out the sources. Do NOT write build scripts that are
dependent on items that are not kept on source control. - If a project contains a set of sub-systems and each sub-system has
its own build scripts, the build file of the project should just call
the build scripts of sub-systems. In addition, these files should import a common build file that contain targets such as compile, test, package etc.
I've seen this post as well, but it is detailed on the way of writing tasks. I need more high level guidelines, as mentioned above.
Here are the guidelines I collected from answers:
- Run every build in a clean environment. It means that each build script needs a
clean
target. - Build scripts should usually include
compile
,package
andtest
targets. - If a product have different development lines (e.g. dev , release),
all of them should have the same build script. But, different
parameters should be passed to these scripts. - Builds performed on development lines usually contain compiling,
packaging, deploying and installing steps. But builds on release
lines include further steps, such as tagging the product and
generating change-logs/release notes. - Build scripts should be kept on source control as well.
- try to keep all build information in build scripts, not on
continuous integration server (Bamboo, TeamCity, etc.) - If your build uses a parameter that may change in future (e.g the
network address to copy the build results), do not hardcode it into
your build script. instead, use build parameters to control it more
easily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在构建 VisualStudio 应用程序,那么最好使用
msbuild
而不是 Ant。msbuild
命令将使用开发人员创建的解决方案文件进行构建,并基本上模拟他们所做的相同构建。如果您确实想自动化一切,请查看 Jenkins。它有一个可以执行 msbuild 的插件,并且每次有人进行更改时都会自动触发构建。我将
msbuild
和 Ant 与 Jenkins 结合使用。我使用msbuild
进行构建,然后使用 Ant 收集并压缩所有构建的工件。然后,人们可以直接从 Jenkins 下载构建的工件。现在,对于 Java 应用程序,您必须使用 Ant。我使用以下准则
clean
目标。此目标会删除构建过程中添加的所有文件,将工作目录返回到发生clean
之前的状态。target
目录中。这样,我的 clean 命令可以简单地执行build.xml
文件。我的主build.xml
文件只是调用所有子项目的build.xml
文件。If you're building VisualStudio applications, you are better off using
msbuild
over Ant. Themsbuild
command will do the build using the solution file your developers created, and basically emulate the same build they do.If you truly want to automate everything, take a look at Jenkins. It has a plugin that can execute
msbuild
and will automatically trigger a build every time someone makes a change. I use a combination fomsbuild
and Ant with Jenkins. I do the build usingmsbuild
, then use Ant to gather up and zip up all of my built artifacts. Then, people can download the built artifacts directly from Jenkins.Now, with your Java applications, you'll have to use Ant. I use the following guidelines
clean
target. This target removes any files that were added during the build process, returning the working directory to a state before theclean
took place.target
directory. That way, my clean command can simply do a<delete dir="${target.dir}/>
and cleans everything up nice and sparkly.build.xml
file. My masterbuild.xml
file simply calls all of the sub-projects'build.xml
files.只是对您上面提到的第二个要点的评论:
每个子系统都应该有自己的构建文件,但该文件应该导入一个公共构建文件。通用构建文件将包含编译、测试、打包等目标。
http://ant .apache.org/manual/Tasks/import.html
子系统构建文件非常简单,不包含重复内容,仅包含特定于该子系统的信息(例如compile.classpath)。
Just a comment on the second bullet point you mentioned above:
Each subsystem should have its own build file but that file should import a common build file. The common build file would contain targets such as compile, test, package etc.
http://ant.apache.org/manual/Tasks/import.html
The subsystem build files are then very simple, don't contain duplication and only contain information that is specific to that subsystem (e.g. compile.classpath).
在 MSBuild 中,目标应尽可能指定输入和输出(以启用依赖项计算)。如有必要,请使用 Returns 属性来指定返回与用于依赖性计算的项目不同的项目的目标。
给定目标的输入和输出项应使用项转换(对于简单的基于路径的转换)或另一个目标(对于更复杂的转换)生成。
对于 MSBuild 4.x 之前的版本,对于您在 .targets 文件中定义的目标,请考虑使用以下模式使使用者能够在您定义的目标之前注入自己的目标:
这使使用者能够在指定的目标之前注入自己的目标只需修改 MyTargetsDependsOn 属性的值:
在 MSBuild 4.x 中,您可以简单地使用 BeforeTargets 和 AfterTargets 属性。
In MSBuild, targets should specify inputs and outputs, wherever possible (to enable dependency calculation). If necessary, make use of the Returns attribute to specify a target that returns different items than the ones used for dependency calculation.
The input and output items for a given target should be generated using either item transforms (for simple, path-based transformations), or another target (for more sophisticated transformations).
For MSBuild pre-4.x, for targets you define in a .targets file, consider using the following pattern to enable consumers to inject their own targets before the ones you are defining:
This enables consumers to inject their own targets before the specified target simply by modifying the value of the MyTargetsDependsOn property:
In MSBuild 4.x, you can simply use the BeforeTargets and AfterTargets attributes.