通过Maven将环境变量传递到config.properties文件 [英] Passing environment variables to config.properties file via maven

查看:126
本文介绍了通过Maven将环境变量传递到config.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a java config.properties file which reads as follows:

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

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

I am wanting to pass in the values to ${mosURL} and ${mopURL} when I initiate my build with Maven. These properties are environment specific URL's.

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

My POM file is below and you can see I have set up property profiles for each environment. (EDIT. POM Example now contains suggestted changes mentioned in Anser below.) I have set env-uat as my default by having <activeByDefault>true</activeByDefault> present in the env-uat section.

但是当我运行mvn test时,一切都按预期开始,但是当尝试使用URL时测试失败时,我收到一条错误消息,通知我没有URL.因此,从pom.xml传递到config.properties的链接不起作用.

But when I run mvn test everyting starts off as expected but when the test fails when attempting to use the URL and I get an error to inform me that no URL is present. So somewhere the link of passing from pom.xml into config.properties isnt working.

我可以从命令中运行"mvn help:active-profiles",并且可以看到以下内容:

I can run "mvn help:active-profiles" from command and I can see the following:

The following profiles are active:

 - env-uat (source: com.mycompany.app:my-app:1)

是否存在我不知道的丢失链接?

Is there a missing link that I dont know about?

我可以运行mvn resources:resources,当我查看target/classes文件夹中生成的.properties文件时,可以看到所有属性均按预期正确列出.但是当我运行"mvn test"时,它们并没有传递到我的java propeties.config文件中.

I can run mvn resources:resources and when I look at the generated .properties file in the target/classes folder I can see that all the properties are listed correctly as I would expect. But they are not being passed into my java propeties.config file when I run 'mvn test'.

我已经开始阅读有关Spring的文章,并且想知道是否需要使用Spring进行配置以将这些值从Maven导入到Java文件中?属性的Maven人口看起来不错.

I have started reading about Spring and wondering if I need to configure something with Spring to get these values form Maven into my Java file? The Maven population of the properties look fine.

<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>

推荐答案

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

I'm posting this as an answer in case it's useful to anyone who may stumble across this in future.

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

After a discussion with the CI developer we decided that holding system specific values in my framework was the wrong approach. The property values are best held outside of Maven and let the CI environment pass them in on build. So I stripped out all environment specific properties from my POM.

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

I then created a separate class with static return methods to hold each of my config values such as below:

public class Config {

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

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

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

My code that would read these properties would be as simple as:

String strUrl1 = Config.getUrl1();

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

I would pass in the actual value from Maven comand such as:

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

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

So it is then up to our CI developers to hold and pass in my specific variables. We are holding the environment values in Octopus which will then pass it to placeholders in TeamCity that will then construct and pass a command to Maven. So when we deploy to Dev through Octopus it will know to pass in the Dev parameters and so on for other environments.

这篇关于通过Maven将环境变量传递到config.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