Enable assert in a maven built project

送分小仙女□ 提交于 2019-12-05 07:27:31
Jigar Joshi

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>

The only things that worked for me was

export MAVEN_OPTS="-ea"

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!