简体   繁体   English

在maven构建的项目中启用断言

[英]Enable assert in a maven built project

I have a java program that is built using Maven and I need to enable the assert keyword. 我有一个使用Maven构建的java程序,我需要启用assert关键字。 Ideally, I'd want to enable assertions in the maven build command. 理想情况下,我想在maven build命令中启用断言。

Maven compiles and builds the java code. Maven编译并构建java代码。 Assertion errors come when you are actually running java code so with maven you can't do it this way 当你实际运行java代码时会出现断言错误,所以使用maven你不能这样做

unless you are using maven plugin to launch java code, you would have to supply -ea to jvm 除非你使用maven插件来启动java代码,否则你必须提供-ea到jvm

exec:java

Pass -ea to commandline argument -ea传递给命令行参数

Surefire 万全

if you meant for test execution then configure sure-fire plugin to pass -ea to jvm 如果你的意思是测试执行,那么配置sure-fire插件将-ea传递给jvm

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <enableAssertions>true</enableAssertions>
    </configuration>
  </plugin>

对我有用的唯一事情是

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. 您无法在启用断言的情况下构建应用程序,因为它们在运行时启用,具体取决于您是否将-ea参数传递给JVM。 Here is configuration of maven exec plugin that enables assertions when running a program: 以下是maven exec插件的配置,它在运行程序时启用断言:

       <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM