Deriving maven artifact version from git branch

2019-01-20 23:30发布

We have a workflow requirement that essentially mean that we need to have the artifact version of a module externally defined from the current branch in git.

I.e. if we are on the master branch in git, I need <version>master-...</version> and if we are on the bugfixX branch, I need <version>bugfixX-....</version> for the generated artifact for this pom.xml.

I have previously found that https://github.com/koraktor/mavanagaiata can provide the SHA-1 hash as a property and it appears from the documentation that it can also provide the branch, so perhaps if it could be run early enough in the process we could set the property and just put <version>${our.version}</version> in the pom. If this is possible, I would very much like to see a working pom.xml (and reward a 500 point bounty for it too).

If not, I guess we are into either preprocessing or "git checkout" do extra magic with some of the hooks (which I have not yet tried, working code would be great too).

We have a top level pom, which can be run to generate a property file in ".." before building the modules where this functionality I'm asking about needs to go.

Any suggestions on how to solve this?

7条回答
做自己的国王
2楼-- · 2019-01-21 00:31

If it is sufficient to set the git tag and version information in the artifact file name, you can use maven-jgit-buildnumber-plugin:

<build>
  <finalName>${artifactId}-${git.buildnumber}</finalName>
  <plugins>
    <plugin>
      <groupId>ru.concerteza.buildnumber</groupId>
      <artifactId>maven-jgit-buildnumber-plugin</artifactId>
      <version>1.2.7</version>
      <executions>
        <execution>
          <id>git-buildnumber</id>
          <goals>
            <goal>extract-buildnumber</goal>
          </goals>
          <phase>prepare-package</phase>
        </execution>
      </executions>
    </plugin>
    <!-- more plugins -->
  </plugins>
</build>
查看更多
登录 后发表回答