访问pom中定义的maven属性

Dav*_*Dai 74 java maven

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

Lei*_*ldt 71

使用properties-maven-pluginproperties在编译时将特定的pom 写入文件,然后在运行时读取该文件.

在你的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>
Run Code Online (Sandbox Code Playgroud)

然后在.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");
Run Code Online (Sandbox Code Playgroud)

  • 现在有一个稳定的1.0.0版本 (2认同)

San*_*osh 21

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

System.getProperty("Key");
Run Code Online (Sandbox Code Playgroud)

  • 这只有在您从Maven运行生成的构建时才有效,可能使用maven-exec-plugin.如果您只是使用Maven编译代码,这将无法正常工作. (5认同)

Vol*_*ozo 7

Leif Gruenwoldt 答案的更新:

使用很重要

this.getClass().getClassLoader().getResourceAsStream("maven.properties");
Run Code Online (Sandbox Code Playgroud)

而不是仅仅

 this.getClass().getResourceAsStream("maven.properties");
Run Code Online (Sandbox Code Playgroud)

特别是,如果您将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>
Run Code Online (Sandbox Code Playgroud)

解释就在那里


SWo*_*ste 6

Maven已经有了解决方案来做你想做的事:

从POM.xml获取MavenProject - pom解析器?

顺便说一下:第一次点击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
Run Code Online (Sandbox Code Playgroud)

  • 这真是一个非常糟糕的做法,只需使用maven资源插件! (2认同)
  • 但前提是你假设他想要/可以改变他的 pom(s)。我同意你的看法,直接在 pom 文件而不是外部文件中拥有属性可能是不好的。可以使用 java 属性类读取该文件 - 当然。但在另一种情况下呢?那么 maven-model 是最好的解决方案......但在 David Dai 不告诉我们他到底想做什么之前,我们都只是猜测。 (2认同)

Mar*_*ker 6

这可以使用标准java属性结合maven-resource-plugin启用的属性过滤来完成.

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

这将适用于标准maven项目和插件项目


ali*_*n01 -8

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