简体   繁体   English

访问 pom 中定义的 maven 属性

[英]Access maven properties defined in the pom

如何在普通 maven 项目和 maven 插件项目中访问 pom 中定义的 maven 属性?

Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time. 使用properties-maven-plugin在编译时将特定的pom properties写入文件,然后在运行时读取该文件。

In your pom.xml : 在你的pom.xml中

<properties>
     <name>${project.name}</name>
     <version>${project.version}</version>
     <foo>bar</foo>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>write-project-properties</goal>
                    </goals>
                    <configuration>
                        <outputFile>${project.build.outputDirectory}/my.properties</outputFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And then in .java : 然后在.java中

java.io.InputStream is = this.getClass().getResourceAsStream("my.properties");
java.util.Properties p = new Properties();
p.load(is);
String name = p.getProperty("name");
String version = p.getProperty("version");
String foo = p.getProperty("foo");

从Maven设置一个System变量,并在调用后使用java

System.getProperty("Key");

Maven already has a solution to do what you want: Maven已经有了解决方案来做你想做的事:

Get MavenProject from just the POM.xml - pom parser? 从POM.xml获取MavenProject - pom解析器?

btw: first hit at google search ;) 顺便说一下:第一次点击google搜索;)

Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();

try {
     reader = new FileReader(pomfile); // <-- pomfile is your pom.xml
     model = mavenreader.read(reader);
     model.setPomFile(pomfile);
}catch(Exception ex){
     // do something better here
     ex.printStackTrace()
}

MavenProject project = new MavenProject(model);
project.getProperties() // <-- thats what you need

This can be done with standard java properties in combination with the maven-resource-plugin with enabled filtering on properties. 这可以通过标准java属性与maven-resource-plugin结合使用,并启用属性过滤。

For more info see http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html 有关详细信息,请参阅http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

This will work for standard maven project as for plugin projects 这将适用于标准maven项目和插件项目

Update for Leif Gruenwoldt answer : Leif Gruenwoldt答案的更新:

It's important to use使用很重要

this.getClass().getClassLoader().getResourceAsStream("maven.properties");

instead of just而不仅仅是

 this.getClass().getResourceAsStream("maven.properties");

Especially, if you write your maven.properties file right to project.build.outputDirectory :特别是,如果您将maven.properties文件写入project.build.outputDirectory

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>properties-maven-plugin</artifactId>
   <version>1.0.0</version>
   <executions>
      <execution>
         <phase>generate-resources</phase>
         <goals>
            <goal>write-project-properties</goal>
         </goals>
         <configuration>
            <outputFile>${project.build.outputDirectory}/maven.properties</outputFile>
         </configuration>
      </execution>
   </executions>
</plugin>

Explanation is right there .解释就在那里

I use the build-info goal in spring-boot-maven-plugin :我在spring-boot-maven-plugin 中使用build-info目标:

In my pom.xml:在我的 pom.xml 中:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
    ...
      <execution>
        <id>build-info</id>
        <goals>
          <goal>build-info</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
    ...
    </configuration>
</plugin>

And in my code:在我的代码中:

@Autowired
BuildProperties buildProperties;

...

@GetMapping
public Map<String, String> getVersion() {
    return Map.of(
            "build.name", buildProperties.getName(), 
            "build.version", buildProperties.getVersion(), 
            "build.date", buildProperties.getTime().toString());
}

More information about this plugin goal can be found here: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-build-info关于这个插件目标的更多信息可以在这里找到: https : //docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/#goals-build-info

您可以使用JDOM(http://www.jdom.org/)解析pom文件。

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

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