Skip to content

Instantly share code, notes, and snippets.

@seraphy
Created March 27, 2020 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraphy/3567428481e69afa1d2d941376aff54a to your computer and use it in GitHub Desktop.
Save seraphy/3567428481e69afa1d2d941376aff54a to your computer and use it in GitHub Desktop.
SpringBootでpackage作成時の、uber-jarと、raw-jarの二つの作成方法についてメモ

概要

SpringBootでpackageを作成すると、依存関係のjarを内部に取り込んだuber-jar形式の実行可能jarになり、且つ、この依存jarを内部に含むjarを成果物する。

これはこれで便利なのだが、標準では、Mavenのローカルリポジトリに登録されるのも、このuber-jarになる。

これは、他のプロジェクトから参照する場合に問題になる。

※ uber-jarを参照しても、uber-jarはネストしたjar構造になっているため、通常のjavaコードから直接参照するのは難しい。(クラスパスにuber-jarを指定してもクラスは発見できない。)

他のプロジェクトから参照したい場合など、依存jarをまとめていない素のjar(オリジナルのjar)のままにローカルリポジトリに登録したい場合は、以下のようにする。

pom.xmlのrepackageを修正すればuber-jarの生成を制御できる

attachfalseにすることで、uber-jarでないオリジナルのjarをローカルリポジトリに登録できるようにする。

classifierをつけることで、生成されるuber-jarの末尾に識別子をつけることができる。

これにより、Mavenのローカルリポジトリには素のjarが登録され、且つ、uber-jarも生成されるようになる。

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<executions>
		<execution>
			<id>repackage</id>
			<goals>
				<goal>repackage</goal>
			</goals>
			<configuration>
				<!-- maven repositoryにはuber-jarではなく、依存jarを含まないoriginalを登録する -->
				<attach>false</attach>
				<!-- 実行可能なuber-jarには分類子(-executable.jar)をつけておく -->
				<classifier>executable</classifier>
			</configuration>
		</execution>
	</executions>
</plugin>

Uber-jarもローカルリポジトリに登録したい場合

生成した素のjarだけでなく、生成されたuber-jarもローカルリポジトリに登録したい場合は、明示的にinstallを実行すれば良い。

以下の場合、もとのアーティファクトIDに-executableという末尾名をつけたもので登録する。

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-install-plugin</artifactId>
	<executions>
		<!-- installターゲット時、実行可能なuber-jarもリポジトリに登録する -->
		<execution>
			<id>install-executable</id>
			<phase>install</phase>
			<configuration>
				<file>${basedir}/target/${project.build.finalName}-executable.jar</file>
				<repositoryLayout>default</repositoryLayout>
				<groupId>${project.groupId}</groupId>
				<artifactId>${project.artifactId}-executable</artifactId>
				<version>${project.version}</version>
				<packaging>jar</packaging>
				<generatePom>true</generatePom>
			</configuration>
			<goals>
				<goal>install-file</goal>
			</goals>
		</execution>
	</executions>
</plugin>

以上おわり

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment