【发布时间】:2018-09-30 17:26:14
【问题描述】:
我正在开发一个在 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>
我已经尝试了多件被评论的事情,但都给出了相同的问题空值。 我正在通过以下 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)
【问题讨论】:
-
这个解决方案不适合我
-
您能在 DBUtils 中发布您的 void main 方法吗?
-
@kimy82 我没有任何主要方法。它是一个网络应用程序
-
我建议你可以看看maven.apache.org/guides/mini/…
标签: java maven system-properties