Spring ブートを使用するマルチモジュール Maven アプリケーションがあります。
- spring boot parent
- myproject parent (both parent and module pom)
- module1
- module2
- module-it (integration tests)
私の module-it では、依存関係に他のモジュールを追加し、maven-failsafe-plugin を次のように構成します。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Maven でプロジェクトをビルドすると、「Build Success」が表示されます。
mvn clean install
ここまでは順調ですね。
それでも、各モジュールをビルドの最後に実行可能な jar にしたいと考えています。上記の設定では、マニフェストは定義されておらず、jar は実行できません。この問題を解決するために、module1 と module2 の pom ファイルに次のコードを追加しました。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
この設定では、jar ファイルは実行可能ですが、ビルドできなくなります。モジュールで使用するクラスが見つかりません。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project module-it: Compilation failure: Compilation failure:
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol: class GreetingController
[ERROR] location: class com.mycompany.testing.greeting.GreetingControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol: class HelloController
[ERROR] location: class com.mycompany.testing.hello.HelloControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class GreetingController
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class HelloController
私のテストには次の形式があります(HelloControllerIT.javaの同上):
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GreetingController.class)
public class GreetingControllerIT {
@Autowired
private GreetingController greetingController;
@Test
public void getIT_OK() throws Exception {
Assert.assertEquals("My greetings", greetingController.getStr());
}
}
コントローラーは次の形式をとります (getStr() メソッドは、構成をテストするためだけにここにあります)。
@RestController
public class GreetingController {
public String getStr() {
return "My greetings";
}
@RequestMapping(value = "/greeting", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public String get() {
return "greeting";
}
}
spring-boot-maven-plugin でビルドが失敗する理由と、問題を解決する方法を理解していただけますか?
私は知っています:
- 私のテストは今のところ統合テストではありません。これは単なる例です。
- stackoverflow で見つけた回答によると、以下のようなファイルを追加してから @ContextConfiguration を使用できますが、試してみましたが、Maven ビルドの出力は変更されません。
@Configuration
@ComponentScan(basePackages = "com.mycompany.testing")
public class AppConfig {
}
よろしくお願いします。