在Maven配置文件中设置系统变量

28

在maven中是否可以设置一个可以在java类中获取的系统属性?

我看到在surefire插件中可以通过以下方式(here)实现:

String param = System.getProperty("my_parameter1");

<configuration>
    <systemPropertyVariables>
        <my_property1>${my_property1}</my_property1>
    </systemPropertyVariables>
</configuration>

然而,我希望了解我正在工作的环境。我已经将prod或dev作为maven配置文件参数传递,但是否可能以某种方式从代码中处理这个参数?例如,在调用配置文件时设置变量,然后调用system.getProperty方法,或者通过其他方式实现?

谢谢。

我的pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>core</groupId>
    <artifactId>core</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <testSourceDirectory>test</testSourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>2.1</version>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>resources</directory>
                <includes>
                    <include>**/*.png</include>
                </includes>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>1.1.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.facebook4j</groupId>
            <artifactId>facebook4j-core</artifactId>
            <version>[2.0,)</version>
        </dependency>
        <dependency>
            <groupId>com.relayrides</groupId>
            <artifactId>pushy</artifactId>
            <version>0.3</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.2</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.threewks.thundr</groupId>
            <artifactId>thundr-mailgun</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3</version>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>DEV</id>
            <properties>
                <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url>
                <swifte.username>user</swifte.username>
                <swifte.password>pass</swifte.password>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>resources</directory>
                        <includes>
                            <include>JavaPNSDev.p12</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>PROD</id>
            <properties>
                <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url>
                <swifte.username>username</swifte.username>
                <swifte.password>pass</swifte.password>
            </properties>
            <build>
                <resources>
                    <resource>
                        <directory>resources</directory>
                        <includes>
                            <include>JavaPNSProd.p12</include>
                        </includes>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>
</project>
1个回答

32
你应该查看exec-maven-plugin
使用以下配置(注意<systemProperties>)...
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.example.Main</mainClass>
        <arguments>
            <argument>argument1</argument>
        </arguments>
        <systemProperties>
            <systemProperty>
                <key>hello.world</key>
                <value>Hello Stack Overflow!</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>

...以及以下的类...

package com.example;
public class Main {
    public static void main(String[] args) {
        String prop = System.getProperty("hello.world");
        System.out.println(prop);
    }
}

当你运行一个包(注意配置中的阶段 - 如果需要,可以更改,可能是为了安装),它会从键“hello.world”打印出值“Hello Stack Overflow!”。所以基本上,该插件在构建时执行你的程序。
另请参见exec:exec目标。在示例中,我使用了exec:java目标,但两者在功能上有所不同。

exec:exec在单独的进程中执行程序和Java程序。

exec:java在同一VM中执行Java程序。


更新

目前我正在根据我的maven pom文件中的配置文件设置一些属性值。是否可以在配置文件中设置系统属性?因为实际上,我只有一个用于开发和生产的pom文件,并且我需要在配置文件中进行设置。

是的,只需在系统属性元素的<value>元素中使用${property.name}即可。例如:

<profiles>
    <profile>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <id>world</id>
        <properties>
            <hello.world>Hello World!</hello.world>
        </properties>
    </profile>
    <profile>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <id>stack</id>
        <properties>
            <hello.world>Hello Stack Overflow</hello.world>
        </properties>
    </profile>
</profiles>

还有插件 <systemProperties>

<systemProperties>
    <systemProperty>
        <key>hello.world</key>
        <value>${hello.world}</value>
    </systemProperty>
</systemProperties>

只需更改配置文件为stackworld,消息将分别打印Hello Stack OverflowHello World


更新 2

另一个插件是properties-maven-plugin。它已经有一段时间没有进行过更新了,但从一些测试来看,所需的功能已经存在。

它具有set-system-properties目标以及其他一些有用的目标,可以帮助简化属性管理。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
        <execution>
            <!-- any phase before your app deploys -->
            <phase>prepare-package</phase>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
            <configuration>
                <properties>
                    <property>
                        <name>hello.world.two</name>
                        <value>Hello World!</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>

我想使用exec Maven插件,而不是我目前使用的Maven编译器插件,你觉得呢? - Biscuit128
参数不行。但是如果您不指定类,则它将不知道程序的启动点,目标将失败。请记住,插件的目的是执行程序。 - Paul Samsotha
不确定你所说的“called”是什么意思。 - Paul Samsotha
我们在目标中设置属性,在配置文件中以相同的方式引用它,在我的Java类中只需执行system.getproperty(value)即可。 - Biscuit128
你还在聊天室吗? - Biscuit128
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接