Mavenを使用したJavaでのGitリビジョンロギング

私たちの会社はMercurialからGitに切り替えました。その後、以前にログに展開ブランチに関する情報を表示し、gitでそれを書き換える方法を理解する必要がありました。 Gitが人気を集めており、多くの企業がGitに移行しているため、おそらく将来誰かが同じ問題に直面するでしょう。



私の目標は、いくつかのMavenプラグインを使用して、Gitからブランチの名前とコミットのハッシュをJavaプログラムのログに出力する方法を示すことです。 これは、長期間展開しておらず、CIツールの履歴が消去されている場合に、ログを分析するときに役立ちます。



開始する



この例では、java、maven、spring-core、gitを使用します。 リンク。 既にJavaで記述されたプロジェクトがあり、Mavenを使用してビルドするとします 。これはGitHubにも保存されます



画像








Maven SCM



Mavenがリポジトリにアクセスするには、メソッドと接続パスを通知する必要があります。 これを行うには、 maven scmを使用します。



... </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> ...
      
      





アクセスのリンクは、プロジェクトのGit Webページの[クイックセットアップ]セクションに形成されます。 リポジトリへのアクセスに関するscmとオプションの使用については、 こちらこちらをご覧ください



現在のgitブランチに関する情報を取得する



次に、mavenプラグインbuildnumber-maven-pluginを追加する必要があります 。これはbuildNumber(コミットのハッシュ)およびscmBranch(ブランチ名)を形成します。



 <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>
      
      





buildNumberとscmBranchは、Mavenでクリーンアップした後に発生する検証段階で生成されます。



パラメータ:





これに関する詳細情報。



プロパティを操作する



その後、 application.propertiesファイルに次の変数を安全に追加できます:$ {buildNumber}および$ {scmBranch}



たとえば、次のパラメーターをapplication.propertiesに追加しました。



 branch.name=${scmBranch} commit.hash=${buildNumber} application.version=${project.version}
      
      





application.propertiesを見つけるには、 buildタグに次を追加する必要があります。

 <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources>
      
      







プロジェクトの組み立て



チームでプロジェクトを組み立てましょう:



 mvn clean install -DskipTests
      
      





ビルド中にmavenが表示したログを見ると、gitコマンドを実行して必要なデータを取得していることがわかります。



 [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
      
      





application.propertiesファイルのtarget / classesフォルダーで受信したデータの正確性を確認することもできます。



画像






内容:



 branch.name=master commit.hash=47c0d1df153b8610392d51d1a7fa0b7b39716e09 application.version=0.0.1-SNAPSHOT
      
      







ログに追加する



springが動作するクラスのプロパティを取得するには、 @ Valueアノテーションを使用します。



例:



 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); } }
      
      





mainメソッドを持つクラスの例:



 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
      
      





ハッシュ削減



コミットのハッシュが長すぎて切り捨てられないため、 buildnumber-maven-pluginで出力する最大文字数を指定する必要があります。



  <configuration> <shortRevisionLength>5</shortRevisionLength> </configuration>
      
      





MANIFEST.MFデータでGitを拡張する



このデータをMANIFEST.MFに追加することができます。 これを行うには、別のmavenプラグインmaven-jar-pluginを接続する必要があります



 <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.MFで確認できます。



 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
      
      





ありがとう この記事がお役に立てば幸いです。



All Articles