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:
file
property (through getFile())parentFile
property on java.io.File (through getParentFile())name
property 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.groupId
for 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:
[WARNING] Some problems were encountered while building the effective model for <your groupID>:<your artifactID>:jar:1.0-SNAPSHOT
[WARNING] 'artifactId' contains an expression but should be a constant. @ <your groupID>:<your artifact ID>:1.0-SNAPSHOT, <basedir>/pom.xml, line 5, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With