みなさん、こんな経験ありませんか?
-
レガシー案件(JDK7 + Spring3系)と新規案件(JDK11 + Spring Boot 2/3系)を同時に触る必要がある
-
Maven Project Update をしたら JRE System Library が勝手に変わって壊れた
-
.m2/repository がカオス化して「どのライブラリが正しいのか分からない」状態になった
この記事では、そんなモヤモヤを解決するための .m2 リポジトリを複数作成して用途に応じて切り替える方法 を紹介します。特に Eclipse ユーザーに刺さる内容です。🔥
基本戦略
-
.m2/settings.xml で localRepository を用途ごとに切り替える
-
profile を設定して、JDK7 / JDK11 のビルド環境を明示する
-
Eclipse のワークスペースごとに settings.xml を固定して「切り替え忘れ」を防止する
設定例
1. JDK7 用 settings-jdk7.xml
<settings>
<localRepository>/home/user/.m2/repo-jdk7</localRepository>
<profiles>
<profile>
<id>jdk7-profile</id>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<java.version>1.7</java.version>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>jdk7-profile</activeProfile>
</activeProfiles>
</settings>
2. JDK11 用 settings-jdk11.xml
<settings>
<localRepository>/home/user/.m2/repo-jdk11</localRepository>
<profiles>
<profile>
<id>jdk11-profile</id>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>jdk11-profile</activeProfile>
</activeProfiles>
</settings>
Eclipse 側の設定
Eclipse (JDK7用)
1.Window > Preferences > Maven > User Settings
2.settings-jdk7.xml を指定
3.ワークスペース = workspace-jdk7
Eclipse (JDK11用)
1.Window > Preferences > Maven > User Settings
2.settings-jdk11.xml を指定
3.ワークスペース = workspace-jdk11
👉 こうしておけば、ワークスペースを開いた瞬間に 正しい JDK とリポジトリが自動適用 されます。
activeByDefault の罠
pom.xml に profile があって、こんなふうに書いていたら要注意です。
<profiles>
<profile>
<id>default-profile</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<java.version>1.8</java.version>
</properties>
</profile>
</profiles>
実はこれ、settings.xml で activeProfile を指定していないときだけ有効化されるんです。
つまり、settings.xml で jdk11-profile を active にしていれば、activeByDefault は無視される
逆に settings が何も指定してなければ、default-profile が有効になる
👉 「思ってた profile と違う!」という事故を防ぐには、settings.xml 側で明示的に指定するのがおすすめです。
メリット / デメリットまとめ
メリット
案件ごとにライブラリや JDK を完全分離できる
Eclipse のワークスペースごとに設定を固定できるので切り替え忘れなし
古い案件(JDK7)と新規案件(JDK11)を安全に並行開発できる
デメリット
同じライブラリを両方のリポジトリに持つのでディスク容量は増える
settings.xml が複数になるので管理ルールが必要
まとめ
JDK7/JDK11 を並行で触るなら、.m2/settings.xml を分けるのが最適解
localRepository + profile をセットで管理すると、Maven Project Update で正しい JRE System Library が反映される
Eclipse × 複数ワークスペース運用と組み合わせると「切り替え忘れ事故ゼロ」にできる
レガシーとモダンの両方を抱える現場では、地味ですがかなり効きます。ぜひ導入してみてください 🙌