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

Spring 如何创建maven配置文件以设置系统属性,spring,maven,spring-boot,maven-3,Spring,Maven,Spring Boot,Maven 3,我需要创建2个maven配置文件,在这里我可以设置两个不同的系统属性。这些配置文件只用于设置系统属性,与任何插件无关 像 profile1设置系统属性1 .... 设置系统属性1 profile2设置系统属性2 .... 设置系统属性2 您可以,但这取决于您需要它做什么。这是最常见的方法: <profiles> <profile> <id>profile-1</id> <build>

我需要创建2个maven配置文件,在这里我可以设置两个不同的系统属性。这些配置文件只用于设置系统属性,与任何插件无关


profile1设置系统属性1
.... 设置系统属性1
profile2设置系统属性2
.... 设置系统属性2

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

  <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>
您需要使用
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!
在没有编译目标的情况下运行它将给您一个
null
,因为
exec
插件没有绑定到此实例中的任何构建阶段

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

可能是
package org.example;

public class App {
    public static void main( String[] args)      {
        System.out.println("-->" + System.getProperty("my-prop"));
    }
}
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) @ sys-prop ---
-->Yabadabadoo!