使用 git flow 发布时,我应该如何更新 pom.xml 中的版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29301818/
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
How should I update the version inside my pom.xml when releasing using git flow?
提问by yankee
In maven projects the version of a project is contained in the <version>
attritbute of the pom.xml file. When creating a new release in the git flow model I need to bumb the version number. This articleexplains how this is done (without maven):
在 maven 项目中,项目的版本包含在<version>
pom.xml 文件的属性中。在 git flow 模型中创建新版本时,我需要修改版本号。这篇文章解释了这是如何完成的(没有 maven):
- Create a release branch
- Change the version number and commit
- Merge the release branch both to develop and master
- 创建发布分支
- 更改版本号并提交
- 合并发布分支以进行开发和掌握
Additionally it says:
另外它说:
It is exactly at the start of a release branch that the upcoming release gets assigned a version number—not any earlier. Up until that moment, the develop branch reflected changes for the “next release”, but it is unclear whether that “next release” will eventually become 0.3 or 1.0, until the release branch is started. That decision is made on the start of the release branch and is carried out by the project's rules on version number bumping.
正是在发布分支的开始,即将发布的版本被分配了一个版本号——而不是更早的版本。在那一刻之前,develop 分支反映了“下一个版本”的变化,但尚不清楚“下一个版本”最终会变成 0.3 还是 1.0,直到发布分支启动。该决定是在发布分支开始时做出的,并由项目关于版本号增加的规则执行。
I see two problems in conjunction with maven here:
我在这里看到与 maven 相关的两个问题:
- The version under development in maven would be [next version]-SNAPSHOT. So we cannot really postpone the decision which version is next up to the moment we create a release branch. Of course if we can change our mind later, but we already need to enter /some value/ here earlier.
- Before creating our release the version in the pom.xml was let's say
1.1-SNAPSHOT
. Now we have changed that to simply1.1
on the release branch and merged that to master. Fine. But we should also merge that branch back to develop and for that we need to adapt the version to e.g.1.2-SNAPSHOT
. And probably we should not have done that on the release branch because that commit should not be part of the release. Actually we probably should have made this change right after branching off develop because all future commits on develop will be for the next version.
- maven 中正在开发的版本将是 [下一个版本]-SNAPSHOT。所以我们不能真正推迟决定下一个版本到我们创建发布分支的那一刻。当然,如果我们以后可以改变主意,但我们已经需要提前在这里输入 /some value/ 了。
- 在创建我们的发行版之前,我们假设 pom.xml 中的版本是
1.1-SNAPSHOT
. 现在我们已将其更改为简单地1.1
在发布分支上并将其合并到 master。美好的。但是我们也应该将该分支合并回开发,为此我们需要将版本调整为例如1.2-SNAPSHOT
. 也许我们不应该在发布分支上这样做,因为该提交不应该是发布的一部分。实际上,我们可能应该在分支开发之后立即进行此更改,因为所有未来的开发提交都将用于下一个版本。
When googling for the problem I found some articles about maven-plugins that can automate the process, which may be interesting, but this question is really on how the git graph should look like and where the version bump commits should be and not how I can automate this using a maven-plugin.
在谷歌搜索这个问题时,我发现了一些关于可以自动化该过程的 maven-plugins 的文章,这可能很有趣,但这个问题实际上是关于 git 图应该是什么样子以及版本碰撞提交应该在哪里,而不是我怎么做使用 maven-plugin 自动执行此操作。
回答by chris
For normal releases, just do the snapshot version bump after merging the release branch:
对于普通发布,只需在合并发布分支后进行快照版本碰撞:
- Create the release branch off
develop
and remove the snapshot from the version - Merge it into
master
- Merge it into
develop
- Change the version on
develop
to the next snapshot version - Push both
master
anddevelop
- 创建发布分支
develop
并从版本中删除快照 - 合并成
master
- 合并成
develop
- 将版本更改为
develop
下一个快照版本 - 同时推
master
和develop
As you push all the changes at the same time, the team will only see the snapshot version increase.
当您同时推送所有更改时,团队只会看到快照版本增加。
For hotfixes, this is not possible as you create it off the master
branch. There is a workaroundfor this scenario, here's an example using the raw git commands.
对于修补程序,这是不可能的,因为您在master
分支之外创建它。这种情况有一个解决方法,这是一个使用原始 git 命令的示例。
Example: You have 1.0.0
on master
and want to create a 1.0.1
hotfix version. Your develop is already at 1.1.0-SNAPSHOT
.
示例:您已经1.0.0
打开master
并想要创建一个1.0.1
修补程序版本。您的开发已在1.1.0-SNAPSHOT
。
git checkout master
git checkout -b hotfix/1.0.1
- Make your hotfix!
mvn versions:set -DnewVersion=1.0.1
git commit -a -m "hotfix release 1.0.1"
git checkout master
git merge hotfix/1.0.1
(easy, because we created the branch offmaster
)git checkout develop
mvn versions:set -DnewVersion=1.0.0
git commit -a -m "workaround to avoid merge conflicts"
git merge hotfix/1.0.1
(will work because of the commit before)mvn versions:set -DnewVersion=1.1.0-SNAPSHOT
git commit -a -m "set version back to 1.1.0-snapshot"
git checkout master
git checkout -b hotfix/1.0.1
- 制作您的修补程序!
mvn versions:set -DnewVersion=1.0.1
git commit -a -m "hotfix release 1.0.1"
git checkout master
git merge hotfix/1.0.1
(很简单,因为我们创建了分支关闭master
)git checkout develop
mvn versions:set -DnewVersion=1.0.0
git commit -a -m "workaround to avoid merge conflicts"
git merge hotfix/1.0.1
(会因为之前的提交而工作)mvn versions:set -DnewVersion=1.1.0-SNAPSHOT
git commit -a -m "set version back to 1.1.0-snapshot"
Not very nice but it works. This solution is also used by jgitflow (a Maven plugin to support you with git flow).
不是很好,但它有效。这个解决方案也被 jgitflow(一个支持你使用 git flow 的 Maven 插件)使用。
A nice alternativeis to not ever commit the version bumps in the pom.xml
and to set it before your build is run on the CI server. Example CI Pipeline:
一个不错的选择是永远不要提交版本颠簸,pom.xml
并在您的构建在 CI 服务器上运行之前设置它。示例 CI 管道:
- Checkout the banch
- Derive release version from the branch name and enrich it with a build number or timestamp, e.g. for build 42 of
release/1.0.1
it would be1.0.1-42
- Set the version using
mvn versions:set -DnewVersion=1.0.1-42
- Build the release and publish it
- 结帐银行
- 从分支名称派生发布版本并用内部版本号或时间戳对其进行丰富,例如,对于
release/1.0.1
它的内部版本 42将是1.0.1-42
- 使用设置版本
mvn versions:set -DnewVersion=1.0.1-42
- 构建发布并发布它
The version number will not be as pure, but you'll never have merge conflicts anymore and you can always track a version back to it's build.
版本号不会那么纯粹,但您永远不会再有合并冲突,并且您始终可以将版本追溯到它的构建。
回答by Aaron Digulla
Keep in mind that Git was developed for the Linux kernel which has it's own version rules.
请记住,Git 是为 Linux 内核开发的,它有自己的版本规则。
For Maven, you should create a release branch which gets the snapshot version of the next release. This change should be a single commit (i.e. just the change of the version number in pom.xml
). When merging that, checkout master
and use git merge --strategy=ours <release-branch>
对于 Maven,您应该创建一个发布分支来获取下一个版本的快照版本。此更改应该是一次提交(即仅更改 中的版本号pom.xml
)。合并时,结帐master
并使用git merge --strategy=ours <release-branch>
--strategy=ours
means: Make a merge by saying "everything in master
has been correctly merged with the release branch"; no changes are being made to master. Afterwards, Git will treat the branches as merged (i.e. having no changes) despite the different version number in both branches.
--strategy=ours
意思是:通过说“所有内容master
都已正确地与发布分支合并”来进行合并;没有对 master 进行任何更改。之后,尽管两个分支的版本号不同,Git 会将分支视为合并的(即没有更改)。
To avoid all kinds of problems when building master
with Maven, use an odd or very high version number which never changes like 99.DEV-SNAPSHOT
.
为避免在master
使用 Maven构建时出现各种问题,请使用从不改变的奇数或非常高的版本号,例如99.DEV-SNAPSHOT
.
When you make the release, strip the -SNAPSHOT
from the version in the release branch and commit. Afterwards, you checkout master and merge once more with --strategy=ours
.
发布时,-SNAPSHOT
从发布分支中的版本中剥离并提交。之后,您结帐 master 并再次与--strategy=ours
.
Note:If you do this, you must not make any other changes on the release branch but changing the versions. Any other hotfixes will be lost! You can only cherry pick them.
注意:如果你这样做,你不能在发布分支上做任何其他更改,而是更改版本。任何其他修补程序都将丢失!你只能挑选它们。
回答by numéro6
You can use jgitflow-maven-plugin: goals jgitflow:release-startand jgitflow:release-finish.
您可以使用jgitflow-maven-plugin:目标jgitflow:release-start和jgitflow:release-finish。
回答by Benoit Courtine
With Maven, you should notchange the version number manually.
使用Maven,你应该不手动更改版本号。
You should add the "scm" information to your pom, in order to let Maven commit and push the version change directly.
您应该将“scm”信息添加到您的 pom 中,以便让 Maven 提交并直接推送版本更改。
Then, use the "release plugin". It will do the work for you. Suppose that your current version is "1.1-SNAPSHOT", the "release:perform" maven task will:
然后,使用“发布插件”。它将为您完成工作。假设您当前的版本是“1.1-SNAPSHOT”,“release:perform”maven 任务将:
- Change the version to 1.1, commit, tag this version and push it.
- Change the version again to 1.2-SNAPSHOT (or 1.1.1-SNAPSHOT, 2.0-SNAPSHOT… you can choose the next version), commit and push it.
- 把版本改成1.1,commit,给这个版本打标签,push。
- 再次将版本改成1.2-SNAPSHOT(或者1.1.1-SNAPSHOT、2.0-SNAPSHOT……可以选择下一个版本),commit并push。
Here is an extract of a git history on a project where the Maven release plugin is used:
以下是使用 Maven 发布插件的项目的 git 历史摘录:
* 2345678 - Normal developpement commit (on branch 1.2-SNAPHOT).
* 5678901 - [maven-release-plugin] prepare for next development iteration
* 8901234 - (tag: 1.1) [maven-release-plugin] prepare release 1.1
* 1234567 - Normal developpement commit (on branch 1.1-SNAPHOT).
Note 1: At the release moment, you have to provision the next version (1.2 in this example). If you change your mind, you can change it later. The Maven "version:set-version" plugin let you reassign the version of all the project hierarchy. You will just have to commit this version change before the next release.
注意 1:在发布时,您必须提供下一个版本(本例中为 1.2)。如果你改变主意,你可以在以后改变它。Maven "version:set-version" 插件让你重新分配所有项目层次结构的版本。您只需在下一个版本之前提交此版本更改。
Note 2: At the release moment, you can also change the release version. Even if the current version is 1.1-SNAPSHOT, you can decide that the release is the 2.0 version and the next development version the 2.1-SNAPSHOT.
注2:在发布时,您还可以更改发布版本。即使当前版本是 1.1-SNAPSHOT,您也可以决定发布的是 2.0 版本,下一个开发版本是 2.1-SNAPSHOT。