是否可以在构建配置文件中设置环境变量而不是在命令行中设置它们?
例如,我想在使用测试环境 (-Denv=test) 时启用调试器。
我希望 maven 这样做:
export MAVEN_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n"
这样,我可以快速附加调试器,而无需一遍又一遍地键入相同的重复行。
我不相信在这种情况下对我有帮助:
<plugin>
...
<!-- Automatically enable the debugger when running Jetty -->
<argLine>-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n</argLine>
</configuration>
...
</plugin>
沃尔特
最佳答案
在最新版本的 Maven 中,您可以通过运行 mvnDebug 而不是 mvn 来激活调试器,mvnDebug bat/sh 文件设置 MVN__DEBUG_OPTS 并将它们传递给 java.exe .传递的值是:
set MAVEN_DEBUG_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
如果这还不够,这可能会奏效(请注意,我还没有测试过,我会在测试完成后进行更新)。 Maven 读取以“env”为前缀的属性。从环境中,您可以通过使用相同的前缀来设置环境变量。即:
<profile>
<id>dev</id>
<properties>
<env.MAVEN_OPTS>-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000<env.MAVEN_OPTS>
</properties>
</profile>
更新:surefire 插件允许您 specify system properties在测试执行期间使用。配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<systemProperties>
<property>
<name>propertyName</name>
<value>propertyValue</value>
</property>
</systemProperties>
</configuration>
</plugin>
如果这些都不适合您,则可以编写一个在您的配置文件中配置的小插件,该插件绑定(bind)到初始化阶段并设置您的变量。该插件将具有如下配置:
<plugin>
<groupId>name.seller.rich</groupId>
<artifactId>maven-environment-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<id>set-properties</id>
<phase>initialize</phase>
<goals>
<goal>set-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<properties>
<env.MAVEN_OPTS>-Xdebug -Xnoagent -Djava.compiler=NONE
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000<env.MAVEN_OPTS>
</properties>
</configuration>
</plugin>
在执行期间,插件将使用 System.setProperty() 设置每个传递的属性。如果前两个不合适或不起作用,这应该可以解决您的问题。
关于java - Maven 命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1275624/