私たちの会社は
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で
クリーンアップした後に発生する
検証段階で生成されます。
パラメータ:
- doCheck-trueの場合、ローカルに変更がある限りプロジェクトはコンパイルされません
- doUpdate-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
ありがとう この記事がお役に立てば幸いです。