使用 Maven、Git:如何标记我的代码的最新版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8901575/
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-10 12:45:36  来源:igfitidea点击:

Using Maven, Git: How do I tag the latest version of my code?

gitmaventagging

提问by Dave

I'm using Maven 3.0.3 with Git. I use an integration tool (Bamboo) to check out a branch of code from Git into a directory. The tool then uses Maven run the standard build lifecycle (compile, test, deploy). What I want is that if my Maven deploy task succeeds, I want to tag the version of my code that is checked out in Git. How can I do this from Maven? Any sample configurations you can provide are greatly appreciated.

我在 Git 中使用 Maven 3.0.3。我使用集成工具 (Bamboo) 将代码分支从 Git 检出到目录中。该工具然后使用 Maven 运行标准构建生命周期(编译、测试、部署)。我想要的是,如果我的 Maven 部署任务成功,我想标记我在 Git 中检出的代码版本。我怎样才能从 Maven 做到这一点?非常感谢您可以提供的任何示例配置。

采纳答案by eis

Use maven scm plugin. See tag functionality in advanced features, which should be relevant.

使用maven scm 插件。请参阅高级功能中的标签功能,这应该是相关的。

Now, git support doesn't come out of the box, so you'll need a dependency to maven-scm-provider-gitexe. Also, to overcome plexus exception issue, you'll also need to add a dependency to a later version of plexus.

现在,git 支持不是开箱即用的,因此您需要依赖maven-scm-provider-gitexe。此外,为了克服plexus 异常问题,您还需要向更高版本的 plexus 添加依赖项。

This is what worked for me:

这对我有用:

<project>
    <scm>
      <connection>scm:git:https://[email protected]/my-project.git</connection>
      <developerConnection>scm:git:https://[email protected]/my-project.git</developerConnection>
    </scm>
    <!-- snip -->
    <build>
      <plugins>
       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-scm-plugin</artifactId>
         <dependencies>
            <dependency>
               <groupId>org.codehaus.plexus</groupId>
               <artifactId>plexus-utils</artifactId>
               <version>2.1</version>
            </dependency>
            <dependency>
               <groupId>org.apache.maven.scm</groupId>
               <artifactId>maven-scm-provider-gitexe</artifactId>
               <version>1.2</version>
           </dependency>
         </dependencies>
        <version>1.0</version>
        <configuration>
          <tag>test</tag>
          <connectionType>connection</connectionType>
        </configuration>
        <executions>
          <execution>
          <id>tag</id>
          <phase>deploy</phase>
          <goals>
            <goal>tag</goal>
          </goals>
          </execution>
        </executions>
       </plugin>
     </plugins>
    </build>
    <!-- snip -->
</project>

回答by rayd

The maven-release-plugin can do this for you -- see an example here: http://maven.apache.org/plugins/maven-release-plugin/examples/prepare-release.html

maven-release-plugin 可以为您执行此操作 - 请参阅此处的示例:http: //maven.apache.org/plugins/maven-release-plugin/examples/prepare-release.html

回答by Edoardo

maven-release-plugin needs only to declare the scm:

maven-release-plugin 只需要声明 scm:

<scm>
    <url>https://github.com/username/repoName</url>
    <connection>scm:git:git://github.com/username/repoName.git</connection>
    <developerConnection>scm:git:[email protected]:username/repoName.git</developerConnection>
    <tag>HEAD</tag>
</scm>

generate git ssh keys

生成 git ssh 密钥

https://help.github.com/articles/generating-ssh-keys/

https://help.github.com/articles/generate-ssh-keys/

and run mvn release:prepare

并运行mvn release:prepare

more from https://github.com/kevinsawicki/github-maven-example

更多来自https://github.com/kevinsawicki/github-maven-example

回答by Marcin Górski

I recommend the small open source project I'm part of -it's called Quicktag and works with couple of VCSes - https://code.google.com/p/quicktag-maven-plugin. Add the plugin and it will generate Java class with static fields that contain build information.

我推荐我参与的小型开源项目 - 它称为 Quicktag 并与几个 VCS 一起使用 - https://code.google.com/p/quicktag-maven-plugin。添加插件,它将生成带有包含构建信息的静态字段的 Java 类。