【问题标题】:set systemProperty in exec-maven-plugin does not work在 exec-maven-plugin 中设置 systemProperty 不起作用
【发布时间】:2016-08-03 02:57:53
【问题描述】:

这是我的 pom.xml 文件:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <profiles>
        <profile>
            <id>my_proj</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.4.0</version>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>

                            <executable>java</executable>
                            <arguments>
                                <argument>-classpath</argument>
                                <classpath />
                                <argument>com.test.Main</argument>

                            </arguments>
                            <systemProperties>
                                <systemProperty>
                                    <key>someKey</key>
                                    <value>someValue</value>
                                </systemProperty>
                            </systemProperties>
                            <environmentVariables>
                                <someKey>
                                    someValue
                                </someKey>
                            </environmentVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

        </profile>
    </profiles>

</project>

在 Main.java 中

public static void main(String[] args) {

    System.out.println("hello world" + System.getenv("someKey") + " " + System.getProperty("someKey"));
}

我运行时的输出

mvn install -Pmy_proj

hello worldsomeValue null

我似乎无法获得 systemProperty 值。我做错了什么?

【问题讨论】:

    标签: java maven maven-3 pom.xml exec-maven-plugin


    【解决方案1】:

    systemProperty 不起作用仅仅是因为它不是 exec-maven-pluginexec 目标的预期元素。

    查看官方exec goal page,没有指定systemProperties元素。因此,您的配置对 Maven 仍然有效,因为它是格式良好的 XML,但它会被 exec-maven-plugin 忽略。

    来自官方Maven Pom Reference关于插件configuration元素:

    请注意,所有配置元素,无论它们在 POM 中的何处,都旨在将值传递给另一个底层系统,例如插件。换句话说:配置元素中的值永远不会被 POM 模式明确要求,但插件目标完全有权要求配置值。


    您正在混淆其java 目标所预见的systemProperties 配置条目。这个选项在那里可用是因为它的上下文:它是为 java 执行而设计的。另一方面,exec 目标更加通用,因此无法预见只有 java 程序需要的选项。

    要通过exec 目标将系统属性传递给Java 执行,您可以使用arguments 配置条目并使用-D notation

    -Dproperty=value 设置系统属性值。

    进一步说明,根据官方 Running Java programs with the exec goal 文档,-D 参数应该放在第一位:

    <configuration>
        <executable>java</executable>
        <arguments>
            <argument>-DsomeKey2=someValue2</argument>
            <argument>-classpath</argument>
            <classpath />
            <argument>com.test.Main</argument>
        </arguments>
        <environmentVariables>
            <someKey>someValue</someKey>
        </environmentVariables>
    </configuration>
    

    另外,你不应该为环境和系统属性设置相同的变量名,否则系统属性将不会被设置。

    【讨论】:

    • 感谢它现在有效。我编辑了评论并将您的答案标记为正确
    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 2016-12-29
    • 2012-03-13
    • 2014-10-15
    • 2011-01-12
    • 2019-04-11
    • 2019-08-08
    • 1970-01-01
    相关资源
    最近更新 更多