... </dependencies> <scm> <connection>scm:git:https://github.com/<your_username>/<your_projectname>.git</connection> <developerConnection>scm:git:https://github.com/<your_username>/<your_projectname>.git</developerConnection> <tag>HEAD</tag> <url>https://github.com/<your_username>/<your_projectname>.git</url> </scm> <build> ...
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>buildnumber-maven-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>validate</phase> <goals> <goal>create</goal> </goals> </execution> </executions> <configuration> <doCheck>false</doCheck> <doUpdate>false</doUpdate> </configuration> </plugin>
branch.name=${scmBranch} commit.hash=${buildNumber} application.version=${project.version}
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
mvn clean install -DskipTests
[INFO] --- buildnumber-maven-plugin:1.4:create (default) @ GitRevision --- [INFO] Executing: /bin/sh -c cd '/home/<user>/Development/GitRevision' && 'git' 'rev-parse' '--verify' 'HEAD' [INFO] Working directory: /home/<user>/Development/GitRevision [INFO] Storing buildNumber: 47c0d1df153b8610392d51d1a7fa0b7b39716e09 at timestamp: 1474375934082 [INFO] Storing buildScmBranch: master
branch.name=master commit.hash=47c0d1df153b8610392d51d1a7fa0b7b39716e09 application.version=0.0.1-SNAPSHOT
public class LoggerExampleImpl implements LoggerExample { private static final Logger log = LoggerFactory.getLogger(LoggerExampleImpl.class); @Value("${branch.name}") private String branchName; @Value("${commit.hash}") private String commitHash; @Value("${application.version}") private String version; public void printLog() { log.info("Project version: {}, git branch: {}, commit hash: {}", version, branchName, commitHash); } }
public class GitRevisionApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); LoggerExample loggerExample = context.getBean(LoggerExampleImpl.class); loggerExample.printLog(); } }
2016-09-20 17:06:07 INFO [main] ClassPathXmlApplicationContext:581 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@66cd51c3: startup date [Tue Sep 20 17:06:07 EEST 2016]; root of context hierarchy 2016-09-20 17:06:07 INFO [main] XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [spring.xml] 2016-09-20 17:06:07 INFO [main] PropertyPlaceholderConfigurer:172 - Loading properties file from URL [file:/home/rado/Development/GitRevision/target/classes/application.properties] 2016-09-20 17:06:08 INFO [main] LoggerExampleImpl:25 - Project version: 0.0.1-SNAPSHOT, git branch: master, commit hash: 52c05227fb27271314d80d39b5026193ff310f04
<configuration> <shortRevisionLength>5</shortRevisionLength> </configuration>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> <manifestEntries> <Implementation-Version>${project.version}-${buildNumber}</Implementation-Version> <Implementation-Build>${scmBranch}</Implementation-Build> <Main-Class>com.habrahabr.example.GitRevisionApplication</Main-Class> </manifestEntries> </archive> </configuration> </plugin>
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: rado Build-Jdk: 1.8.0_102 Specification-Title: GitRevision Specification-Version: 0.0.1-SNAPSHOT Implementation-Title: GitRevision Implementation-Vendor-Id: com.habrahabr.example Implementation-Build: master Implementation-Version: 0.0.1-SNAPSHOT-47c0d1df153b8610392d51d1a7fa0b7 b39716e09 Main-Class: com.habrahabr.example.GitRevisionApplication
Source: https://habr.com/ru/post/310738/
All Articles