scm maven
您可以轻松地告诉Maven在JAR清单文件中包含工件的版本及其Git / SVN /…修订版,然后在运行时通过getClass()。getPackage访问该信息。 getImplementationVersion() 。 (所有功劳归功于MarkusKrüger和其他同事。)
在清单中包括Maven工件版本
(注意:如果您还想包含SCM修订版,则实际上将不希望使用它;请参见下文。)
pom.xml:
<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId>...<configuration><archive><manifest><addDefaultImplementationEntries>true</addDefaultImplementationEntries><addDefaultSpecificationEntries>true</addDefaultSpecificationEntries></manifest></archive></configuration>...</plugin></plugins></build>...
</project>
然后,JAR文件的结果MANIFEST.MF将包括以下条目,其值来自指示的属性:
Built-By: ${user.name}
Build-Jdk: ${java.version}
Specification-Title: ${project.name}
Specification-Version: ${project.version}
Specification-Vendor: ${project.organization.name
Implementation-Title: ${project.name}
Implementation-Version: ${project.version}
Implementation-Vendor-Id: ${project.groupId}
Implementation-Vendor: ${project.organization.name}
(规范供应商和实施供应商来自POM的组织/名称。)
包括SCM修订版
为此,您可以使用生成属性$ {buildNumber}的内部版本号Maven插件 ,也可以从Jenkins或Hudson(对于Subversion为SVN_REVISION,对于Git为GIT_COMMIT) 传递的环境变量中检索它。
仅对于git,您还可以使用maven-git-commit-id-plugin ,该插件可以替换现有资源文件中的字符串,例如$ {git.commit.id}(使用maven的资源过滤,必须启用)。实际值或将它们全部输出到git.properties文件中。
让我们使用buildnumber-maven-plugin显式创建清单条目,其中包含内部版本号(即修订版)
<project><build><plugins><plugin><!-- Create the property $buildNumber holding the current Git revision --><groupId>org.codehaus.mojo</groupId><artifactId>buildnumber-maven-plugin</artifactId><version>1.2</version><executions><execution><phase>validate</phase><goals><goal>create</goal></goals></execution></executions><configuration><doCheck>false</doCheck><doUpdate>false</doUpdate></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>2.4</version><configuration><archive><manifestEntries><Implementation-Title>${project.name}</Implementation-Title><!-- buildNumber is produced at runtime by buildnumber-maven-plugin --><Implementation-Version>${project.version} ${buildNumber}</Implementation-Version></manifestEntries></archive></configuration></plugin>
...
访问版本和修订版
如上所述,您可以通过getClass()。getPackage.getImplementationVersion()和getClass()。getPackage.getImplementationTitle()访问代码中的清单条目。
资源资源
- SO:如何在运行时获取Maven Artifact版本?
- Maven存档器文档
翻译自: https://www.javacodegeeks.com/2013/05/accessing-an-artifacts-maven-and-scm-versions-at-runtime.html
scm maven