![](/img/trans.png)
[英]how to access properties defined in other module pom in maven multimodule project
[英]Access maven properties defined in the pom
如何在普通 maven 項目和 maven 插件項目中訪問 pom 中定義的 maven 屬性?
使用properties-maven-plugin在編譯時將特定的pom properties
寫入文件,然后在運行時讀取該文件。
在你的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>
然后在.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已經有了解決方案來做你想做的事:
從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
這可以通過標准java屬性與maven-resource-plugin
結合使用,並啟用屬性過濾。
有關詳細信息,請參閱http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
這將適用於標准maven項目和插件項目
使用很重要
this.getClass().getClassLoader().getResourceAsStream("maven.properties");
而不僅僅是
this.getClass().getResourceAsStream("maven.properties");
特別是,如果您將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>
解釋就在那里。
我在spring-boot-maven-plugin 中使用build-info目標:
在我的 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>
在我的代碼中:
@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());
}
關於這個插件目標的更多信息可以在這里找到: 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.