question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add "start-stop" goals for the spring-boot-maven-plugin

See original GitHub issue

In order to run integration tests for a spring boot app, you can use the workaround described in #667.

There is a more detailed article here.

But in some cases, this approach can’t be used. One example would be running Cucumber tests.

I have two use cases: The first one uses maven. I want to start my spring-boot app, run some Cucumber tests, then stop the spring-boot app, everything within Maven.

Another use case would be: Having the spring-boot running in my IDE and then run the Cucumber tests also from the IDE, by connecting to the already running app.

I wouldn’t like to have the deployment phase hardcoded into the test class as in the example article. This is because (as described in the second use case) if I already have an instance of the application running, I should be able to run my cucumber test directly from my IDE, without starting another instance. I’d like to decouple the deployment step and testing step, as they don’t really belong together.

Another reason for having the start-stop approach in the maven plugin is that the method of running the tests described in the article doesn’t help me, because Cucumber has to be run using a @RunWith(@Cucumber.class) junit annotation, and the spring boot need the @RunWith(SpringJUnit4ClassRunner.class) annotation. And we can’t use two @RunWith annotations on one test class.

I managed to start my spring boot app using maven, like this:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>boot.Main</mainClass>
            <layout>ZIP</layout>
        </configuration>
        <executions>
            <execution>
                <id>boot-run</id>
                <goals>
                    <goal>run</goal>
                </goals>
                <phase>pre-integration-test</phase>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </execution>
        </executions>
    </plugin>

But after the boot-app is started, the maven build is blocked… As soon as you press Ctrl+C, to stop the tomcat, the build resumes.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:32 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
snicollcommented, Mar 13, 2015

okay so those who are interested to try this out are welcome to use a local build of my gh-2525 branch branch. This should work out-of-the-box as long as forking is not enabled (more on that later). For instance, the following configuration should run your integration tests

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

We are using a property to trigger an auto-configuration that registers a MBean who serves two purposes:

  1. Tell us if the application is ready (so that your integration tests really start when they have to). This is using the new ApplicationReadyEvent (see gh-2638)
  2. Stop the application gracefully (basically closing the ApplicationContext).

So what’s the problem with fork? The short answer is that I don’t know. Since we are using JMX, we can also start the process with the necessary system properties to expose the MbeanServer on a given port and connect to that port to invoke the exact same operations. But for some reasons it does not work while the same code outside of the maven plugin works. I am sure I am missing something.

If you want to try it out (code unpolished), check the gh-2525-remote.

0reactions
gauravbrillscommented, Jun 28, 2015

@snicoll it worked thanks … using the BOM was a silly error from my side cheers .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot Maven Plugin Documentation
Stop an application that has been started by the "start" goal. Typically invoked once a test suite has completed. 5. Packaging Executable ...
Read more >
spring boot maven plugin stop goal - Stack Overflow
Spring boot maven plugin stop goal fails to stop the application and leaves the application process hanging (and I and cannot start another ......
Read more >
Use of Spring Boot Maven Plugin with Example - Java Guides
The Spring Boot Maven Plugin provides Spring Boot support in Apache Maven, letting you package executable jar or war archives and run an...
Read more >
Difference Between spring-boot:repackage and Maven package
To start our Spring Boot application using the simple java -jar command, we need to build a fat JAR. The Spring Boot Maven...
Read more >
Deploying into Spring Boot Red Hat Fuse 7.6
After you follow the Section 2.2, “Generating your booster project” steps for the Externalized Configuration mission, follow these steps to build and run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found