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

I have the same question for Maven that someone else had around Ant ( How can I get the name of the last folder in my base path in Ant? ).

How do I get just the last directory name from the $ {project.basedir} variable?

For example, if my pom.xml is in:

/home/me/project/pom.xml

Then $ {project.basedir} = / home / me / project /

I just want the directory name 'project'.

Thank!

+3


source to share


1 answer


Using: ${project.file.parentFile.name}

How it works:

In fact, ${project}

allowed object MavenProject . From there, you can simply use the bean properties to get the value you want. In this case:

  • use property file

    (via getFile () )
  • use property parentFile

    on java.io.File (via getParentFile () )
  • use property name

    on file to get only name without path information (via getName () )



(edit: after comment)

It's a good idea to use properties for artifact IDs. The property ${project.file.parentFile.name}

cannot be resolved when used as part of the artifactId, however some properties do work ( project.groupId

for artifactId it seems to work).

However, this is not recommended. In fact, if you use any property for the artifact id instead of a constant, you will get a warning when building 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.

      

+10


source







All Articles