Passing parameter for mvn test from pom - java

I have a springboot project with tests
there are few parameters(for ex passwords, pins etc) i would like to pass for mvn tests, I know this can be done with -D option from the cli. Can these values be passed from pom.
Below didnt seem to work, i guess this is for execution and not for compilation
<properties>
<someproperty> abcd </someproperty>
</properties>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -Dsomeproperty=someValue </argLine>
</configuration>
</plugin>
This did the trick.
Answering my question in case someone else needs this

Related

How to run unit tests in excludedGroups in Maven

I have a JUnit 4.12 SlowTests test suite that I want to exclude from execution unless specifically requested on the Maven command line.
I have added the following to my pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<excludedGroups>com.Example.SlowTests</excludedGroups>
<includes>
<include>**/*TestSuite.class</include>
</includes>
<excludes>
<exclude></exclude>
</excludes>
</configuration>
</plugin>
I have defined the category as SlowTests and applied it to the MySlowTests class.
I have annotated the test suite as follows:
#RunWith(Categories.class)
#IncludeCategory(SlowTests.class)
#SuiteClasses({ MySlowTests.class })
public class MySlowTestSuite
When I run mvn package all the unit tests except MySlowTests are executed.
However, looking at various answers such as https://stackoverflow.com/a/25639372/820657 and https://stackoverflow.com/a/21830866/820657 I expected the following command:
mvn package -Dgroups=com.Example.MySlowTests
to run the excluded MySlowTests tests but they don't run. In fact no tests run.
What am I doing wrong?
The Maven Surefire plugin has some issues w.r.t categories in versions < 2.13 (IIRC) but as long as you are using Surefire >= 2.13 the following will run any classes annotated with #Category(com.yourcompany.SlowTests.class):
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>com.yourcompany.SlowTests</groups>
</configuration>
</plugin>
This approach is often used with profiles, the following configuration ...
<profiles>
<profile>
<id>slow</id>
<properties>
<testCategories>com.yourcompany.SlowTests</testCategories>
</properties>
</profile>
<profile>
<id>fast</id>
<properties>
<testCategories>com.yourcompany.FastTests</testCategories>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${testCategories}</groups>
</configuration>
</plugin>
</plugins>
</build>
... can be used to run:
mvn install -P slow: runs the slow tests only
mvn install -P fast: runs the fast tests only
mvn install -P fast,slow: runs the fast and slow tests
Update 1: for this question: "Is there a way to use this approach so I can run all fast tests by default?"
You can define two properties:
<properties>
<includedCategories></includedCategories>
<excludedCategories>com.yourcompany.SlowTests</excludedCategories>
</properties>
Then update your surefire plugin definition like so:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${includedCategories}</groups>
<excludedGroups>${excludedCategories}</excludedGroups>
</configuration>
</plugin>
And, finally, add this profile:
<profile>
<id>slow</id>
<properties>
<includedCategories>com.yourcompany.SlowTests</includedCategories>
<excludedCategories></excludedCategories>
</properties>
</profile>
This just toggles the includedCategories and excludedCategories properties. By default, you include everything except those tests annotated with com.yourcompany.SlowTests (i.e. everything except your 'slow' tests). When you run with -P slow you exclude everything except those tests annotated with com.yourcompany.SlowTests (i.e. everything except your 'slow' tests).
Note: what I said in my original answer about Surefire versions < 2.13 misbehaving with Categories still stands so to make this work you need to be using a version of the Maven Surefire plugin >= 2.13.

Maven install does not encode in UTF-8 even if configured

Hi I have a problem with the encoding of my project.
When I run JUnit tests from eclipse, there are no failures. The problem is when I do maven > clean maven > install, one of the tests fails.
I have this string: "ADMINISTRACIÓN", and it's fine when i run the JUnit from eclipse, but I've printed the variable and when maven does the tests, the value of this string is: "ADMINISTRACI�N".
I've changed every property I could find of encoding in eclipse to UTF-8.
-Configured the pom this way:
(...)
<project>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
(...)
</properties>
</project>
(...)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
(...)
But the output is the same. I have a coworker that has the same project than me, and the same eclipse client and config, and her maven tests print accents with no trouble.
Any further ideas?
Thanks a lot!
Try run your build with:
mvn -DargLine=-Dfile.encoding=UTF-8 clean insall
if help, you can configure surefire in project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
</configuration>
</plugin>
Problem may occur because System.out use default system encoding, you can change this be setting file.encoding java property.
I tried all settings mentioned in this post to build my project successfully however that didn't work for me. At last I was able to build my project successfully with mvn -DargLine=-Dfile.encoding=UTF-8 clean insall command.

How to exclude some directory in maven command line for test? [duplicate]

Something like the following.
I would like a way to skip my dao tests in surefire. Trying to avoid overhead of defining Suites.
With CI I'd like to have one nightly that runs all tests and another 5 minute poll of SCM that runs only 'fast' tests.
mvn -DskipPattern=**.dao.** test
Let me extend Sean's answer. This is what you set in pom.xml:
<properties>
<exclude.tests>nothing-to-exclude</exclude.tests>
</properties>
<profiles>
<profile>
<id>fast</id>
<properties>
<exclude.tests>**/*Dao*.java</exclude.tests>
</properties>
</profile>
</profiles>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.tests}</exclude>
</excludes>
</configuration>
</plugin>
Then in CI you start them like this:
mvn -Pfast test
That's it.
Sure, no problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<!-- classes that include the name Dao -->
<exclude>**/*Dao*.java</exclude>
<!-- classes in a package whose last segment is named dao -->
<exclude>**/dao/*.java</exclude>
</excludes>
</configuration>
</plugin>
Reference:
Maven Surefire Plugin > Inclusions and Exclusions of Tests
(The excludes can not be configured via command line, so if you want to turn this behavior on conditionally, you will have to define a profile and activate that on the command line)
It is possible to exclude tests using the commandline; using ! to exclude.
Note: I'm not sure but possibly needs 2.19.1 or later version of surefire to work.
Examples:
This will not run TestHCatLoaderEncryption
mvn install '-Dtest=!TestHCatLoaderEncryption'
Exclude a package:
mvn install '-Dtest=!org.apache.hadoop.**'
This can be combined with positive filters as well. The following will run 0 tests:
mvn install '-Dtest=Test*CatLoaderEncryption,!TestHCatLoaderEncryption'
See the Maven Surefire docs.

run mvn release with several profiles

Is it possible to run maven with several profiles?
I have a java class annotated with #WebService. Depending on the maven profile the targetNamespace will change. If I run the
mvn release:prepare release:perform
twice, each time with a different profile, I will achieve what I want but the jar versions will not be same regarding the pom version.
So I thought running the release with 2 profiles could do it. Unfortunately when I enter
-P profile-1, -P profile-2
or
-P profile-1,profile-2
only one profile gets executed.
Here ary ma profiles:
<profiles>
<profile>
<id>profile-1</id>
<properties>
<target-namespace>sample-1.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-2</id>
<properties>
<target-namespace>sample-2.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I do print out the target-namespace properties to verify that in fact both profiles are running which is not the case.
Thanks
The second command-line (comma-separated profiles) is more correct, but won't quite do what you're trying to do, given your pom.
When you specify multiple profiles, they are all active in the same maven process -- it doesn't mean "run each goal with the first profile and then again with the second". So in the command that you are running, maven attempts to run with both profiles active.
Unfortunately, the contents of your profiles are overlapping. Well, the properties are. The <build> section is identical in each, which means that it could even be moved outside of the profiles and just declared once.
But the <properties> are completely overlapping ... hopefully it's clear that the target-namespace property cannot have two different values at once.
I wrote an answer below for some ways to address this properties question, which was posed in a very general way. But honestly, for anyone who ever reads this: there might easily be other, better ways to accomplish whatever you're actually trying to do. The OP was clearly attempting to do something more specific and this multiple profile thing was the method they were deciding to try, but they were confused when it wasn't working. A question about how to best solve their actual specific problem would probably have been a more productive question. But just in case this is a good way to solve your problem, here goes.
For the purposes of discussion I'm going to ignore the <build> contents above, which appear completely nonsensical, and assume that you need separate profiles because you'll sometimes want to activate each one independently of the other (maybe when not running release).
You can't use two values at once for the same property. You need to have multiple executions occur instead. You have a few different options for this...
Multiple commands. No changes needed to your pom, but this requires that your profiles have no real applicability to the actions in release:prepare, which you'll only run once. If that's not the case, this won't work. The first two commands could be combined into one but the third one has to be separate:
mvn release:prepare
mvn release:perform -P profile-1
mvn release:perform -P profile-2
Multiple <executions> of the release plugin itself, with different releaseProfiles in each one. This is a minor variation on option 1, but might be more appealing depending on your needs:
...
<artifactId>maven-release-plugin</artifactId>
<executions>
<execution>
<id>id-1</id>
<configuration>
<releaseProfiles>profile-1</releaseProfiles>
</configuration>
</execution>
<execution>
<id>id-2</id>
<configuration>
<releaseProfiles>profile-2</releaseProfiles>
</configuration>
</execution>
...
Because the release plugin isn't bound to any phase (you run it directly), you'll have to specify both executions separately on the command line, like mvn release:prepare release:perform#id-1 release:perform#id-2.
Use unique plugin execution ids within each profile -- then run mvn release:prepare release:perform -P profile-1,profile-2 as usual. You'd also have to change the profiles so that they use different property names. e.g.
...
<profile>
<id>profile-1</id>
<properties>
<namespace-1>sample-1.org</namespace-1>
...
<execution>
<id>antrun-1</id>
<configuration>
<target>
<echo>${namespace-1}</echo>
...
<profile>
<id>profile-2</id>
<properties>
<namespace-2>sample-2.org</namespace-2>
...
<id>antrun-2</id>
<configuration>
<target>
<echo>${namespace-2}</echo>
...
... this assumes that the all the plugin executions are such that they would be fine to run in a round-robin order like this for each phase of the overall build (clean phase = antrun 1 then antrun 2; test phase = surefire 1 then surefire 2; etc).
Due to Maven Documentation your second option is correct:
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the section in settings.xml.
Hence you need to use next command format:
mvn <goals_list> -P profile-1,profile-2
If behaviour is incorrect there could be wrong profile configuration in your POM file. Could you please provide its content?
-P profile-1,profile-2 should be the right form, if it doesn't work it might be due to some conflicts etc. in the profiles themselves.
As an alternative you could try and play with the <activation> tags in settings.xml to activate the profiles based on some property, e.g.
<activation>
<property>
<name>releaseProfile</name>
</property>
</activation>
Then set the property in your mvn call: mvn -DreleaseProfile release:prepare release:perform. (You might have to pass a value for the property, it's been a while since I used that).

Is there a way to make maven build class files with UTF-8 without using the external JAVA_TOOL_OPTIONS?

I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install
However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.
Tried everything as described here: enabling UTF-8 encoding for clojure source files
Anyone has a light on that awesome Maven issue?
#Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872
It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>...</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>...</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Yes there is, define
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc
LANG=en_US.UTF-8

Categories

Resources