---
title: "Maven: 如何在properties文件中获取maven的 pom.xml 的键值"
author: "usench"
date: "2016-08-26T15:39:04Z"
tags: []
categories: ["开源软件"]
slug: "maven-properties-maven-pom-xml"
draft: false
lastmod: "2016-08-26T15:39:04Z"
id: 2320368
url: "https://usench.iteye.com/blog/2320368"
---

原文链接：http://blog.csdn.net/bigtree\_3721/article/details/51898095

 

 

```
如何在properties文件中获取maven的 pom.xml 的键值？
```

```
答案就是：在资源文件(*.properties)中放置pom.xml预先设置的变量, 在执行mvn package时就会自动将变量替换为真实值
例如:
1. 我们在src\main\resources\application.properties放置如下内容
     version=${project.version}
2. 配置pom.xml让Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
3. 执行mvn package
    查看生成的jar文件中的application.properties, version值已经变成了pom.xml中的version

因此我们只需使用java读取application.properties即可
```