4
votes

I have a multi module maven spring-booot maven project. One of the child modules is a stub(a spring boot app) that I want to start and then stop during integration tests of myRealApp.

Parent Pom
|   
|----myRealApp module(spring boot app)
|----stub-of-some-remote-rest-api module(This is also a spring-boot app)

This is how the pom file looks like in the module myRealApp. Which also has all the integration tests. Its trying to start the stub module during before integration-tests phase. But I get the error when I run maven goal in the child module directory:

 mvn integration-tests -X

Error: Could not find or load main class io.swagger.MyStub

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run (start-boot) on project: Could not exec java

I can see in debug mode that that the working directory is set correctly.

Pom of myRealApp:

         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${spring.boot.mainclass}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <configuration>
                        <workingDirectory>${project.parent.basedir}/module-of-my-stub/src/main/java</workingDirectory>
                        <mainClass>io.swagger.MyStub</mainClass>
                    </configuration>
                    <id>start-boot</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-boot</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

Maybe I am not allowed to set workingDirectory in individual executions, or give reference to an other module?

The stub is there acting like a remote rest service which my application is depending on, which I used during development since the actual remote service's test environment was not reliable and was down half of the time. So decided to also use it in Integration tests too.

2
Why have you configured spring-boot-maven-plugin with different executions? If you like to write integration tests it's easy to do ...Furthermore why are you using such a strange directory /my-stub/src/main/java ? Please follow conventions for Maven and the conventions for Spring Boot....Furthermore you should read the guides about testing spring boot apps cause there is no need to write a stub for spring boot apps...khmarbaise
@khmarbaise The stub is there acting like a remote rest service which my application is depending on, which I used during development since the actual remote service's test environment was not reliable and was down half the time. So why not using it also for integration tests? Why it is a strange directory? I am trying to find the main class of the stub module from my application. I'm afraid you didn't understand my question.Spring
@khmarbaise I updated the questionSpring
Ideally there would be a way to add the module-of-my-stub dependency to the classpath the spring-boot-maven-plugin:run goal uses. It doesn't look like there is a direct way, though there is a useTestClasspath option. This link also discusses an alternative approach.df778899
Other way round, but it is speculation. The thinking is that by adding a test dependency of module-of-my-stub to myRealApp, and then adding useTestClasspath to the existing spring-boot-maven-plugin:run configuration, this would make MyStub visible to it on the classpath. Otherwise, the classesDirectory trick in the link above should be able to point it to the target/classes directory under module-of-my-stub.df778899

2 Answers

1
votes

To consolidate some of the comments against the question, these basically followed the advice in this other answer.

The key was setting the classesDirectory against the spring-boot-maven-plugin:run goal to point to the target\classes directory under the other project.

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>${spring.boot.mainclass}</mainClass>
        </configuration>
        <executions>
            <execution>
                <id>start-stub</id>
                <configuration>
                    <arguments>
                        <argument>--server.port=8090</argument>
                    </arguments>
                    <mainClass>io.swagger.Stub</mainClass>
                    <classesDirectory>../my-stub/target/classes</classesDirectory>
                </configuration>
                <goals>
                    <goal>start</goal>
                </goals>
                <phase>pre-integration-test</phase>
            </execution>

            <execution>
                <goals>
                    <goal>build-info</goal>
                </goals>
            </execution>
        </executions>
  </plugin>
0
votes

spring-boot:run only works on the project that packages the springboot application. Best option to make this work: you'll need to use the exec-maven-plugin:exec, ensure that async is set to true. Look at https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html to see how you should configure the plugin.