Java 在 Maven 配置文件中设置系统变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25604969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting a system variable within a maven profile
提问by Biscuit128
Is it possible within maven to set a system property that is attainable from within a java class.
是否可以在 maven 中设置可从 java 类中获得的系统属性。
I have seen that this is possible (here) within the surefire plugin as follows;
我已经看到这在surefire插件中是可能的(here),如下所示;
String param = System.getProperty("my_parameter1");
<configuration>
<systemPropertyVariables>
<my_property1>${my_property1}</my_property1>
</systemPropertyVariables>
</configuration>
However I would like to get a handle on the environment I am working in, I am already passing prod or dev as a maven profile argument - is it possible somehow to get a handle in the code on this either from setting a variable in the profile i call and then calling system.getProperty or some other way?
但是,我想获得我正在工作的环境的句柄,我已经将 prod 或 dev 作为 Maven 配置文件参数传递 - 是否有可能通过在配置文件中设置变量以某种方式获得代码中的句柄我调用然后调用 system.getProperty 或其他方式?
Thanks
谢谢
my pom file
我的 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.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-annotations</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-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>
采纳答案by Paul Samsotha
You should check out the exec-maven-plugin.
您应该查看exec-maven-plugin。
With the following configuration (notice the <systemProperties>
)...
使用以下配置(注意<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>
...and the following class...
......以及下面的课程......
package com.example;
public class Main {
public static void main(String[] args) {
String prop = System.getProperty("hello.world");
System.out.println(prop);
}
}
...and running a package
(notice the phase in the configuration - you can change if you want, maybe to install), it prints out the value Hello Stack Overflow!
from the key hello.world
. So basically, the plugin executes your program when you build.
...并运行package
(注意配置中的阶段 - 您可以根据需要进行更改,也许可以安装),它会打印出Hello Stack Overflow!
key 中的值hello.world
。所以基本上,插件会在您构建时执行您的程序。
See also the exec:exec
goal. In the example, I used the exec:java
goal, but the two are different in how they function.
另见exec:exec
目标。在示例中,我使用了exec:java
目标,但两者的功能不同。
exec:exec
executes programs and Java programs in a separate process.
exec:java
executes Java programs in the same VM.
exec:exec
在单独的进程中执行程序和 Java 程序。
exec:java
在同一个 VM 中执行 Java 程序。
UPDATE
更新
Currently I am setting some values in properties based on the profile in my maven pom file. Is it possible to set this system property in the profile ? because really, i only have one pom file for dev and prod and its within the profile i would need to set it.
目前,我正在根据我的 maven pom 文件中的配置文件在属性中设置一些值。是否可以在配置文件中设置此系统属性?因为实际上,我只有一个用于 dev 和 prod 的 pom 文件,并且它在我需要设置的配置文件中。
Yes, just use the ${property.name}
in the <value>
element of the system property element. For example:
是的,就使用${property.name}
在<value>
系统属性元素的元素。例如:
<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>
And the plugin <systemProperties>
:
和插件<systemProperties>
:
<systemProperties>
<systemProperty>
<key>hello.world</key>
<value>${hello.world}</value>
</systemProperty>
</systemProperties>
Just by changing the profile, to either stack
or world
, the message will print Hello Stack Overflow
or Hello World
, respectively.
只需将配置文件更改为stack
或world
,消息将分别打印Hello Stack Overflow
或Hello World
。
UPDATE 2
更新 2
Another plugin is the properties-maven-plugin. Nothing's been done on it in a while, but from a few tests, the necessary functionality is there.
另一个插件是properties-maven-plugin。有一段时间没有对它做任何事情,但是从一些测试来看,必要的功能就在那里。
It has a set-system-properties
goal along with some other useful goalsto help ease properties management
它有一个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>