【问题标题】:Passing environment variables to config.properties file via maven通过 maven 将环境变量传递给 config.properties 文件
【发布时间】: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>

【问题讨论】:

    标签: java maven


    【解决方案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>
    

    我的配置和你的一样,唯一的区别似乎是这个。 希望这会有所帮助

    【讨论】:

    • 感谢您的回复。顶部的这部分不是 env-dev 或 env-uat 等环境的子项,所以这里的问题是我应该添加哪个 URL?在您创建的示例中,您添加了 dev 的,但是 uat 的呢?
    • 这是变量的默认值:如果在运行项目时使用profile,maven 将使用配置文件部分中的值覆盖该值。我假设如果他没有找到默认值,则无法按预期工作。您是否尝试过此解决方案?
    • 我明白了,以上内容现在说得通了。我只是尝试填充新属性并失败并出现同样的问题。
    • 您确定资源读取正确吗?如果将 config.properties 中的 mosURL=${mosURL} 替换为 mosUrl=https://mos-dev.mywebsiteurlishere.com 是否有效?
    • 想知道它的位置或名称是否为 confg.properties。它看起来在 src/Test/Resources 下的预期位置。
    【解决方案2】:

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

    在与 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 参数等等。

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      相关资源
      最近更新 更多