通过Maven将环境变量传递到config.properties文件

时间:2018-07-31 06:24:22

标签: java maven

我有一个Java config.properties文件,其内容如下:

mosURL=${mosURL}
mopURL=${mopURL}

我想在使用Maven启动构建时将值传递给${mosURL}${mopURL}。这些属性是特定于环境的URL。

下面是我的POM文件,您可以看到我已经为每个环境设置了属性配置文件。 (EDIT。POM示例现在包含在下面的Anser中提到的建议更改。)通过在env-uat部分中显示<activeByDefault>true</activeByDefault>,我将env-uat设置为默认值。

但是当我运行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测试”时,它们并没有传递到我的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>

2 个答案:

答案 0 :(得分:1)

这里的问题应该与变量的“默认”值有关。

尝试在pom顶部的全局属性声明中添加属性

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <properties>
        //add them here aswell
        <mosURL>https://mos-dev.mywebsiteurlishere.com</mosURL>
        <mopURL>https://mop-dev.mywebsiteurlishere.com/</mopURL>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

我和您的配置相同,唯一的区别似乎是此配置。 希望这会有所帮助

答案 1 :(得分:0)

我将其发布为答案,以防将来对偶然发现此问题的人有用。

与CI开发人员讨论后,我们认为在我的框架中保留系统特定的值是错误的方法。属性值最好保存在Maven之外,并让CI环境将其传递给构建。因此,我从POM中删除了所有特定于环境的属性。

然后我用静态返回方法创建了一个单独的类,以保存我的每个配置值,如下所示:

public class Config {

public static String getUrl1() {
    return System.getProperty("url1");
}

public static String getUrl2() {
    return System.getProperty("url2");
}
}

我读取这些属性的代码很简单:

String strUrl1 = Config.getUrl1();

我会从Maven命令行输入实际值,例如:

mvn clean test -Durl1=https://url1goeshere.com/ -Durl2=https://url2goeshere 

因此,这取决于我们的CI开发人员来保留并传递我的特定变量。我们将环境值保存在Octopus中,然后将其传递给TeamCity中的占位符,该占位符然后构造并将命令传递给Maven。因此,当我们通过Octopus部署到Dev时,它将知道在其他环境中传递Dev参数,等等。

相关问题