How to set default parameter in maven project? - maven

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>
...

Related

Why "mvn site" is running tests?

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?

Can not append system properties from profile config

I have build configuration for maven failsafe plugin which includes systemPropertyVariables:
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<buildDirectory>${project.build.directory}</buildDirectory>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Also I have a profile which can append few properties to same plugin via pluginManagement:
<profile>
<id>test</id>
<activation>
<property>
<name>test.host</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<TEST_HOST>test.host</TEST_HOST>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
But I can't read properties from this profile. It's working fine if I delete systemPropertyVariables in build plugin configuration section and keep in pluginManagement configuration of profile. It seems that I need to merge these properties when used together, so I tried to add combine.children="append" and combine.self="append" to configuration of plugin, but it didn't help.
UPDATE:
I've found the cause of this issue: I didn't mention that I have parent pom including from this one, and parent pom.xml has these lines:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.0-M3</version>
<configuration combine.children="append">
<systemProperties>
<!-- some properties that I don't use -->
</systemProperties>
</configuration>
</plugin>
And these parent pom failsafe configuration is breaking all configs in my pom.xml - if I remove parent pom reference, failsafe properties will be included correctly. I can't just remove parent pom reference and can't modify it (actually I can fork it and edit, but it's not an easy task), is it possible to override these properties, since I don't use them?
Attached minimal reproducible project: https://send.firefox.com/download/88dfb9f933c9567f/#71ij1jbuEuAYgdwIiZQZRg
I managed to do it by adding an extra pom in between the untouchable (or pariah) parent and current child, I call this new middle pom midboy =)
So midboy has only the following difference from pariah;
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.0.0-M3</version>
<inherited>false</inherited>
</plugin>
And this pom becomes the the new parent pom of the child, while it uses the original pariah pom as its parent.
child ---[inherits]---> midboy ---[inherits]---> pariah
With inherited:false I essentially cut-off the failsafe plugin being passed into the child and breaking its profile-based configuration addition to its own local failsafe plugin & config. & this works perfectly!
No skipping of the test this time! 😁
Running com.test.so51905256.PropITCase
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 s - in com.test.so51905256.PropITCase
p.s. don't forget to add some sort of <activation> for the test profile

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.

Is it possible to get the list of active profiles in Maven?

In Maven, is there a way to get a list of the active profiles, say, as a property or as a text file?
More specifically, when I run:
mvn resources:resources -P MyProfile
I want to get the string MyProfile somewhere I can read it into my Java program.
Maven 3.2.1
Thanks
Edit
I attempted to configure the Maven Help plugin to run the active-profiles goal whenever the goal resources:resources is run by configuring an execution to participate in the process-resources phase as shown below. That did not work either ...:
<packaging>jar</packaging>
<build>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You could try this maven plugin. The configuration below will create a text file that will contain the profiles that were active during the build.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>2.2</version>
<configuration>
<output>${basedir}/target/active-profiles.txt</output>
</configuration>
</plugin>

Is testng.xml file updated automatically

When running testng with mvn, I configured my workspace as required:
My pom.xml file is configured with all the required dependencies and testng.xml contains all the required classes.
Yet, when I add a new test class, the testng.xml isn't updated automatically-
Shouldn't it scan from the root for the corresponding tests? Or will I have to update the testng.xml file manually?
(BTW, my workspace is configured according to the following post: How to call testng.xml file from pom.xml in Maven)
The xml method gives you more granular control of your test sets. It will not be altered automatically, although eclipse will generate xml configs on the fly if you select say a folder and right click run as testng. If you would just like to run all testng annotated test and you have maven configured properly with surefire or failsafe, you don't even need an xml file. Just run "mvn verify" and all test should be run based on annotation. If this doesn't work, please post your surefire/failsafe pom sections.
If you want to configure a particular xml in maven, use something like (surefire or failsafe work the same.):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<configuration>
<argLine>-Xmx1024m</argLine>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</execution>
</executions>
</plugin>
If you do need more granular control, and would like to use an xml file specified by maven, you launch it via "verify -P MyProfile"
<profiles>
<profile>
<id>MyProfile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>MyProfile.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>MyOtherProfile</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>MyOtherProfile.xml</suiteXmlFile>
<!-- <groups>functest,perftest</groups> -->
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profiles>

Resources