Java 在maven项目中,系统属性始终为空

Java 在maven项目中,系统属性始终为空,java,maven,maven-3,maven-plugin,Java,Maven,Maven 3,Maven Plugin,我的实用课 Properties prop = new Properties(); ClassLoader loader = Thread.currentThread().getContextClassLoader(); String profile = System.getProperty("env"); //InputStream in = loader.getResourceAsStream(fileName + ".properties")

我的实用课

Properties prop = new Properties();
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        String profile = System.getProperty("env");
        //InputStream in = loader.getResourceAsStream(fileName + ".properties");
        InputStream in = loader.getResourceAsStream(fileName + "_" + profile + ".properties");
始终返回null

我确实在pom中添加了属性maven插件

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>set-system-properties</goal>
                    </goals>
                    <configuration>
                        <properties>
                            <property>
                                <name>env</name>
                                <value>prod</value>
                            </property>
                        </properties>
                    </configuration>
                </execution>
            </executions>
        </plugin>

org.codehaus.mojo
属性maven插件
1.0.0
设置系统属性
环境
戳

但是仍然无法在util类中获得属性值。

据我所知,设置系统属性目标的文档,它在当前机器上设置环境变量。根据它是如何定义的,这些环境变量可以仅在构建期间定义,也可以仅为当前用户定义,或者,如果您还拥有当前计算机的管理员权限的话

在任何情况下,它只会在构建项目的机器上设置变量

如果我正确理解了您的问题,您希望存储生成是
发行版
(生产版)还是
调试版
生成

在这种情况下,您可以使用
maven汇编插件
并定义清单值。这个清单值将被写入jar的清单中,您可以在运行时读取该清单,例如。G使用JCabi清单库。

您尝试过这个吗

<systemProperties>
    <systemProperty>
        <name>env</name>
        <value>prod</value>
    </systemProperty>
</systemProperties>

环境
戳

作为旁注-如果在看到错误时使用surefire运行此程序,则可能应该使用其内置的系统属性功能:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <systemPropertyVariables>
            <env>prod</env>
        </systemPropertyVariables>
    </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
戳

String env=System.getProperty(“env”):代码在哪里,何时运行?你运行哪个Maven目标?@MoritzPetersen是的。不走运