我正在尝试根据配置文件通过maven设置一些系统环境变量。 所以我的pom代码是
<profile>
<id>dev</id>
<activation>
<property>
<name>com.xxx.profile</name>
<value>dev</value>
</property>
</activation>
<properties>
<db-url>jdbc:postgresql://abc</db-url>
<db-username>xxx</db-username>
<db-pwd>yy</db-pwd>
</properties>
</profile>
所以当我构建项目时,我会这样做 mvn clean install -Dcom.xxx.profile = dev
在代码中,我有
String urlDB = System.getProperty("db-url");
String username = System.getProperty("db-username");
String password = System.getProperty("db-pwd");
但是当我运行它时,所有这些变量都是空的。
System.getProperty(“com.xxx.profile”)确实给出了正确答案....
想法?
答案 0 :(得分:1)
Maven profiles documentaion表示激活部分包含触发配置文件的条件,例如您需要系统属性&#34; com.xxx.profile&#34;有价值&#34; dev&#34;。基于此,您可以判断System.getProperty(&#34; com.xxx.profile&#34;)将返回正确,因为此值已存在于您的系统中(自配置文件触发)。
继续类似问题Using the properties tag within maven profiles。基本上属性&#39;您定义的值将用于在过滤资源的过程中替换$ {propertiesKeys}。因此,通过使用属性,您不会定义新的系统属性。 必需的pom设置(您要解析的.properties文件在哪里):
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
属性文件示例:
database.driverClassName=${database.driverClassName}
database.url=${database.url}
虽然那时你不会读取系统属性而是属性文件。
properties-maven-plugin我想它确实是你想要的。查看&#34; set-system-properties&#34;在使用文档的底部。您将找到可与配置文件一起使用的代码。这是一个例子:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<goals>
<goal>set-system-properties</goal>
</goals>
<configuration>
<properties>
<property>
<name>db-url</name>
<value>${db-url}</value>
</property>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
答案 1 :(得分:0)
<properties>
<env>dev</env>
</properties>
<profiles>
<profile>
<id>dev</id>
<properties>
<AppName>dev-${project.artifactId}</AppName>
<build.profile.id>dev</build.profile.id>
<spring.profiles.active>dev</spring.profiles.active>
<build.outputDirectory>target</build.outputDirectory>
<wildfly.plugin.clean>clean</wildfly.plugin.clean>
<wildfly.plugin.install>install</wildfly.plugin.install>
<env>dev</env>
</properties>
</profile>
<profile>
<id>sit</id>
<properties>
<AppName>sit-${project.artifactId}</AppName>
<build.profile.id>sit</build.profile.id>
<spring.profiles.active>sit</spring.profiles.active>
<build.outputDirectory>deployment/sit</build.outputDirectory>
<wildfly.plugin.clean>none</wildfly.plugin.clean>
<wildfly.plugin.install>none</wildfly.plugin.install>
<env>sit</env>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<AppName>uat-${project.artifactId}</AppName>
<build.profile.id>uat</build.profile.id>
<spring.profiles.active>uat</spring.profiles.active>
<build.outputDirectory>deployment/uat</build.outputDirectory>
<wildfly.plugin.clean>none</wildfly.plugin.clean>
<wildfly.plugin.install>none</wildfly.plugin.install>
<env>uat</env>
</properties>
</profile>
<profile>
<id>prd</id>
<properties>
<AppName>prod-${project.artifactId}</AppName>
<build.profile.id>prd</build.profile.id>
<spring.profiles.active>prd</spring.profiles.active>
<build.outputDirectory>deployment/prd</build.outputDirectory>
<wildfly.plugin.clean>none</wildfly.plugin.clean>
<wildfly.plugin.install>none</wildfly.plugin.install>
<env>prd</env>
</properties>
</profile>
</profiles>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>**********************${build.profile.id} ** IS RUNNING</echo>
</tasks>
<tasks>
<echo>*********************${build.profile.id} **COPYING
PROPERTIES</echo>
<delete file="src/main/resources/application.properties" />
<delete file="src/main/resources/log4j2.xml" />
<copy
file="src/main/resources/${build.profile.id}/application.properties"
tofile="src/main/resources/application.properties" />
<copy file="src/main/resources/${build.profile.id}/log4j2.xml"
tofile="src/main/resources/log4j2.xml" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>