2016-10-01 74 views
1

我有一个使用了Spring启动一个多模块的Maven的应用程序:春天开机测试多模块的Maven应用

- spring boot parent 
    - myproject parent (both parent and module pom) 
     - module1 
     - module2 
     - module-it (integration tests) 

在我的模块,我加入我的依赖其他的模块和I配置Maven-failsafe-插件如下:

<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不可执行。 要解决这个问题,我已经添加了以下在我的模块1和模块2 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 { 
} 

在此先感谢您的帮助。

回答

4

为了解决这个问题,我们可以在文档中描述的添加分类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>