【发布时间】:2017-10-28 01:42:31
【问题描述】:
我在 Netbeans 中设置了一个基本的 maven Java 项目。我在POM.xml 文件中创建了一个新属性,并尝试在我的代码中读取它。但是,我不断得到一个空值而不是值。我做错了什么?
POM.XML:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.test</groupId>
<artifactId>Test</artifactId>
<version>1.0</version>
</parent>
<artifactId>Test</artifactId>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<system>ABC</system>
</properties>
</project>
代码:
package com.test.Test;
public class Test {
public static void main(String[] args) {
System.out.println("Creating connection to " + System.getProperty("system") + "...");
}
}
【问题讨论】:
-
因为它们是 maven 属性,而不是系统属性。它们仅在 maven 构建期间有效,在程序运行时无效。
-
为什么需要从 Maven 属性中读取它们?应该在测试期间还是在应用程序运行期间使用它们?