使用 Jenkins/Git 增加 Maven 项目版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9807226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:39:21  来源:igfitidea点击:

Incrementing Maven Project Version with Jenkins/Git

gitmavenhudsonjenkinsmaven-release-plugin

提问by EngineerBetter_DJ

I'm using Jenkins to build Maven Java projects and deploy them to a Nexus repository. I also use Git, although I'm more used to Subversion so my Git knowledge is limited.

我正在使用 Jenkins 构建 Maven Java 项目并将它们部署到 Nexus 存储库。我也使用 Git,虽然我更习惯于 Subversion,所以我的 Git 知识有限。

I'd like Jenkins/Maven to:

我希望 Jenkins/Maven 能够:

  1. Merge feature branch into integration branch
  2. Build merged code, run unit tests
  3. If they pass, increment Maven version number
  4. Push merged code to origin's integration branch
  5. Deploy artifact to Nexus repository
  1. 将功能分支合并到集成分支
  2. 构建合并代码,运行单元测试
  3. 如果它们通过,则增加 Maven 版本号
  4. 将合并的代码推送到 origin 的集成分支
  5. 将工件部署到 Nexus 存储库

I gather the Git merging can be achieved thus: http://twasink.net/2011/09/20/git-feature-branches-and-jenkins-or-how-i-learned-to-stop-worrying-about-broken-builds/

我认为 Git 合并可以这样实现:http: //twasink.net/2011/09/20/git-feature-branches-and-jenkins-or-how-i-learned-to-stop-worrying-about-损坏的构建/

I've also read a lot about the maven-release-plugin.

我也阅读了很多关于maven-release-plugin.

I'm not sure how exactly to achieve the above results. If I hardcode SCM details into each project's POM, then won't the maven-release-pluginbe acting only on thatrepository rather than Jenkins' local one?

我不确定如何准确地实现上述结果。如果我将 SCM 详细信息硬编码到每个项目的 POM 中,那么它不会maven-release-plugin只作用于存储库而不是 Jenkins 的本地存储库吗?

If I use a solution of having Jenkins pass a environment variable to Maven to specify the version number, then I'd expect to have local version resolution issues in my IDE.

如果我使用让 Jenkins 将环境变量传递给 Maven 来指定版本号的解决方案,那么我希望在我的 IDE 中存在本地版本解析问题。

回答by ?ukasz R?anek

OK, this a quite a difficult to do. But it's doable. Let me explain the idea step by step.

好吧,这很难做到。但这是可行的。让我一步一步地解释这个想法。

  1. First we need to merge the two branches together. This is quite easy to do with Jenkins Git plugin, as it have this feature built in: Merge before buildoption is available in Advancedsection in your project configuration.

  2. So we got the code merged between those two branches, we can move to building the project - business as usual, Maven style Jenkins project.

  3. Now we need to increment the version of the project and it's modules, commit the merged branch and push the artifacts to the repository. And here we have two options to go with:

    1. Use maven-release-plugin to the work. Yes, it's possible, as no one said that you will need to put the URLs in your pom, as it can be provided in two different ways. The plugin will use one provided with command line switch -DconnectionUrl=. And if none will be provided, it will check release.properties file to get it from there. Last reserve is to go to the pom.xml ang get ${project.scm.connection}variable. So you can easily use the plugin to do all of the dirty work without any hassles and introduce that property using different techniques provided by Jenkins.
    2. You can use a combination of maven-versions-plugin(specificaly versions:setand versions:commitgoals) with maven-scm-pluginto do the tagging and committing the changes.

    In both cases you can use the Post Stepswith Run only if build succeedsoption and invoke the top-level Maven target as usual. For all of the settings mentioned above or described by the particular plugin there is an option to provide them easily in a text field. For uploading your artifacts to the Nexus repo I would go with Jenkins build in feature to Deploy artifacts to Maven repository, that is availble within your project configuration. This will require you to provide a repository configuration in either the POM definition, or as property.

  1. 首先,我们需要将两个分支合并在一起。使用 Jenkins Git 插件很容易做到这一点,因为它内置了功能:在项目配置的高级部分中提供了构建前合并选项。

  2. 所以我们在这两个分支之间合并了代码,我们可以开始构建项目 - 照常营业,Maven 风格的 Jenkins 项目。

  3. 现在我们需要增加项目及其模块的版本,提交合并的分支并将工件推送到存储库。在这里,我们有两个选择:

    1. 使用 maven-release-plugin 来工作。是的,这是可能的,因为没有人说您需要将 URL 放在 pom 中,因为它可以通过两种不同的方式提供。该插件将使用随命令行 switch 提供的插件-DconnectionUrl=。如果没有提供,它将检查 release.properties 文件以从那里获取它。最后保留是去 pom.xml ang get${project.scm.connection}变量。因此,您可以轻松地使用该插件轻松完成所有繁琐的工作,并使用 Jenkins 提供的不同技术引入该属性。
    2. 您可以使用maven-versions-plugin(特别是版本:设置版本:提交目标)与maven-scm-plugin 的组合来进行标记和提交更改。

    在这两种情况下,您都可以使用Post Stepswith Run only if build successfully选项并像往常一样调用顶级 Maven 目标。对于上面提到的或特定插件描述的所有设置,都有一个选项可以在文本字段中轻松提供它们。为了将您的工件上传到 Nexus存储库,我将使用 Jenkins 内置功能将工件部署到 Maven 存储库,这在您的项目配置中可用。这将要求您在 POM 定义中或作为属性提供存储库配置。

Hope that covers all of the questions you have. If you need me to be more specific in any range of the answer, don't hesitate to ask.

希望涵盖了您的所有问题。如果您需要我在任何范围内提供更具体的答案,请随时提出。