简体   繁体   中英

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.

In your 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.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:

Get MavenProject from just the POM.xml - pom parser?

btw: first hit at google search ;)

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.

For more info see http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

This will work for standard maven project as for plugin projects

Update for Leif Gruenwoldt answer :

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 :

<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 :

In my 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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