Issue
I am trying to skip test when I run mvn clean package command , but the unit tests should run when I execute mvn clean test.I know about these 2 options
- <maven.test.skip>true</maven.test.skip>
- Use -Dmaven.test.skip=true parameter . Like mvn -Dmaven.test.skip=true package
But I can't use option 2 , because I am running the build from a platform that does not provide an option to add the parameters in a maven phase. So , I can only use mvn clean test / mvn clean package command.
I can't use option 1 , because if I set the skip tests parameter in pom.xml , then tests are skipped even in mvn test phase. So , what can be my option here , given the above constraints
Solution
In your pom.xml
- set skipping the tests to by default - set skipTests to true
- add a profile to run the tests, activated by the presence of a local file or directory. Override the skipTests property and allow the tests in the profile - set it to false.
- create the file in your local environment, thus activating the profile and the testing. Do not include it in the version control.
You would not have the file on the build platform. Therefore the default behavior would be active, skipping the tests.
For example:
<properties>
<skipTests>true</skipTests>
</properties>
...
<profile>
<id>api</id>
<activation>
<file>
<exists>${basedir}/run.test</exists>
</file>
</activation>
<properties>
<skipTests>false</skipTests>
</properties>
</profile>
Here the name of the local file is run.test, held in the project's root (base) directory.
Answered By - Rusi Popov
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)