Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable assert in a maven built project

Tags:

java

assert

maven

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.

like image 650
kabb Avatar asked Nov 13 '13 23:11

kabb


People also ask

How do I enable assert?

To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.

Which command can be use to enable the assertion?

You can use the command-line arguments –ea (for enabling assertions) and –da (for disabling assertions) with other options for classes, packages, and system classes when running the program.

Is assertion enabled by default?

Enabling and Disabling AssertionsBy default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions. Enables or disables assertions in all classes except system classes.


2 Answers

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>
like image 134
jmj Avatar answered Oct 08 '22 03:10

jmj


The only things that worked for me was

export MAVEN_OPTS="-ea"
like image 24
johsin18 Avatar answered Oct 08 '22 02:10

johsin18