Command line mvn parameter to stop spring-boot-maven-plugin from executing run - maven

I'm developing a Spring Boot application and as part of the Integration Test phase of my maven project, I have configured the spring-boot maven plugin to start up and shut down during the pre and post integration test parts of the build as follows:
<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>
</plugin>
However, I did this just so that my developers can run their tests against a local instance of the service. On Jenkins, I would run the tests against an external service where I had deployed the service and I do not need the Spring Boot application to be spun up within the Jenkins job.
Is there a way for me to explicitly stop the spring-boot maven plugin from starting up the service via a mvn command line override?

Sure, you can expose a property for it, something like
<properties>
<skip.it>false</skip.it>
</properties>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<skip>${skip.it}</skip>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<skip>${skip.it}</skip>
</configuration>
</execution>
</executions>
</plugin>
Once you have that, run your command as follows: mvn verify -Dskip.it=true and the Spring Boot application will not be started as part of your integration tests.

Related

Spring boot layer jar and build pack

I just started using spring boot 2.3 with layer jar and build pack feature.
Docker image is always built when
mvn clean install/package
code is committed and requested PR in git
However, this will slow down build process, how can I control the phase in which the image is being built and how can I control if image should be built at all?
Following is configuration that added to pom file
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
<image>
<name>${image.name}</name>
<env>
<BP_JVM_VERSION>${BP_JVM_VERSION}</BP_JVM_VERSION>
</env>
</image>
</configuration>
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
The build-image goal is attached to the package phase by default. It is run each time the package goal is run because of the executions configuration you have in your pom.xml:
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
If you remove this <executions> block, build-image will not be run automatically, but can be run manually with mvn spring-boot:build-image.
Alternatively, you can attach the goal to a different phase like install by specifying the phase in the <execution> block like this:
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
You can use a spring-boot.build-image.skip property
Add it to the propertied with true value
<properties>
<spring-boot.build-image.skip>true</spring-boot.build-image.skip>
</properties>
so the build-image goal will be skipped by default. Whenever you want to build the image pass false to the cmd
mvn clean install -Dspring-boot.build-image.skip=false
Update:
If you want to change the phase from install to package, you need to configure the plugin as following:
<executions>
<execution>
<id>default</id>
<phase>none</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
<execution>
<id>build-image-during-package</id>
<phase>package</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>

Maven plugin crossed execution order

I'm writing some integration tests using failsafe plugin.
I want to execute the following:
1) Start Docker (goal start in phase pre-integration-test)
2) Start Spring (goal start in phase pre-integration-test)
3) Tests (phase integration-test)
4) Stop Spring (goal stop in post-integration-test)
5) Stop Docker (goal stop in post-integration-test)
Why? I want to start Docker BEFORE Spring so all databases are ready when Spring boots and also I want to stop Docker AFTER Spring to avoid a lot of errors in Spring due to database connection losses.
I have the following pom.xml:
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.1</version>
<executions>
<execution>
<id>run-docker-containers</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>stop-docker-containers</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
...
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
But with this pom.xml I'm getting this order:
1) Start Docker
2) Start Spring
3) Tests
4) Stop Docker
5) Stop Spring
This is, Docker is stopped BEFORE Spring and I don't want that.
I know that the execution order in Maven is given by the order in pom.xml, but in this case I need to have crossed goals.
Any advice?
Thanks.
Execution order in Maven is based on two components:
First, the lifecycle phase to which the execution is bound
For executions bound to the same phase, the ordering in the POM takes precedence.
Maven doesn't allow the same plugin to be configured twice, so it's not possible to move the Docker Stop execution below Spring. The only way I know to fix things like this is to introduce a third phase into the mix. It looks a bit weird to bind 'stop' goals to the verify phase, and I'd definitely add comments to explain why this was done for posterity - but it works.
This plugin config does the following (assuming you use the maven-failsafe-plugin for integration tests; if you use a different plugin follow the same principles with that plugin). Maven lifecycle phase in parentheses:
1) Docker Start (pre-integration-test)
2) Spring Start (pre-integration-test; Spring plugin config after Docker)
3) Execute tests (integration-test)
4) Spring Stop (post-integration-test)
5) Docker Stop (verify)
6) Verify test results (verify; Failsafe plugin config after Docker)
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.1</version>
<executions>
<execution>
<id>run-docker-containers</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-docker-containers</id>
<phase>verify</phase> <!-- important: note phase -->
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${version.maven.failsafe.plugin}</version>
<executions>
<execution>
<id>failsafe-integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>failsafe-verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

maven tomcat7:run terminates automaticatlly in eclipse

