LoginSignup
2
0

mavenプロジェクトではない既存プロジェクトを壊さないで、pomで管理できるようにする

Last updated at Posted at 2023-12-20

この記事は「ネオシステム Advent Calendar 2023」の21日目の記事です。

背景

Jacocoを利用してカバレッジそ取得しようとしたのですが、既存の設定や構成等を壊したくなかったので外部からpomで管理できるようにしました。

環境

Java11
Junit4
Maven3
Eclipse

github

sample_common
sample_main
sample_maven_project

必要な知識

git submodule

概要

  • 今回はテストmavenプロジェクトとしてjunitテストを行う
  1. git submoduleで依存プロジェクトも含めたプロジェクトを取得する
  2. 上記で取得したプロジェクト全体をpom.xmlで管理する

プロジェクト構成

  • maven管理したいプロジェクト
* メインのプロジェクト
 
sample_common
┣ .git
┗ src
  ┗ main
    ┗ java


        
* Common系のプロジェクト

sample_main   // sample_commonに依存している
┣ .git
┗ src
  ┣ main
  ┃ ┣ java
  ┃ ┗ resources  
  ┃    ┣ hoge-en.properties
  ┃    ┗ hoge-jp.properties
  ┗ test
      ┗ java

  • 上記をmavenで管理するプロジェクト

sample_maven_project
┣ .git
┣ src
┣ sample_main // submoduleで取得したプロジェクト
┣ sample_common // submoduleで取得したプロジェクト
┗ pom.xml

sample_common

  • プロパティファイルを読む機能を実装してます
public class PropertyUtil {

	private Properties prop;

	public PropertyUtil(String path) {
		ResourceBundle rb = ResourceBundle.getBundle(path);
		Enumeration<String> e = rb.getKeys();

		prop = new Properties();
		while (e.hasMoreElements()) {
			String key = e.nextElement();
			prop.put(key, rb.getString(key));
		}
	}

	public String getValue(String key) {
		return prop.getProperty(key);
	}

sample_main

  • プロパティファイルの名前を渡して、値をenumで取得する機能を用意します
  • hoge-環境変数というファイルを指定します
public enum PropKey {
	KEY1,
	KEY2,
	KEY3;

	private final String key;

	private static final String envName = System.getenv("SAMPLE_ENV_NAME");
	private static final PropertyUtil PROP = new PropertyUtil("hoge-"+envName);

	PropKey(){
		this.key=name();
	}

	PropKey(String key){
		this.key=key;
	}

	@Override
	public String toString() {
		return PROP.getValue(key);
	}

	public String getValue() {
		return PROP.getValue(key);
	}

}

テストクラス

  • プロパティファイルで取得した内容のテストです
public class LogicTest {

	@Test
	public void test1() {

		assertThat("ねこ", is(PropKey.KEY1.getValue()));
	}

	@Test
	public void test2() {

		assertThat("いぬ", is(PropKey.KEY2.getValue()));
	}

	@Test
	public void test3() {

		assertThat("うさぎ", is(PropKey.KEY3.getValue()));
	}
}

sample_maven_project

pom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>sample_maven_project</groupId>
	<artifactId>sample_maven_project</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>sample_maven_project</name>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<build>
		<!-- 試験の対象の指定 -->
		<sourceDirectory>${project.basedir}/sample_main/src/main/java</sourceDirectory>
		<!-- 試験の指定 -->
		<testSourceDirectory>${project.basedir}/sample_main/src/test/java</testSourceDirectory>


		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<executions>
					<execution>
						<id>add-source</id>
						<phase>generate-sources</phase>
						<goals>
							<goal>add-source</goal>
						</goals>
						<configuration>
							<sources>
								<source>${project.basedir}/sample_main/src/main/java</source>
								<source>${project.basedir}/sample_main/src/test/java</source>
								<source>${project.basedir}/sample_common/src/main/java</source>
							</sources>
						</configuration>
					</execution>
					<execution>
						<id>add-test-resource</id>
						<phase>generate-test-resources</phase>
						<goals>
							<goal>add-test-resource</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<resources>
						<resource>
    						<directory>${project.basedir}/sample_main/src/main/resources</directory>
						</resource>
					</resources>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.1</version>
				<configuration>
					<userSystemClassLoader>true</userSystemClassLoader>
					<environmentVariables>
						<SAMPLE_ENV_NAME>jp</SAMPLE_ENV_NAME>
					</environmentVariables>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<configuration>
					<target>
						<echo message="#### ${project.basedir} #### " />
					</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
	</dependencies>
</project>

sourceDirectory

  • テスト対象のパスを指定する

testSourceDirectory

  • 実際に動かす試験のパスを指定する

build-helper-maven-plugin

  • プロジェクトのビルド時に関連する操作を行えるプラグイン
add-source
  • src/main/javasrc/test/java以外にあるソースもソースコードとして認識できるようにする
    • テストを実行するために必要なソースのパスを指定
add-test-resource
  • src/main/resourcessrc/test/resources以外にあるリソースも認識できるようにする
    • テストの実行に必要なプロパティファイルなどパスを指定

タグの構成がおかしいがいくつかの環境で試してもこれでしか動きませんでした。原因不明。
これで動かない場合は公式の例を参考に正しく記載してください。

maven-surefire-plugin

  • テストを実行するためのプラグイン

ここではSAMPLE_ENV_NAMEという環境変数にjpという値をしています。 今回は記載していませんが、テスト対象のクラスの指定も可能です。 testSourceDirectory`はクラスまで指定できません。

maven-antrun-plugin

  • Antタスクを実行するためのプラグイン

今回は必要ないです。コメント出力するためだけに追加してます。

dependency

  • 依存関係を追加
    今回はjunitを追加しています。
    Mavenプロジェクトではないプロジェクトで外部jarに依存してる場合は、jarのパスをここに記載してください。

ゴール

clean build-helper:add-source build-helper:add-resource antrun:run test surefire-report:report
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0