I have the same question for Maven that someone had about Ant (How can I get the name of the last folder in my basedir path in Ant?).
How do you get just the last directory name from the variable ${project.basedir}?
For example, if my pom.xml is in:
/home/me/project/pom.xml
Then ${project.basedir} = /home/me/project/
I want just the directory name ‘project’.
Thanks!
Use:
${project.file.parentFile.name}How to work it out:
The
${project}actually resolved to a MavenProject object. From there, you can just use bean properties to get the value you need. In this case:fileproperty (through getFile())parentFileproperty on java.io.File (through getParentFile())nameproperty of the File to get just the name without path information (through getName())(edit: after comment)
It’s not a good idea to use properties for artifact IDs. The
${project.file.parentFile.name}property cannot be resolved when using it as part of artifactId, however some properties do work (project.groupIdfor the artifactId seems to work).However, this is not recommended. In fact, if you use any property for the artifact ID instead of a constant, you’ll get a warning when you build your project: