[英]How to read system property from maven in java
我正在運行在tomcat服務器上的Web應用程序。 我為不同的環境使用了不同的屬性文件,因此我想從java文件中的pom文件讀取環境變量,並在運行命令mvn clean install
時設置該環境屬性。
這是我的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>marketingcenter</groupId>
<artifactId>marketingcenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<profiles>
<!-- The configuration of the development profile -->
<profile>
<id>dev</id>
<!-- The development profile is active by default -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment.id>environment_dev</environment.id>
</properties>
</profile>
<!-- The configuration of the production profile -->
<profile>
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment.id>environment_prod</environment.id>
</properties>
</profile>
<!-- The configuration of the testing profile -->
<profile>
<id>qa</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment.id>environment_qa</environment.id>
</properties>
</profile>
<profile>
<id>reg</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment.id>environment_reg</environment.id>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<!-- <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>environment</name>
<value>environment_dev</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin> -->
<!-- <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.keurig.config.DBUtils</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
<systemProperties>
<systemProperty>
<key>environment</key>
<value>environment_dev</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<systemPropertyVariables>
<environment>environment_dev</environment>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我嘗試了多處評論,但都給出相同問題的null值。 我正在通過遵循DBUtils類中的代碼來讀取環境屬性-
public class DBUtils {
private static final Logger LOG=Logger.getLogger(DBUtils.class);
public static Connection getConnection() throws SQLException {
Connection connection = null;
String str =System.getProperty("environment");
System.out.println("deven"+str);
//static Properties _config = PropertyLoader.loadProperties(System.getProperty("environment"));
Properties prop = PropertyLoader.loadProperties(str);
String url = prop.getProperty("databaseUrl");
String driverName = prop.getProperty("databaseDriver");
}
}
但我每次都變得虛無。 這是結果-
devennull
devennull
2018-04-20 12:52:22,424 [http-nio-8080-exec-8] ERROR
java.lang.IllegalArgumentException: null input: name
at com.app.data.PropertyLoader.loadProperties(PropertyLoader.java:51)
at com.app.data.PropertyLoader.loadProperties(PropertyLoader.java:128)
at com.app.data.DBUtils.getConnection(DBUtils.java:23)
據我所知,在maven階段要使用main方法執行Java類時,可以使用該插件。 不會將系統屬性傳遞給保存Web應用程序執行的JVM。
如果在DBUtils類中設置void main,則將在具有系統屬性的JVM中執行此操作,從而看到系統屬性。
public static void main(String[] args) {
String k = System.getProperty("environment");
System.out.println(k);
}
最簡單的方法-使用中間app.properties
文件:例如:
1-具有environment.server.name
變量的概要文件的Maven代碼段:
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment.server.name>DEVELOPMENT</environment.server.name>
</properties>
</profile>
2-在app.properties
文件中(如果不存在則創建。通常在src/main/resources
),我們使用簡單的映射:
environment.server.name=${environment.server.name}
3-創建將處理應用程序屬性的簡單類:
public class AppProperties {
private static final String PROPERTIES_FILE_NAME = "app.properties";
public static final String SERVER_NAME;
static {
try {
final PropertiesConfiguration configuration = new
PropertiesConfiguration(PROPERTIES_FILE_NAME);
SERVER_NAME = configuration.getString("environment.server.name");
} catch (ConfigurationException e) {
throw new RuntimeException("Failed to load properties", e);
}
}
private AppProperties() {}
}
4-您現在可以在應用程序中使用屬性文件:
String serverName = AppProperties.SERVER_NAME;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.