Why "mvn site" is running tests? - maven

I have the following build configuration for a Spring Boot application.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
</plugins>
</build>
When I run mvn site, I expect the site goal of the site phase to execute. But my tests are running, which means the test goal of the default lifecycle is also getting executed.
I know I can skip running the test with -DskipTets, but I want to understand why this is happening?

Related

How to run specific profile ID / suites (to run specific TestNG.xml) in Gradle?

I am new to Gradle, but I know in Maven we can run specific profile.
In my case, I have 2 TestNG.xml files and in Maven POM.xml I can write like this
<profiles>
<profile>
<id>FirstRegression</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng1.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>SecondRegression</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng2.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
In command line , we can choose which profile (which TestNG.xml file) we want to run.
mvn test -PFirstRegression will execute our testng1.xml file only, meanwhile
mvn test -pSecondRegresion will execute our testng2.xml file only.
How can we do this in build.gradle file ? so we can choose which profile to run in gradle.
I can put like this in build.gradle
plugins {
id 'java'
}
test {
useTestNG() {
suites 'testng1.xml'
suites 'testng2.xml'
}
}
But when I run gradle clean build , it will run both of them.
Is there anyway we can say gradle clean build --"please run testng2.xml only" ? Thank You.

Multi Module Maven Project Code Coverage Issue on Sonar

I have a question related to multi module maven project with JaCoCo and SONAR.
I have a parent and 3 child modules.
parent |-child1 - pom.xml |-child2 - pom.xml |-child3 - pom.xml |-pom.xml
I include the JaCoCo plugin in the parent pom.xml. When I run the mvn clean install sonar:sonar build from parent pom.xml, I see that each child generates its own jacoco.exec file. Something like this child1/target/jacoco.exec, child2/target/jacoco.exec etc . However, there is no jacoco.exec been generated in the parent level.
When I run my sonar analysis, I see that the unit test coverage is showing up as 0.0% on the sonar dashboard.
My question is
1. What should I do to see the unit test coverage for the entire project?
2. To show the one unit test coverage, Does SONAR pick the jacoco.exec file from the parent level or from the child level?
Please help. This is really a road blocker for me. Appreciate all your inputs.
I was facing the same issue, then I included the following in each module pom xml and it worked:
<!-- Sonar START -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
<jacoco.version>0.7.9</jacoco.version>
<!-- Sonar START -->
Plugins :
<!-- SONAR config START -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<tasks>
<echo>Project Base Path (WEB):: ${project.basedir}</echo>
<echo>Jacoco exec target path (WEB):: ${sonar.jacoco.reportPath}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>${argLine}</argLine>
</configuration>
</plugin>
<!-- SONAR config END -->
And configure in Jenkins for "Goals and options" as:
sonar:sonar
For Running specific tests :
sonar:sonar -DfailIfNoTests=false -Dtest=<Test Class> test
I had the similar issue, 0.0% coverage & no unit tests count on Sonar dashboard with SonarQube 6.7.2:
Maven : 3.5.2,
Java : 1.8,
Jacoco : Worked with 7.0/7.9/8.0,
OS : Windows
After a lot of struggle finding for correct solution, resolved issue with this configuration my parent pom looks like:
<properties>
<!--Sonar -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.4.0.905</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
I've tried few other options like jacoco-aggregate & even creating a sub-module by including that in parent pom but nothing really worked & this is simple. I see in logs <sonar.jacoco.reportPath> is deprecated,but still works as is and seems like auto replaced on execution or can be manually updated to <sonar.jacoco.reportPaths> or latest. Once after doing setup in cmd start with mvn clean install then mvn org.jacoco:jacoco-maven-plugin:prepare-agent install & then do mvn sonar:sonar , this is what I've tried please let me know if some other best possible solution available.Hope this helps!! If not please post your question..

How to set default parameter in maven project?

To disable cache in my test phase, I need to enter command :
mvn test -Dnet.sf.ehcache.disabled=true
Is there any way to set this property "net.sf.ehcache.disabled" as default? (Which menas: whenever I specify "mvn test" I want to set this parameter "net.sf.ehcache.disabled" as true automatically)
You could try to configure the surefire plugin in your pom.xml like that:
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<net.sf.ehcache.disabled>true</net.sf.ehcache.disabled>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
...

run jasmine.js test as part of Maven 'test' goal

I have the following jasmine tests configuration in my project pom.xml:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
<goal>jasmine</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>${basedir}/src/main/js</jsSrcDir>
<sourceIncludes>
<include>myCode.js</include>
</sourceIncludes>
<jsTestSrcDir>${basedir}src/test/js/specs</jsTestSrcDir>
<specIncludes>
<include>mySpec.js</include>
</specIncludes>
</configuration>
</plugin>
...
with this configuraton I can run jsamine:test and the test wil run.
I want to run the tests as part of the goal test, but Maven won't run them.
I even tried removing the line <goal>jasmine</goal> but Maven still won't run the jasmine tests.
What am I doing wrong?
You must also add the execution of the plugin like this into you project:
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
cause the one you've defined in pluginManagement does not really execute the plugin.

Disable sure-fire and execute fail-safe report to run my test cases in a Maven project

In my parent POM I have configured for sure fire report. But in some modules, I need to skip the sure fire report generation and instead I want to run fail-safe plugin to generate the test report.
I have tried like
<properties>
<skipTests>true</skipTests>
<skipFailsafeTests>false</skipFailsafeTests>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>${skipFailsafeTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipExec>${skipTests}</skipExec>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
But still it seems sure-fire report is running. Is there any way to overcome it?

Resources