在 Ant 或 MSBuild 中编写构建脚本的任何最佳实践

发布于 2024-12-03 02:09:33 字数 1041 浏览 0 评论 0原文

我正在尝试使用 Ant 和 MSBuild 自动化项目(java 和 .net)的构建过程。我已经阅读过它们并知道如何在 Ant 和 MSBuild 中编写构建脚本。但我想知道,是否有任何编写构建脚本的指南或最佳实践?这是我发现的两个,但我想听到其他开发人员的更多信息。

  • 在编写构建脚本时,只需依赖位于 源代码管理并可在工作文件夹中使用 检查来源。不要编写以下构建脚本 依赖于未保留在源代码控制中的项目。
  • 如果一个项目包含一组子系统,并且每个子系统都有 它自己的构建脚本,项目的构建文件应该只调用 子系统的构建脚本。此外,这些文件应该导入一个通用的构建文件,其中包含编译、测试、打包等目标。

我见过 这篇文章也是如此,但它详细介绍了编写任务的方式。如上所述,我需要更高级别的指导方针。

以下是我从答案中收集的指南:

  1. 在干净的环境中运行每个构建。这意味着每个构建脚本都需要一个 clean 目标。
  2. 构建脚本通常应包含 compilepackagetest 目标。
  3. 如果一个产品有不同的开发线(例如 dev 、release), 它们都应该具有相同的构建脚本。但是,不同 应将参数传递给这些脚本。
  4. 在开发线上执行的构建通常包含编译、 打包、部署和安装步骤。但建立在发布的基础上 行包括进一步的步骤,例如标记产品和 生成变更日志/发行说明。
  5. 构建脚本也应该保留在源代码控制中。
  6. 尝试将所有构建信息保留在构建脚本中,而不是 持续集成服务器(Bamboo、TeamCity 等)
  7. 如果您的构建使用的参数将来可能会更改(例如 复制构建结果的网络地址),不要将其硬编码到 你的构建脚本。相反,使用构建参数来更多地控制它 容易地。

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:

  1. Run every build in a clean environment. It means that each build script needs a clean target.
  2. Build scripts should usually include compile, package and test targets.
  3. 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.
  4. 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.
  5. Build scripts should be kept on source control as well.
  6. try to keep all build information in build scripts, not on
    continuous integration server (Bamboo, TeamCity, etc.)
  7. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗酒趁年少 2024-12-10 02:09:33

如果您正在构建 VisualStudio 应用程序,那么最好使用 msbuild 而不是 Ant。 msbuild 命令将使用开发人员创建的解决方案文件进行构建,并基本上模拟他们所做的相同构建。

如果您确实想自动化一切,请查看 Jenkins。它有一个可以执行 msbuild 的插件,并且每次有人进行更改时都会自动触发构建。我将 msbuild 和 Ant 与 Jenkins 结合使用。我使用 msbuild 进行构建,然后使用 Ant 收集并压缩所有构建的工件。然后,人们可以直接从 Jenkins 下载构建的工件。

现在,对于 Java 应用程序,您必须使用 Ant。我使用以下准则

  • 每个构建脚本都需要一个 clean 目标。此目标会删除构建过程中添加的所有文件,将工作目录返回到发生clean之前的状态。
  • 我遵循 Maven 在使用目标名称时所做的操作,因此我的目标是诸如 cleancompilepackage 之类的名称。
  • 同样遵循 Maven 指南,我的所有构建文件都放置在 target 目录中。这样,我的 clean 命令可以简单地执行
  • 如果我有子项目,则每个子项目项目有自己的 build.xml 文件。我的主 build.xml 文件只是调用所有子项目的 build.xml 文件。
  • 使用Ivy 易于设置且易于使用,您不再有在源代码中存储 jar 文件的问题。 不幸的是,大多数
  • 较旧的项目mavenize都比它的价值要困难。
  • 使用 Jenkins 进行构建作为持续构建而且,离开部门的所有构建(UAT、QA 和保护构建)都必须是 Jenkins 构建。
  • 事实上,Jenkins 可以执行单元测试并在其构建页面上显示其结果。
  • 使用其他工具,例如 checkstyle、PMD、CPD、Findbugs 和其他代码验证产品。当然,Jenkins 可以运行其中的每一个并显示漂亮的图表,这些图表可用于向您的经理展示您的工作有多努力。

