I have a java program that is built using Maven and I need to enable the assert
keyword. Ideally, I'd want to enable assertions in the maven build command.
Asked
Active
Viewed 1.1k times
6

Kalle Richter
- 8,008
- 26
- 77
- 177

kabb
- 2,474
- 2
- 17
- 24
-
5[By default, Surefire enables JVM assertions for the execution of your test cases](http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#enableAssertions) – Jens Piegsa Nov 13 '13 at 23:41
-
The problem is I need the assertions in my main program, not in my test cases. – kabb Nov 14 '13 at 00:02
3 Answers
11
Maven compiles and builds the java code. Assertion errors come when you are actually running java code so with maven you can't do it this way
unless you are using maven plugin to launch java code, you would have to supply -ea
to jvm
exec:java
Pass -ea
to commandline argument
Surefire
if you meant for test execution then configure sure-fire plugin to pass -ea
to jvm
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<enableAssertions>true</enableAssertions>
</configuration>
</plugin>
-
2The surefire plugin enables assertions by default: http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#enableAssertions so there is no need to do that explicitly. – Stephan Jan 13 '16 at 06:45
5
The only things that worked for me was
export MAVEN_OPTS="-ea"

johsin18
- 838
- 7
- 10
-
When running java from mvn via exec:java this solution works to enable assertions. – Chris Mazzola Feb 11 '21 at 16:44
0
You cannot build application with assertions enabled since they are enabled at runtime depending on whetevery or not you pass -ea
argument to JVM. Here is configuration of maven exec plugin that enables assertions when running a program:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-ea</argument>
<argument>-classpath</argument> <classpath />
<argument>io.mc.validationdemo.App</argument>
</arguments>
</configuration>
</plugin>

csharpfolk
- 4,124
- 25
- 31