简体   繁体   English

如何从pom.xml获取属性值?

[英]how to get property value from pom.xml?

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( 这段代码在运行时给了我null(

ps: there is no settings.xml in this project ps:此项目中没有settings.xml

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" (eg 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. 如果尚未完成,则必须为项目启用Maven筛选。 It is the feature that will look for placeholders inside the files of your project to be replaced by values from the pom. 此功能将查找项目文件中的占位符,以将其替换为pom中的值。 In order to proceed, you need add these lines inside the <build> tag of your pom.xml file. 为了继续,您需要在pom.xml文件的<build>标记内添加这些行。 This is how to do with src/main: 这是使用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文件中添加任意数量的变量,并使用此方法加载每个变量。

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/

The mechanism chiefly in charge to transfer Maven properties to Java application is provided by the Maven Resource Plugin Maven资源插件提供了主要负责将Maven属性转移到Java应用程序的机制。

The plugin is part of the Maven Super Pom and executed during the process-resources phase of the Jar Default Lifecyle . 该插件是Maven Super Pom的一部分,在Jar Default Lifecyleprocess-resources阶段执行。 The only thing you have to do is to active filtering . 您唯一要做的就是主动过滤

This will replace any placeholders, eg ${my.property} in any of the files in src/main/resources with the corresponding property from your pom, eg <property><my.property>test</my.property></property> 这会将src/main/resources中任何文件中的所有占位符(例如${my.property}替换${my.property} 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应用程序可用取决于您-从类路径中读取它就可以了。

String version = project.getProperties().getProperty("getdownload-webapp.version");

项目是MavenProject类型的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM