Java 从 Maven 获取 ${basedir} 的父目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19511785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:53:44  来源:igfitidea点击:

getting parent directory of ${basedir} from maven

javamaven

提问by Surendran Duraisamy

Is there way to get parent directory for ${basedir}in my pom.xml? Currently I have

有没有办法${basedir}在我的 pom.xml 中获取父目录?目前我有

<earSourceDirectory>${basedir}/EAR/src/main/resources</earSourceDirectory> 

but I need to access parent directory of basedir as my resources lies in different maven project.

但我需要访问 basedir 的父目录,因为我的资源位于不同的 maven 项目中。

How can I get the parent folder of ${basedir}?

如何获取 $ 的父文件夹{basedir}

回答by diegomtassis

${project.basedir}/../

However, access resources in a different module is something I try to avoid. I'd suggest using the unpack goal of the maven-dependency-plugin.

但是,我尽量避免访问不同模块中的资源。我建议使用maven-dependency-plugin解包目标

回答by Robert Scholte

Please, don't try to go outside the basedir, because it's considered a very bad practice. Even if you succeed now, it will be the start of a workaround over workaround, trying to battle Maven. In the end, Maven will win. If a developer checks out a directory with a pom.xml, it should be able run the project with mvn verifywithout any references to other directories.

请不要试图走出 basedir,因为这被认为是一种非常糟糕的做法。即使你现在成功了,这也将是一种替代变通方法的开始,试图与 Maven 抗争。最终,Maven 将获胜。如果开发人员签出带有 的目录pom.xml,它应该能够在mvn verify不引用其他目录的情况下运行项目。

If these are shared resources, make it a separate project and include it as a dependency (search for multi module projects). Or use the maven-remote-resources-pluginto pull these resources into your project.

如果这些是共享资源,请将其设为单独的项目并将其作为依赖项包含(搜索多模块项目)。或者使用maven-remote-resources-plugin将这些资源拉入您的项目。

回答by Ashish

In the children:

在儿童中:

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
</properties>

In the grandchildren:

在孙子中:

<properties>
    <main.basedir>${project.parent.parent.basedir}</main.basedir>
</properties>

回答by Ehtesham Siddiquie

I had the requirement of moving zip generated in MainDirectory/Submodule/target/ to MainDirectory/target/. I tried all solutions from everywhere but I was unable to solve. I finally tried Ant plugin in pom.xml of submodule to copy as below:

我需要将 MainDirectory/Submodule/target/ 中生成的 zip 移动到 MainDirectory/target/。我尝试了所有地方的所有解决方案,但我无法解决。我终于在子模块的 pom.xml 中尝试了 Ant 插件复制如下:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>copy to parent target</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <copy todir="../target/" overwrite="true" flatten="true"> <fileset dir="${basedir}/target/"/> </copy> </target> </configuration> </execution> </executions> </plugin>

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <id>copy to parent target</id> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <copy todir="../target/" overwrite="true" flatten="true"> <fileset dir="${basedir}/target/"/> </copy> </target> </configuration> </execution> </executions> </plugin>