如何创建maven配置文件以设置系统属性

时间:2015-11-05 23:28:56

标签: spring maven spring-boot maven-3

我需要创建2个maven配置文件,我可以在其中设置两个不同的系统属性。这些配置文件只是设置系统属性而不涉及任何插件。

<profile>
   <id> profile1 to set system property 1</id>
   .... set system property1
</profile>
<profile>
   <id> profile2 to set system property 2</id>
   .... set system property2
</profile>

1 个答案:

答案 0 :(得分:3)

你可以,但这取决于你需要它。这是最常见的做法:

  <profiles>
    <profile>
      <id>profile-1</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0-alpha-2</version>
            <executions>
              <execution>
                <goals>
                  <goal>set-system-properties</goal>
                </goals>
                <configuration>
                  <properties>
                    <my-prop>Yabadabadoo!</my-prop>
                  </properties>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

但这仅在Maven的执行期间设置系统属性,所以如果你想(例如)这个类拿起它:

package org.example;

public class App {
    public static void main( String[] args)      {
        System.out.println("-->" + System.getProperty("my-prop"));
    }
}

您需要使用mvn -P profile-1 compile exec:java -Dexec.mainClass=org.example.App运行它,它将产生以下结果:

[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ sys-prop ---
-->Yabadabadoo!

在没有compile目标的情况下运行它将为您提供null,因为exec插件未绑定到此实例中的任何构建阶段。

但是如果你需要(比如说)单元测试的系统属性,那么surefire插件就是你需要的。

相关问题