Why "mvn install" is running my tomcat in Install Phase? - java

I have configured failsafe together with tomcat7-maven-plugin for making integration-test. It's great and works very well when I type:
mvn clean verify -P integration-test
My pom.xml is like that:
<!-- Runs integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<executions>
<!-- Invokes both the integration-test and the verify goals of the Failsafe Maven plugin -->
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<!-- Skips integration tests if the value of skip.integration.tests property is true -->
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/onde-vou</path>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
</plugin>
But I got an unexpected behavior. When I type:
mvn clean install
It starts the tomcat and shutdown at the end of the process.
How do I avoid this behavior? It is useless for me and I lose some secs.

You can try mvn clean install -Dmaven.test.skip=true which skip the test, hence skips the execution pre-integration-test
or remove
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>

Related

Sonar doesn't show the test coverage

My pom.xml has the below properties:
<sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/../target/jacoco.exec</sonar.coverage.jacoco.xmlReportPaths>
<sonar.language>java</sonar.language>
<surefire.plugin.argline>-XX:PermSize=256m -XX:MaxPermSize=1048m</surefire.plugin.argline>
This is what I have in my Jacoco plugin configuration:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<phase>initialize</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>target/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
<append>true</append>
</configuration>
</execution>
<execution>
<id>default-report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>target/jacoco-report</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<destFile>target/jacoco.exec</destFile>
<dataFile>target/jacoco.exec</dataFile>
<append>true</append>
</configuration>
</plugin>
I'm using Sonar 7.8. I see the jacoco.exec file into the target folder after run:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install sonar:sonar
However, when I go to the Sonar page, I see how many test I have, but I only see 0% in the test coverage. Any suggestion?

Maven exec won't recognize my app in src/test

I am trying to run a mvn exec for one of my spring boot apps but it is giving me a class loader exception.
I have an app in main named App.java that runs of 28433 port.
I also have an app in src/test named MockServerApp.java.
I am trying to run mvn exec to execute them.
When I do mvn exec on my main app it works and starts. When I do it on my MockServerApp though it causes a class loader exception.
Here is my POM
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>second-cli</id>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
</executions>
</plugin>
Here is the error I am getting :
java.lang.ClassNotFoundException: com.nulogix.billing.mockserver.MockServerApp
at java.net.URLClassLoader.findClass (URLClassLoader.java:436)
at java.lang.ClassLoader.loadClass (ClassLoader.java:588)
at java.lang.ClassLoader.loadClass (ClassLoader.java:521)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:270)
at java.lang.Thread.run (Thread.java:835)
How do I fix this?
EDIT: I have tried using the spring-boot-maven-plugin since I will be using the MockAppServer for integration tests and have tried launching both in the pre-integration life cycle.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>pre-integration-test2</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.App</mainClass>
</configuration>
</execution>
<execution>
<id>post-integration-test2</id>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<mainClass>com.nulogix.billing.mockserver.MockServerApp</mainClass>
</configuration>
</execution>
</executions>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Still gives me the classNotFoundException.
You can mark MockServerApp with following annotations. And instead of trying to run MockServerApp from maven, you can simply run junits from maven and let Spring manage creation of test context.
#RunWith(SpringRunner.class)
#SpringBootTest
You can see further documentation at spring boot testing

How to run embedded TomEE for integration tests with test resources

I have J2EE project based on maven. This project contains connection to database, which is set up via resources.xml and persistence.xml. Connection works fine for normal deployment.
My problem is, that i would like to run embedded TomEE server for integration tests. For these tests i need use inmemory database.
To start TomEE i use combination of maven plugins showed below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.13</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
</plugin>
When i start maven goal mvn install, server runs as expected, but with wrong DB connection. I did not find way, how to set up, that i need use src/test/resources instead of src/main/resources.
Do you know how to set up this plugin in way? I'am open to suggestions of similar solutions, which is simple and do not contains 'lot of' frameworks.
Thank you in advance.
After some investigation i found solution described below.
Three plugins were added into pom.
First plugin will start TomEE server for us.
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>7.0.4</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<checkStarted>true</checkStarted>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<simpleLog>true</simpleLog>
</configuration>
Second one will replace proper resources and persistence files.
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-test-persistence</id>
<phase>process-test-resources</phase>
<configuration>
<tasks>
<!--backup the "proper" files-->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml.proper"/>
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml.proper"/>
<!--replace the "proper" files with the "test" version-->
<copy file="${project.build.testOutputDirectory}/META-INF/resources.xml" tofile="${project.build.outputDirectory}/META-INF/resources.xml"/>
<copy file="${project.build.testOutputDirectory}/META-INF/persistence.xml" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>restore-persistence</id>
<phase>prepare-package</phase>
<configuration>
<tasks>
<!--restore the "proper" files -->
<copy file="${project.build.outputDirectory}/META-INF/resources.xml.proper" tofile="${project.build.outputDirectory}/META-INF/resources.xml"/>
<copy file="${project.build.outputDirectory}/META-INF/persistence.xml.proper" tofile="${project.build.outputDirectory}/META-INF/persistence.xml"/>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
Last plugin force 'clean' before install. Without this i had problem in case, when i forget clean project before install.
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>initialize</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>

Clover does not create database

I'm trying to run Clover to see test coverage, but it will not create any database (and thus not give any report).
Among others, I've tried to run
mvn clean clover2:setup clover2:instrument clover2:clover clover2:check
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>4.0.4</version>
<configuration>
<!-- <cloverDatabase>C:\clover\clover.db</cloverDatabase> -->
<!-- <reportsDirectory>${project.build.directory}/testreports</reportsDirectory> -->
<targetPercentage>10%</targetPercentage>
<includes>
<include>**Test.java</include>
<include>**IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>instrument</goal>
<goal>check</goal>
<goal>setup</goal>
</goals>
</execution>
</executions>
</plugin>
I've run with and without the <cloverDatabase> and <reportsDirectory> properties. No difference.
No database is ever created anywhere. Why?
I managed to get it working again by removing Clover, and then adding Clover back with only simple settings
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>4.0.4</version>
<configuration>
<targetPercentage>70%</targetPercentage>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
If I try to add inclusion specifications, then suddenly it won't create any db anymore....

maven: Cannot get pre-integration-phase to work

I'm trying to get pre and post integration phase to work with maven, to no avail.
My goal is to set up and tear down integration tests by running some binaries that are necessary. I'm testing with antrun-plugin and exec-plugin, but none of them prints the messages.
I'm running mvn verify. If I bind the plugins to clean phase and run mvn clean, the echo message and the ls are shown.
What's wrong? I'm using maven3
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.3</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>ls</executable>
<arguments>
<argument>-la</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>xxx</id>
<configuration>
<target>
<echo>Cleaning deployed website</echo>
</target>
</configuration>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I helped someone else get the failsafe plugin configured correctly earlier. Try explicitly specifying the phases in the failsafe plugin executions. Not sure why that is needed, as the failsafe plugin docs say the goals are supposed to be bound to correct phases by default - but it seems to be.

Categories

Resources