Show Maven Profile - maven

I want to build a spring-boot Java project without running the integration tests. I'm taking the approach of adding annotation
#Category(IntegrationTest.class)
and defining interface
public interface IntegrationTest {}
I have this in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>${maven-surefire-plugin.skipUnitTests}</skipTests>
<excludedGroups>
com.test.annotationtype.IntegrationTest
</excludedGroups>
</configuration>
</plugin>
When I run mvn install, it runs the integration test.
It's a multi-profile pom.xml, and I'd like to at least verify that the profile I think I'm running, really is the one actually run.
<profile>
<id>woodsman-default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
...
</profile>
When I run
mvn -debug install
no where in the log do I see the profile id woodsman-default. How can I get Maven to show me which profile it decided to use?
Bonus points for suggesting why it's not ignoring integration tests, but I'd settle for the easy answer.
Thx
Woodsman

Related

Surefire wont trigger the integration tests

I am facing this issue in my Spring MVC project:
I have two types of tests. Unit and Integration. Both separated by package in test/java, respectively by test/java/unit and test/java/integration. I am using maven-surefire plugin to trigger tests.
I have two profiles, unit and integration defined in pom.xml
<profile>
<id>unit</id>
<properties>
<include.tests>**/unit/**/*.java</include.tests>
</properties>
</profile>
<profile>
<id>integration</id>
<properties>
<include.tests>**/integration/**/*.java</include.tests>
... other properties for configuring integration platform (DB, ...) omitted for simplicity
</properties>
</profile>
And surefire plugin is configured like this
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>${include.tests}</include>
</includes>
</configuration>
</plugin>
When I run mvn surefire:test -Punit, all unit tests are triggered, which is what I want (I´ve checked the amount of tests passed is same as amount of tests passed when I trigger them by unit folder for example from IDEA).
But when I try to run mvn surefire:test -Pintegration, the task will run and "succeed", but the number of tests peformed is 0. But when I run these tests from IDEA for example with Integration profile active, I see there is X tests which will run successfully.
Basic integration tests uses #RunWith(SpringJUnit4ClassRunner.class) for running test (plus some config xmls to configure other properties).
Unit tests are JUnit 5 based using Mockito.

Possible to Skip Junit tests only in Jenkins?

Currently our companies Jenkins deploy process isn't set up to read external property files for testing. I would still like to write and run tests locally and then have tests be skipped when pushed to github (where a jenkins process gets kicked of to build the app and push it into a container).
Is it possible to programmatically tell surefire pluggin to only run in a local environment but not to run in the Jenkins pipeline?
I know I can use the following config:
<configuration>
<skipTests>true</skipTests>
</configuration>
but this means I need to remember to comment and uncomment each time I push, and I would rather not do that.
Thank you
The answer of #NullPointerExeption is an option.
You can also use maven profiles for that.
e.g.
<profiles>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>jenkins</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
When a developer run tests they will be executed as skipTests is false by default
From Jenkins use
mvn clean verify -Pjenkins
This will skip tests.
In general it's good to have a jenkins profile and place all the staff that it's related to jenkins environment in this profile
More on profiles here
You can specify the build in your jenkins file to skip the test during build stage.
This will ensure your project is build skipping tests
stages {
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
}
}
}

Always display the current active profiles during the compile phase in Maven

I have a Spring Boot application which supports two profiles: dev and release. Obviously, the dev profile is used when working locally, and release profile is used by Jenkins as part of a CI/CD pipeline when actually deploying the application on a server.
Profiles definition
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<activatedProperties>release</activatedProperties>
</properties>
</profile>
</profiles>
dev profile is activated by default since this is where developers spend most of their time: developing. I want them to specify the release profile when exporting the application somewhere else, but even better, delegate that task to Jenkins.
I just discovered the following command to see which profiles are active:
mvn help:active-profiles
So I can use this in my Jenkins pipeline script
mvn clean compile -Prelease help:active-profiles
That works. BUT, I wonder if there is a way to always run help:active-profiles goal during the compile phase, so all developers can clearly see which profile they are using.
Thank you
You can add Maven Help Plugin as shown below:
<plugin>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>print-profile</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
You can expect the log with the below message:
[INFO] --- maven-help-plugin:2.2:active-profiles (print-profile) # testProject ---
[INFO]
Active Profiles for Project 'com.test.testProject:jar:1.0.0-SNAPSHOT':
The following profiles are active:
- sonar (source: external)
- release (source: external)
- dev (source: com.test.testProject:jar:1.0.0-SNAPSHOT)