If you're building VisualStudio applications, you are better off using msbuild over Ant. The msbuild 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 fo msbuild and Ant with Jenkins. I do the build using msbuild, 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

  • Each build script needs a clean target. This target removes any files that were added during the build process, returning the working directory to a state before the clean took place.
  • I follow what Maven does when using target names, so my targets are names things like clean, compile, and package.
  • Also following Maven guidelines, all of my built files are placed in the target directory. That way, my clean command can simply do a <delete dir="${target.dir}/> and cleans everything up nice and sparkly.
  • If I have sub-project, each sub-project has its own build.xml file. My master build.xml file simply calls all of the sub-projects' build.xml files.
  • Use Ivy. It's easy to setup and easy to use. You no longer have the problem of storing jar files in your source repository, or losing what versions of what jar files you're dependent upon.
  • Heck, if you can, use Maven and be done with it. Unfortunately, most older projects are harder to mavenize than it's worth.
  • Do your builds using Jenkins as a continuous build server. And, all builds that leave the department (UAT, QA, and protection builds) must be a Jenkins build.
  • Encourage your developers to write Unit tests. In fact, Jenkins can execute unit tests and display their results on its build page.
  • Use other things like checkstyle, PMD, CPD, Findbugs, and other code verification products. And, of course, Jenkins can run each of these and display pretty graphs that can be used to show your manager how hard you're working.
狂之美人 2024-12-10 02:09:33

只是对您上面提到的第二个要点的评论:

如果一个项目包含一组子系统,并且每个子系统都有自己的
自己的构建脚本,项目的构建文件应该只调用
构建子系统的脚本。

每个子系统都应该有自己的构建文件,但该文件应该导入一个公共构建文件。通用构建文件将包含编译、测试、打包等目标。

http://ant .apache.org/manual/Tasks/import.html

子系统构建文件非常简单,不包含重复内容,仅包含特定于该子系统的信息(例如compile.classpath)。

Just a comment on the second bullet point you mentioned above:

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.

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).

z祗昰~ 2024-12-10 02:09:33

在 MSBuild 中,目标应尽可能指定输入和输出(以启用依赖项计算)。如有必要,请使用 Returns 属性来指定返回与用于依赖性计算的项目不同的项目的目标。

给定目标的输入和输出项应使用项转换(对于简单的基于路径的转换)或另一个目标(对于更复杂的转换)生成。

对于 MSBuild 4.x 之前的版本,对于您在 .targets 文件中定义的目标,请考虑使用以下模式使使用者能够在您定义的目标之前注入自己的目标:

<PropertyGroup>
  <MyTargetDependsOn>
    Target1;
    Target2;
    SomeOtherTarget
  </MyTargetDependsOn>
</PropertyGroup>
<Target
  Name="MyTarget"
  DependsOnTargets="$(MyTargetDependsOn)">

</Target>

这使使用者能够在指定的目标之前注入自己的目标只需修改 MyTargetsDependsOn 属性的值:

<PropertyGroup>
  <MyTargetDependsOn>
    $(MyTargetDependsOn);
    YetAnotherTarget
  </MyTargetDependsOn>
</PropertyGroup>
<Target
  Name="YetAnotherTarget">

</Target>

在 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:

<PropertyGroup>
  <MyTargetDependsOn>
    Target1;
    Target2;
    SomeOtherTarget
  </MyTargetDependsOn>
</PropertyGroup>
<Target
  Name="MyTarget"
  DependsOnTargets="$(MyTargetDependsOn)">

</Target>

This enables consumers to inject their own targets before the specified target simply by modifying the value of the MyTargetsDependsOn property:

<PropertyGroup>
  <MyTargetDependsOn>
    $(MyTargetDependsOn);
    YetAnotherTarget
  </MyTargetDependsOn>
</PropertyGroup>
<Target
  Name="YetAnotherTarget">

</Target>

In MSBuild 4.x, you can simply use the BeforeTargets and AfterTargets attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文