我对 Maven 有同样的问题,有人对 Ant ( How can I get the name of the last folder in my basedir path in Ant? ) 提出过同样的问题。
如何从变量 ${project.basedir} 中获取最后一个目录名称?
例如,如果我的 pom.xml 在:
/home/me/project/pom.xml
然后 ${project.basedir} =/home/me/project/
我只想要目录名称“项目”。
谢谢!
最佳答案
使用:${project.file.parentFile.name}
如何解决:${project}
实际上解析为 MavenProject 对象。从那里,您可以只使用 bean 属性来获取所需的值。在这种情况下:
file
属性(通过 getFile() ) parentFile
属性(通过 getParentFile() )name
属性来获取没有路径信息的名称(通过 getName() ) (编辑:评论后)
为 Artifact ID 使用属性不是一个好主意。
${project.file.parentFile.name}
属性作为 artifactId 的一部分使用时无法解析,但是某些属性确实有效(artifactId 的 project.groupId
似乎有效)。但是,不建议这样做。事实上,如果您使用 Artifact ID 的任何属性而不是常量,您将在构建项目时收到警告:
[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.
关于maven - 从 maven ${project.basedir} 获取最后一个文件夹的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849868/