Maven: How do I activate a profile from command line?

This is a snippet from my pom.xml.
I tried the following, but the profile was not activated.
mvn clean install -Pdev1
mvn clean install -P dev1
When I tried mvn help:active-profiles no profiles were listed as active.
If I set <activeByDefault> for dev1 to true, and run mvn help:active-profiles, it shows me the profile is activated.
<profile>
<id>dev1</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<env>local</env>
<properties.file>src/test/resources/dev1.properties</properties.file>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/dev1.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>dev2</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<env>local</env>
<properties.file>src/test/resources/dev2.properties</properties.file>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/dev2.testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I am wondering why my profile is not getting activated. Has anyone encountered similar issue?
Both commands are correct :
mvn clean install -Pdev1
mvn clean install -P dev1
The problem is most likely not profile activation, but rather the profile not accomplishing what you expect it to.
It is normal that the command :
mvn help:active-profiles
does not display the profile, because is does not contain -Pdev1. You could add it to make the profile appear, but it would be pointless because you would be testing maven itself.
What you should do is check the profile behavior by doing the following :
set activeByDefault to true in the profile configuration,
run mvn help:active-profiles (to make sure it is now always activated, even without -Pdev1),
run mvn install.
It should produce the same result as before, and therefore confirm that the problem is the profile not doing what you expect.
Activation by system properties can be done as follows
<activation>
<property>
<name>foo</name>
<value>bar</value>
</property>
</activation>
And run the mvn build with -D to set system property
mvn clean install -Dfoo=bar
This method also helps select profiles in transitive dependency of project artifacts.
I have encountered this problem and i solved mentioned problem by adding -DprofileIdEnabled=true parameter while running mvn cli command.
Please run your mvn cli command as : mvn clean install -Pdev1 -DprofileIdEnabled=true.
In addition to this solution, you don't need to remove activeByDefault settings in your POM mentioned as previouses answer.
I hope this answer solve your problem.
Just remove activation section, I don't know why -Pdev1 doesn't override default false activation. But if you omit this:
<activation>
<activeByDefault>false</activeByDefault>
</activation>
then your profile will be activated only after explicit declaration as -Pdev1

How to skip unittests when using mvn scm:bootstrap

I'm trying to use the mvn scm plugin to check out the daily tag, and create an assembly from that version of the code. I configured the scm plugin and everythhing is working well, except that I can not seem to tell it to not run the unittests.
I tried:
Passing the -Dmaven.test.skip=true command line parameter
Creating a profile where the surefire plugin skips test, and list that profile in the scm configuration "profiles" section
setting the "maven.test.skip=true" as an environment variable
In all cases, when the scm plugin starts running the goals I told it to run in the configuration (see below), it also runs the unittests.
Below is the example I used to skip tests by using a profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.0</version>
<configuration>
<goals>install,assembly:assembly</goals>
<profiles>skiptest</profiles>
</configuration>
</plugin>
And this is the profile (I defined this in the pom.xml of the project):
<profiles>
<profile>
<id>skiptest</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The command I use to do the checkout and bootstrap is:
mvn scm:bootstrap -DscmVersion=daily-20110427-421 -DscmVersionType=tag
I'm running mvn 2.2.1 on a Linux machine, and doing a checkout from a CVS repository. It's an existing project, I have continuous integration and tagging all up and running, I just want to check out a daily tag and create an assembly from that.
Any tips are much appreciated.
Edit: Got it to work with the answer below, but only after I upgraded to maven-scm-plugin version 1.1. Apparently, 1.0 did not propagate profiles.
Try this in the profile:
<profiles>
<profile>
<id>skiptest</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
</profiles>

Resources