Guys I know its very easy to run tomcat7 plugin in maven(in eclipse) but as I'm new to maven structure I can't figure it out that I was running maven build with "tomcat:run" and was working but I switched to maven plugin tomcat7 configured in pom.xml It starts it and stopped and gives build success msg. how change i make it keep listening?
Here my tomcate plugin settings
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<addContextWarDependencies>true</addContextWarDependencies>
<fork>true</fork>
<path>/</path>
<port>8080</port>
<httpsPort>8443</httpsPort>
<keystoreFile>C:/Users/Sohail Haider/.keystore</keystoreFile>
<keystorePass>apexsohail</keystorePass>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
Take into account the default lifecycle in maven
So, what you are doing in the configuration is starting and stopping tomcat when you are building the project. I think that you have a webapp project and want to run tomcat so you have to remove executions tag from the configuration and just execute mvn tomcat7:run

How to run junit tests when jetty is configured to start/end at pre/post-integration-test?

I want to run junit tests, while jetty is running , how to correctly configure this?
Now it seems that the tests run before jetty is started so they can't get a connection and the project build fails.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.10.v20130312</version>
<configuration>
<connectors>
<connector implementation="org.eclipse.jetty.server.bio.SocketConnector">
<port>9090</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<jvmArgs>-Xms1024m -Xmx2048m</jvmArgs>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.16</version>
</dependency>
</dependencies>
</plugin>
Please help me with this! How to ensure that the junit tests (during the maven build) run in the integration-phase?
The Maven test phase is for running 'Unit' tests, that is, small tests that check that the smallest possible functionality or 'Unit' is functioning as expected.
These are run by the Maven Surefire plugin. You can find more info on this here:
http://maven.apache.org/surefire/maven-surefire-plugin/
What you have are integration tests. That is, they are broader tests that check that different parts of your code integrates together. This is run by the Maven Failsafe plugin which you can find more details on here:
http://maven.apache.org/surefire/maven-failsafe-plugin/
What you need to do is identify your integration tests as integration tests to the Failsafe plugin. By default, it picks up classes named **/IT*.java, **/*IT.java, and **/*ITCase.java. The naming conventions between the plugins is mutually exclusive by default so your tests should only run in one or the other.
If you only have integration tests then you can configure Surefire to run during the integration-test phase instead. Instructions on doing that here:
http://docs.codehaus.org/pages/viewpage.action?pageId=62120

JMS integration tests with Maven and Glassfish

I'm developing an app requiring MDB, running over Glassfish 3.1. I've managed already to run unit/integration tests of simple EJBs using embedded container with no problem. Now I'm trying to create integration tests for my MDBs.
1) I tried launching the Glassfish embedded server programatically, but it does not support creation of JMS queues.
2) I run a Glassfish server from the Maven plugin.
Now I can create queues, and deploy my MDBs, no problem at all. Now, I just can't figure out a way of running JUnit.
- When I create an InitialContext, it times-out when accessing the local server. I have no ways of accessing my beans.
I found a workaround, but it's not serving my needs perfectly:
In my test sources, I created a simple Singleton #Startup bean. In the #PostConstruct method, I call the unit test classes I want to achieve. In order for this bean to be deployed, I have a special special maven build rule that packages some of my tests files in the EJB jar. Deploying this special jar results in my tests being launch. To make it clear, here's an extract of my Maven file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
<resources>
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
<resource>
<directory>${project.build.directory}/test-classes</directory>
<includes>
<include>**/*TestTrigger.class</include>
<include>**/*IntegrationTest.class</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>ejb</goal>
</goals>
<configuration>
<classifier>TEST</classifier>
<outputDirectory>${project.build.directory}/test-ejb</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.glassfish</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>${glassfish.version}</version>
<configuration>
<goalPrefix>glassfish</goalPrefix>
<app>target/${project.build.finalName}-TEST.jar</app>
<port>8080</port>
<name>MyApp</name>
<serverID>embedded</serverID>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>admin</id>
<phase>pre-integration-test</phase>
<goals>
<goal>admin</goal>
</goals>
<configuration>
<commands>
<param>create-jms-resource --restype javax.jms.QueueConnectionFactory jms/TestQueueConnectionFactory</param>
<param>create-jms-resource --restype javax.jms.Queue --property imqDestinationName=ceQueue jms/ceQueue</param>
</commands>
</configuration>
</execution>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>undeploy</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
Now, is there any way my IntegrationTest can be launched using surfire, in order to produce a proper report and fail build if test don't pass? Not to mention Cobertura.
Thank you for your help.
I didn't solve my problem so to say. What I had to do is upgrade to GlassFish 3.1.1. This version supports JMS in embedded mode. Therefore I can run a server programatically and deploy the queues I want using the admin command runner.
I just lost a bit of time trying to name my connection factory jms/connectionFactory where the jms/ prefix was unnecessary and was failing all lookups.
I also had to include a thread sleep in all my unit tests or else the server would close before tests are over.

Resources