简体   繁体   English

无法访问Maven的pom.xml中设置的属性,System.getProperty返回null

[英]Cannot access properties set in Maven's pom.xml, System.getProperty returns always null

I try to set up properties in my pom.xml and access them during my jUnit test. 我尝试在我的pom.xml中设置属性,并在我的jUnit测试期间访问它们。 The class I want to read them in, is imported by the jUnit Test.java and is using String prop = System.getProperty("target1"); 我想要读取它们的类是由jUnit Test.java导入的,并使用String prop = System.getProperty("target1"); but it always is null. 但它总是为空。 What I tried in my pom.xml already: 我在pom.xml中尝试过的内容:

...
<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <target1>2948</target1>
        </properties>
    </profile>
</profiles>

and also 并且

...
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-failsafe-plugin</artifactId>
   <version>2.15</version>
   <systemPropertyVariables>
      <target2>2948</target2>
   </systemPropertyVariables>
   <systemProperties>
     <property>
        <name>target3</name>
        <value>2948</value>
     </property>
   </systemProperties>
...
</configuration>
...     

I do have a parent pom.xml but that can't be the problem can it? 我确实有一个父pom.xml,但这不是问题吗? I'm using Netbeans and starting the Tests by hiting "Test File" on the pom.xml 我正在使用Netbeans并通过在pom.xml上点击“Test File”来启动测试

Thanks yous :) 谢谢你:)

I don't know about maven-failsafe-plugin, but I am able to get the result by adding property and value in maven-surefire-plugin since that is what is used by junit tests in a maven project. 我不知道maven-failsafe-plugin,但我能够通过在maven-surefire-plugin中添加属性和值来获得结果,因为这是maven项目中junit测试所使用的。 Here is a detailed explanation: Specify system property to Maven project 以下是详细说明: 为Maven项目指定系统属性

from the maven site ( http://maven.apache.org/surefire/maven-failsafe-plugin/ ) - "The Failsafe Plugin is designed to run integration tests while the Surefire Plugins is designed to run unit tests". 来自maven网站( http://maven.apache.org/surefire/maven-failsafe-plugin/ ) - “Failsafe插件旨在运行集成测试,而Surefire插件旨在运行单元测试”。 Since you are running a junit, you should add your property to surefire plugin. 由于您运行的是junit,因此应将属性添加到surefire插件。

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

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