Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过maven将环境变量传递到config.properties文件_Java_Maven - Fatal编程技术网

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

Java 通过maven将环境变量传递到config.properties文件,java,maven,Java,Maven,我有一个java config.properties文件,其内容如下: mosURL=${mosURL} mopURL=${mopURL} 我想在使用Maven启动构建时,将值传递给${mosURL}和${mopURL}。这些属性是特定于环境的URL 下面是我的POM文件,您可以看到我已经为每个环境设置了属性配置文件(EDIT.POM示例现在包含下面Anser中提到的建议更改。)我已将env uat设置为默认值,在env uat部分显示true 但是,当我运行mvn测试时,所有测试都会按预期启

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

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

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

但是,当我运行mvn测试时,所有测试都会按预期启动,但当尝试使用URL时测试失败,我会收到一个错误,通知我没有URL。因此,从pom.xml传递到config.properties的链接在某个地方不起作用

我可以从命令运行“mvn帮助:活动配置文件”,我可以看到以下内容:

The following profiles are active:

 - env-uat (source: com.mycompany.app:my-app:1)
是否有我不知道的缺失环节

编辑:我可以运行
mvn resources:resources
,当我查看target/classes文件夹中生成的.properties文件时,我可以看到所有属性都如我所期望的那样正确列出。但当我运行“mvn测试”时,它们并没有被传递到我的java properties.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>

4.0.0
com.mycompany.app
我的应用程序
1.
https://mos-uat.mywebsiteurlishere.com
https://mop-uat.mywebsiteurlishere.com/
信息杯
黄瓜-java8
1.2.2
测试
信息杯
黄瓜刺柏
1.2.5
测试
org.seleniumhq.selenium
硒服务器
3.5.3
测试
朱尼特
朱尼特
4.12
测试
番石榴
番石榴
22
org.apache.poi
poi
3.17
org.apache.poi
poi ooxml
3.17
org.apache.xmlbeans
xmlbeans xpath
2.3.0
com.microsoft.sqlserver
mssql jdbc
6.5.2.jre9-预览
测试
公地io
公地io
2.4
org.junit.jupiter
JUnitJupiter api
5.1.0
测试
org.apache.maven.plugins
maven资源插件
2.3
UTF-8
org.apache.maven.plugins
maven编译器插件
3.3
1.8
1.8
org.apache.maven.plugins
maven surefire插件
2.22.0
真的
**/*TPOS_Run_All.java
src/main/resources
真的
环境发展
真的
https://mos-dev.mywebsiteurlishere.com
https://mop-dev.mywebsiteurlishere.com/
环境
假的
https://mos-uat.mywebsiteurlishere.com
https://mop-uat.mywebsiteurlishere.com/

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

尝试在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>

4.0.0
com.mycompany.app
我的应用程序
1.
//也在这里添加它们
https://mos-dev.mywebsiteurlishere.com
https://mop-dev.mywebsiteurlishere.com/
UTF-8
我的配置和你的一样,唯一的区别似乎是这个。
希望这有帮助

我将此作为一个答案发布,以防它对将来可能遇到此问题的人有用

在与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 comand的实际值,例如:

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

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

感谢您的回复。顶部的这一部分不是环境(如env dev或env uat)的子部分,因此这里的问题是我应该添加哪个URL?在您创建的示例中,您添加了dev-one,但是uat-one呢?这是变量的默认值:如果在运行项目时使用了
profile
,maven将使用profile部分中的值覆盖该值