如何通过java代码访问配置文件(在pom.xml中)中设置的值

DD1*_*DD1 5 java xml maven

我有一个maven项目,在pom.xml中设置了不同的配置文件,具有不同的值.但我不知道如何通过java代码访问配置文件中设置的值.例如-

我的pom.xml:

<profile>
        <id>scaler</id>
        <properties>
            <user>xxxxxxx</user>
            <secret>yyyyyyyy</secret>
            <proxyHost>172.19.17.13</proxyHost>
            <proxyPort>9444</proxyPort>
            <environment>SCALER</environment>
        </properties>
    </profile>
Run Code Online (Sandbox Code Playgroud)

Java代码 -

String serviceurl = "http://"<proxyhost>":<proxyPort>/";
Run Code Online (Sandbox Code Playgroud)

在上面的java代码中,我想使用代理主机作为172.19.17.13&port为9444,如pom.xml中定义但是如何从pom中访问这些值?我将感谢你的帮助

小智 5

您应该使用 Maven 过滤功能。

http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

只需在 src/main/resources 中添加一个带有一些占位符的属性文件:

key=${myvalue}
Run Code Online (Sandbox Code Playgroud)

thenmyvalue应该被定义为 pom.xml 中的一个属性

请务必激活您的资源过滤器:

<resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
</resource>
Run Code Online (Sandbox Code Playgroud)


Vit*_*sok 0

我不确定这取决于 Maven 配置文件。您可以尝试使用properties-maven-plugin(或其他解决方案),如此处所述。只是将属性写入文件,然后在 java 代码中使用它。