java 如何从 pom.xml 获取属性值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26404343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get property value from pom.xml?
提问by curiousity
I have added a node in my pom.xml:
我在 pom.xml 中添加了一个节点:
<properties>
<getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>
how could I get this 1.5 value in code?
我怎么能在代码中得到这个 1.5 值?
String version = System.getProperty("getdownload-webapp.version"); // output version = null
This code gave me null while running(
这段代码在运行时给了我空值(
ps: there is no settings.xml in this project
ps:这个项目中没有settings.xml
回答by awwsmm
So you have a property like this.
所以你有这样的财产。
<properties>
<getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>
Create a file as follows in your Maven project.
在您的 Maven 项目中创建如下文件。
src/main/resources/project.properties
Or as follows if it is for tests only.
如果仅用于测试,则如下所示。
src/test/resources/project.properties
Add this line inside the new file. Please note that you should not prefix with "properties" (e.g. don't write "properties.getdownload-webapp.version").
在新文件中添加这一行。请注意,您不应该使用“properties”作为前缀(例如不要写“properties.getdownload-webapp.version”)。
version=${getdownload-webapp.version}
Note that you can also add flags like this to the file.
请注意,您还可以将这样的标志添加到文件中。
debug=false
If not already done, you have to enable Maven filtering for your project. It is the feature that will look for placeholders inside the files of your project to be replaced by values from the pom. In order to proceed, you need add these lines inside the <build>
tag of your pom.xml file. This is how to do with src/main:
如果尚未完成,您必须为您的项目启用 Maven 过滤。该功能将在您的项目文件中查找占位符以替换为来自 pom.xml 的值。为了继续,您需要将这些行<build>
添加到 pom.xml 文件的标记中。这是如何处理 src/main:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
And here is how to do for src/test:
这是 src/test 的方法:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
Finally, in your source code (MyClassName.java
), add a block like
最后,在您的源代码 ( MyClassName.java
) 中,添加一个块,如
Properties props = new Properties();
props.load(MyClassName.class.getClassLoader().getResourceAsStream("project.properties"));
String version = props.getProperty("version");
You can add as many variables as you want to the project.properties
file and load each one using this method.
您可以向project.properties
文件中添加任意数量的变量,并使用此方法加载每个变量。
回答by Fritz Duchardt
The mechanism chiefly in charge to transfer Maven properties to Java application is provided by the Maven Resource Plugin
Maven Resource Plugin提供了主要负责将Maven 属性传递给Java 应用程序的机制
The plugin is part of the Maven Super Pomand executed during the process-resources
phase of the Jar Default Lifecyle. The only thing you have to do is to active filtering.
该插件是Maven Super Pom 的一部分process-resources
,在Jar Default Lifecyle阶段执行。您唯一需要做的就是主动过滤。
This will replace any placeholders, e.g. ${my.property}
in any of the files in src/main/resources
with the corresponding property from your pom, e.g. <property><my.property>test</my.property></property>
这将替换任何占位符,例如${my.property}
在src/main/resources
您的 pom中具有相应属性的任何文件中,例如<property><my.property>test</my.property></property>
How you make this property then available to your Java application is up to you - reading it from the classpath would work.
如何使此属性可用于 Java 应用程序取决于您 - 从类路径中读取它会起作用。
回答by Nadir
I assume you want to get it in the code to check something right? You can use filtering from maven that will inject the value in the source code, similar to the filtering option http://mojo.codehaus.org/templating-maven-plugin/
我假设您想在代码中获取它以检查正确的内容?您可以使用来自 maven 的过滤,它将在源代码中注入值,类似于过滤选项 http://mojo.codehaus.org/templating-maven-plugin/
回答by Bradley Smith
String version = project.getProperties().getProperty("getdownload-webapp.version");
Where project is of type MavenProject
其中项目是 MavenProject 类型