Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name of last folder from maven ${project.basedir}

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!

like image 308
Naijaba Avatar asked Dec 16 '22 02:12

Naijaba


1 Answers

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:

  • use the file property (through getFile())
  • use the parentFile property on java.io.File (through getParentFile())
  • use the 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.
like image 180
prunge Avatar answered Dec 27 '22 11:12

prunge