在exec-maven-plugin中设置systemProperty不起作用

Tra*_*ang 2 java pom.xml maven-3 maven exec-maven-plugin

这是我的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>
Run Code Online (Sandbox Code Playgroud)

并在Main.java中

public static void main(String[] args) {

    System.out.println("hello world" + System.getenv("someKey") + " " + System.getProperty("someKey"));
}
Run Code Online (Sandbox Code Playgroud)

我运行时的输出

mvn install -Pmy_proj
Run Code Online (Sandbox Code Playgroud)

hello worldsomeValue null
Run Code Online (Sandbox Code Playgroud)

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

A_D*_*teo 8

systemProperty不起作用仅仅因为它不是exec目标的预期要素exec-maven-plugin.

检查官方exec目标页面,未systemProperties指定任何元素.因此,您的配置对于Maven仍然有效,因为它是格式良好的XML,但它被忽略了exec-maven-plugin.

从关于插件元素的官方Maven Pom Referenceconfiguration:

值得注意的是,所有配置元素(无论它们位于POM中)都旨在将值传递给另一个底层系统,例如插件.换句话说:POM模式从不明确要求配置元素中的值,但插件目标完全有权要求配置值.


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

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

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

进一步说明,根据官方运行Java程序和exec目标文档,-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>
Run Code Online (Sandbox Code Playgroud)

此外,您不应为环境和系统属性设置相同的变量名,否则不会设置系统属性.