I working on a Java/Maven project. I use mvn test to run my test suite. However, in this case all assertions are enabled. Unfortunately this increase the time of some operation from log(n) to n^2 which is rather inconvenient. What is more is that the operation is defined in a dependency packageA whereas I work on packageB. Using plain Java I could add -ea:packageB... to enable only the assertions which I actually need. Is it possible to achieve this behaviour using either the content of the pom.xml or a command line argument to mvn? I know I can disable all assertions but I would rather keep the assertions in the package that I am actually working on...
You can achieve that using surefire-plugin. But first you need to specify profiles for different packages.
Using surefire-plugin you can exclude or include packages, using wildcard or regex.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>%regex[.*[Cat|Dog].*Test.*]</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<excludes>
<exclude>**/TestCircle.java</exclude>
<exclude>**/TestSquare.java</exclude>
</excludes>
</configuration>
</plugin>
You can find more detailed information on this site.
I use these properties:
<maven.test.skip>false</maven.test.skip>
<maven.test.failure.ignore>false</maven.test.failure.ignore>
Then you can leave the Asserts as is, but change these properties to either fail on Assert.fail(), or don't do the tests at all.
Or similarly an extra parameter using CMD, like adding:
-Dmaven.test.skip=true
Related
I'm having difficulty trying to add an argument to the jvm. It looks like using surefire is the only way to do this. My current code in the pom.xml is
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>-Xmx512m</argLine>
<systemPropertyVariables>
<all.clusters>${all.clusters}</all.clusters>
<branding.token>${brandingToken}</branding.token>
</systemPropertyVariables>
</configuration>
</plugin>
Maven is new to me so perhaps I'm missing something simple.
Running in a Netbeans 8.1 environment.
Turns out that what I needed to do was not to get surefire to run with special arguments, because that only covers tests. The trick to getting it working was the fact that this project is a NetBeans application which uses the
nbm-maven-plugin.
The following blog post describes how to modify the arguments.
Blogpost
I have a test suite which needs to have some setup code to be executed before hand to make sure some data is correct in our database.
We're using the maven surefire plugin to run tests in parallel. ${tests.wildcard} is specified by the profile.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkCount>4</forkCount>
<reuseForks>false</reuseForks>
<includes>
<include>${tests.wildcard}</include>
</includes>
</configuration>
</plugin>
I'd like to be able to run a method only once per entire maven execution before surefire runs my tests in parallel. How can I do that?
You could have a special test case which executes your verification code (and fail if the case).
This test case would then be executed by a specific Maven Surefire execution (excluding other tests) and attached to a Maven phase occurring just before the test phase (like process-test-classes): hence, effectively being invoked once per Maven run and before any other test.
Then, the normal test phase would execute any other desired test, excluding the special init test.
An example of such a configuration would be:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<excludes>
<exclude>**/InitTest.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>test-init</id>
<phase>process-test-classes</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<test>InitTest</test>
</configuration>
</execution>
</executions>
</plugin>
Note the global configuration for any Surefire execution would exclude the special init test. Then an additional execution would be executed (before the test phase) and would run only the init test (using the <test> element, which takes priority over any other include/exclude).
As such you would have the following flow:
Execute special init test and verify data into database (and fail if required)
Execute any other test
Update
Note that you can achieve the same and perhaps in a more semantically correct way by overriding the default surefire test execution (having default-test execution id) to run the special test (and exclude the others) and then add another surefire execution for the rest (as described above as global configuration, this time as specific execution configuration).
With this approach everything would be attached to the test phase, that's why it would be semantically more correct, although a little bit more verbose in the pom.
I'm working on creating a reporting mechanism for tests and I want it to execute after all of the tests are run, and the resulting junit xml files are written out. Unfortunately, it doesn't appear that the executing continues after it fails in the test phase. Does anybody know what Maven configuration I could use to get the desired result if possible. Many Thanks.
May be this helps?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
We have our POM defining the maven-surefire-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
<argLine>-Xms64m -Xmx256m</argLine>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
However, our Java tests (which involve some parallel tests and static singletons) only run properly when we run our test phase/build using:
mvn test -DforkMode=always
Strangely, even if we change our <configuration> to use (instead of the newer options):
<forkMode>always</forkMode>
And run:
mvn test
It will fail. But then if we run:
mvn test -DforkMode=always
It will pass. Instead of the newer options, it still will only work if we explicitly provide forkMode on the command line. We have tried this with multiple versions of the surefire plugin, to the same effect.
Are there any locations where this property could be overridden, or known issues in which the XML configuration is not properly used?
Rookie mistake. The configuration I was using was listed in a separate <profile> block that was not being executed. The profile with:
<activeByDefault>true</activeByDefault>
Did not include its own Surefire configuration at all (so it didn't show up in a search), and was using inherited values, which explains why the command-line system properties were able to affect its behavior.
I want to compile only selected files or directories (including subdirectories) within source directory. I was pretty sure I can do this using <includes> of maven-compiler-plugin's configuration, but it seems to not work as I expect since it still compiles all classes into target/classes. What is really strange, Maven output suggest that the setting actually does its work, because with:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<includes>
<include>com/example/dao/bean/*.java</include>
</includes>
</configuration>
</plugin>
I have:
[INFO] Compiling 1 source file to c:\Projects\test\target\classes
but with no compiler's configuration I have:
[INFO] Compiling 14 source file to c:\Projects\test\target\classes
In both cases however, all 14 classes are compiled into target/classes as I mentioned. Can you explain that or suggest another solution to compile only selected files?
Simple app with 3 classes.
com/company/Obj1.java
com/company/Obj2.java
com/company/inner/Obj3.java
build in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<includes>
<include>com/company/inner/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
result: 1 class is compiled.
And any combination of includes is working well
or you mean something else?
I have faced a similar situation. We needed to hot swap only modified files to our remote docker container in order to improve changes-deploy time. This is how we solved it.
Add includes option in build plugin with command line argument.
Note since we wanted to add multiple files, so we have used includes and not include
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>1.8</source>
<target>1.8</target>
<includes>${changed.classes}</includes>
</configuration>
</plugin>
Now run compile phase with argument, example:
mvn compile -Dchanged.classes=com/demo/ClassA.java,com/demo/ClassB.java,com/demo2/*
maven-compiler-plugin using Ant-like inclusion/exclusion notation.
You can see examples in Ant documentation Ant FileSet Type
If you are want include only files from one directory, you need write it like you did:
<include>com/example/dao/bean/*.java</include>
To include also subdirectories from path, it will be:
<include>com/example/dao/bean/**/*.java</include>
I had no difficulty including or excluding files for compilation with maven compiler plugin 2.5.1. Here is the dummy project I used for the purpose. Perhaps the include pattern that you use is different.