Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot test multimodule maven application

I have a multi-module maven application which uses Spring boot:

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

In my module-it, I add the other modules in my dependencies and I configure maven-failsafe-plugin as follows:

<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>

When I build my project with maven, I get "Build Success":

mvn clean install

So far so good.
Yet I would like each of my modules to be an executable jar at the end of the build. With the above settings, the manifest is not defined and the jar is not executable. To fix this issue, I've added the following in my module1 and module2 pom files:

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

With this setting, my jar file is executable but I cannot build anymore. Classes that I use in my module-it are not found.

[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

My tests have the following form (idem for 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());
    }
}

And the Controllers have this form (the getStr() method is only here to test the configuration):

@RestController
public class GreetingController {

    public String getStr() {
        return "My greetings";
    }

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public String get() {
        return "greeting";
    }
}

Can you please help me understand why spring-boot-maven-plugin makes my build fail and how I can solve the issue?

I'm aware of:

  • my tests are not integration tests for now. It is just an example ;
  • According to answers I've found on stackoverflow, I could add a file such as the one below and then use @ContextConfiguration but I tried and it does not change the output of Maven build.
@Configuration
@ComponentScan(basePackages = "com.mycompany.testing")
public class AppConfig {
}

Thanks in advance for your help.

like image 859
D3nsk Avatar asked Oct 01 '16 12:10

D3nsk


People also ask

How do I run a multi-module project in Maven?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.


1 Answers

To solve this issue, we can add a classifier as described in the documentation custom repackage classifier

The plugin then becomes:

<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>
like image 154
D3nsk Avatar answered Oct 21 '22 11:10

D3nsk