【问题标题】:Spring boot test multimodule maven applicationSpring Boot 测试多模块 Maven 应用程序
【发布时间】:2017-02-09 22:08:08
【问题描述】:

我有一个使用 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。使用上述设置,清单未定义且 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());
    }
}

而Controllers有这种形式(这里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 {
}

提前感谢您的帮助。

【问题讨论】:

    标签: spring-boot integration-testing maven-module


    【解决方案1】:

    为了解决这个问题,我们可以添加一个分类器,如文档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>
    

    【讨论】:

      【解决方案2】:

      您也可以将 repackage 目标参数 attach 设置为 false:

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

      【讨论】:

        猜你喜欢
        • 2019-02-06
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多