maven 配置文件无法获取 属性

maven profile can't get the property

请告诉我可能是什么问题?

测试因错误而崩溃: 失败:java.lang.NullPointerException:条目中的空值:url=null

pom.xml:

 <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.21.0</version>
                    <configuration>
                        <systemPropertyVariables>
                            <db.url>https://dev.site.com/</db.url>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

BaseTest.java:

@BeforeClass (alwaysRun = true)
public void setUp() {

    Logger.getLogger("com.dataart.demo.java.logging.SomeClass").log(Level.INFO,System.getProperty("db.url"));

     if (System.getProperty("os.name").toLowerCase().contains("win")) {
         driverPath = "src/test/resources/chromedriver86.exe";
     }
     else {
         driverPath = "src/test/resources/chromedriverlinux86";
     }
    System.setProperty("webdriver.chrome.driver", driverPath);
    LoggingPreferences logs = new LoggingPreferences();
    logs.enable(LogType.BROWSER, Level.SEVERE);
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromePref = new HashMap<>();
    chromePref.put("profile.default_content_settings.popups", 0);
    chromePref.put("download.default_directory", System.getProperty("user.dir"));
    options.setExperimentalOption("prefs", chromePref);
    options.addArguments("headless");
    options.addArguments("window-size=1800,1000");
    options.setCapability(CapabilityType.LOGGING_PREFS, logs);
    options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10L, SECONDS);
    driver.get(System.getProperty("db.url")); // line 71
} 

失败:java.lang.NullPointerException:条目中的空值:url=null

http://joxi.ru/D2PqDR9uJ8PBlA

如果您开始单独测试,您的配置应该有效。 (里面IDE)

但是测试会失败 mvn install, mvn test, ...

Failures: java.lang.NullPointerException: null value in entry: url=null

那你还应该配置surefire-plugin

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <db.url>https://dev.site.com/</db.url>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <db.url>https://dev.site.com/</db.url>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

对于 maven 的观点,我认为你应该查看下面的这个片段(这是从以前的答案派生的):

  1. surefire/failsafe 配置进入 pluginManagement。如果您需要覆盖一个模块的 systemProperty,您可以这样做。如果需要,您还可以 merge/replace 父级配置(请参阅此 Q/A:)。
  2. 两者的配置都将注入系统 属性 db.url,值为 ${test.db.url}
  3. 上述 属性 的值在配置文件中配置。

这旨在避免重复 surefire/failsafe 配置,尤其是当 db.url 不是您需要配置的单个 属性 时。

您也可以使用 Maven 资源插件从过滤后的文件中读取此文件(同时避免使用全局共享上下文,例如:System.getProperty)。我个人认为这是最好的(我更喜欢配置文件而不是系统 属性,前者更容易共享)。

<project>
  <build>
    <pluginManagement>
      <plugins>
        <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> <configuration>
          <systemPropertyVariables>
            <db.url>${test.db.url}</db.url>
          </systemPropertyVariables>
        </configuration> </plugin> 
        <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration>
          <systemPropertyVariables>
            <db.url>${test.db.url}</db.url>
          </systemPropertyVariables>
        </configuration> </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <profile>
    <id>dev</id>
    <activation> <activeByDefault>true</activeByDefault> </activation>
    <properties>
      <test.db.url>https://dev.site.com/</test.db.url>
    </properties>
  </profile>
</project>