本文介绍了如何从java Code获取Maven项目BaseDir()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从我的Java代码中获取Maven project basedir()?

How to get Maven project basedir() from my Java code?

推荐答案

根据,有一个maven属性 $ {basedir}

According to Codehaus, there's a maven property ${basedir}

如果您在资源中包含属性文件,该文件具有 $ {basedir} 占位符,并且,你会发现有一个构建时间替换将basedir放入属性文件中。然后,您可以使用代码中的属性实例加载它。

If you include a properties file in your resources, which has the ${basedir} placeholder, and enable filtering for the resources plugin, you will find that there's a build time substitution of the basedir into the properties file. You can then load this using a Properties instance in code.

在/ src / main / resources中,创建一个名为project.properties的文件,包含

in /src/main/resources, create a file called project.properties, containing

project.basedir=${basedir}

然后,在POM中,启用对/ src / main / resources的过滤,如上面链接的maven资源过滤文档中所述。

Then, in the POM, enable filtering for /src/main/resources, as outlined in the maven resources filtering documentation linked above.

然后,在代码中,在运行时,将属性文件加载到属性实例

Then, in code, at runtime, load the properties file into a Properties instance

Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("project.properties"));
String basedir = props.get("project.basedir");

另一种方法是处理一些源文件并通过挂钩到<$ c来替换它们$ c> process-sources 阶段,但这不太容易解释。

An alternative would be to process some source files and do substitution in there by hooking into the process-sources phase, but that's not likely to be easy to explain.

这篇关于如何从java Code获取Maven项目BaseDir()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 04:37