Enable assert in a maven built project

馋奶兔 提交于 2020-01-13 09:27:44

问题


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.


回答1:


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>



回答2:


The only things that worked for me was

export MAVEN_OPTS="-ea"



回答3:


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>


来源:https://stackoverflow.com/questions/19966620/enable-assert-in-a-maven-built-project

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