How to get Maven BaseDir () project from java code

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

+4
source share
3 answers

According to Codehaus , the maven ${basedir} property exists

If you include a property file in your resources that has a ${basedir} placeholder, and enable filtering for the resource plugin , you will find that there is a temporary substitution of the database in the properties file. You can then load it using the Properties instance in the code.

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

 project.basedir=${basedir} 

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

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

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

An alternative would be to process some source files and make a replacement there by connecting to the process-sources phase, but this is unlikely to be easy to explain.

+5
source

You cannot, because maven is used to build and does not exist after the build.

If you need it at build time (for example, via the exec plugin), then it is either available as a system, or you can pass it as an argument to an executable program.

+3
source

I assume that you want it to be on startup with "exec: exec" or "test". If this happens, you can get it through

 System.getProperties().get("basedir") 
+3
source

All Articles