spring-boot之Spring Boot 测试多模块 Maven 应用程序

虾米姐 阅读:9 2025-01-19 22:14:33 评论:0

我有一个使用 Spring Boot 的多模块 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 构建项目时,我得到“构建成功”:
mvn clean install 

到现在为止还挺好。
然而,我希望我的每个模块在构建结束时都是一个可执行的 jar。使用上述设置, list 未定义且 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()); 
    } 
} 

并且 Controller 具有这种形式(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 { 
    } 
    

    在此先感谢您的帮助。

    请您参考如下方法:

    为了解决这个问题,我们可以添加一个分类器,如文档 custom repackage classifier 中所述。

    然后插件变为:

    <plugin> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-maven-plugin</artifactId> 
        <executions> 
            <execution> 
                <goals> 
                    <goal>repackage</goal> 
                </goals> 
                <configuration> 
                    <classifier>exec</classifier> 
                </configuration> 
            </execution> 
        </executions> 
    </plugin> 
    


    标签:Maven
    声明

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    关注我们

    一个IT知识分享的公众号