【发布时间】:2019-01-07 10:53:24
【问题描述】:
我有一个 java config.properties 文件,内容如下:
mosURL=${mosURL}
mopURL=${mopURL}
当我使用 Maven 启动构建时,我想将值传递给 ${mosURL} 和 ${mopURL}。这些属性是特定于环境的 URL。
我的 POM 文件在下面,您可以看到我已经为每个环境设置了属性配置文件。 (编辑。POM 示例现在包含下面 Anser 中提到的建议更改。)我已将 env-uat 设置为我的默认设置,方法是在 env-uat 部分中显示 <activeByDefault>true</activeByDefault>。
但是当我运行 mvn test 时,一切都按预期开始,但是当尝试使用 URL 时测试失败并且我收到错误消息,通知我没有 URL 存在。因此,从 pom.xml 传递到 config.properties 的链接在某处不起作用。
我可以从命令运行“mvn help:active-profiles”,我可以看到以下内容:
The following profiles are active:
- env-uat (source: com.mycompany.app:my-app:1)
是否缺少我不知道的链接?
编辑:我可以运行 mvn resources:resources,当我查看 target/classes 文件夹中生成的 .properties 文件时,我可以看到所有属性都按照我的预期正确列出。但是当我运行“mvn test”时,它们并没有被传递到我的 java propeties.config 文件中。
我已经开始阅读有关 Spring 的内容并想知道是否需要使用 Spring 进行配置以将这些值从 Maven 获取到我的 Java 文件中?属性的 Maven 群体看起来不错。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<properties>
<mosURL>https://mos-uat.mywebsiteurlishere.com</mosURL>
<mopURL>https://mop-uat.mywebsiteurlishere.com/</mopURL>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans-xpath</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.5.2.jre9-preview</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- specify UTF-8, ISO-8859-1 or any other file encoding -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<exclude>**/*TPOS_Run_All.java</exclude>
</includes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>env-dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<mosURL>https://mos-dev.mywebsiteurlishere.com</mosURL>
<mopURL>https://mop-dev.mywebsiteurlishere.com/</mopURL>
</properties>
</profile>
<profile>
<id>env-uat</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<mosURL>https://mos-uat.mywebsiteurlishere.com</mosURL>
<mopURL>https://mop-uat.mywebsiteurlishere.com/</mopURL>
</properties>
</profile>
</profiles>
</project>
【问题讨论】: