java Maven2:在 jar 路径中使用 ${basedir}
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4252485/
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
Maven2: use ${basedir} in jar path
提问by Clayton
I am using an external, proprietary jar in my project. When I hard-code the path as follows in my pom.xml, it works fine:
我在我的项目中使用了一个外部的、专有的 jar。当我在 pom.xml 中按如下方式对路径进行硬编码时,它工作正常:
<dependency>
<groupId>com.foo.bar</groupId>
<artifactId>bar</artifactId>
<version>5.2</version>
<scope>system</scope>
<type>jar</type>
<systemPath>D:\workspace\myproj\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>
However, when I try to use the ${basedir} variable, maven can't find the jar:
但是,当我尝试使用 ${basedir} 变量时,maven 找不到 jar:
<dependency>
<groupId>com.foo.bar</groupId>
<artifactId>bar</artifactId>
<version>5.2</version>
<scope>system</scope>
<type>jar</type>
<systemPath>${basedir}\external\companyname\lib\proprietary_api.jar</systemPath>
</dependency>
The pom is located in D:\workspace\myproj
pom 位于 D:\workspace\myproj
This also needs to be cross-platform compatible (dev on Windows, deploy on Linux).
这也需要跨平台兼容(在 Windows 上开发,在 Linux 上部署)。
Thanks!
谢谢!
回答by lexicore
It is wrong to use system scope for your proprietary JARs. You should deploy or install it into the local/central repository.
将系统范围用于您的专有 JAR 是错误的。您应该将其部署或安装到本地/中央存储库中。
回答by icyrock.com
I'm not sure this will help, but try using forward (/
) instead of backward (\
) slashes. Also, try running it with mvn -e
and mvn -X
(the latter will produce a lotof debugging lines) - this might help you pinpoint the problem.
我不确定这会有所帮助,但请尝试使用向前 ( /
) 而不是向后 ( \
) 斜线。另外,尝试使用mvn -e
and运行它mvn -X
(后者会产生很多调试行)——这可能会帮助你查明问题。
Here's an example:
下面是一个例子:
of using ${basedir}
in the same way you want.
${basedir}
以您想要的相同方式使用。
Btw, why don't you mvn install:install-file
the dependency instead of using systemPath
? See:
顺便说一句,你为什么不mvn install:install-file
使用依赖而不是使用systemPath
?看:
回答by Sajith
In order to be cross-platform compatible use ${file.separator} instead of the slashes.. so that will automatically convert to OS specified format
为了跨平台兼容使用 ${file.separator} 而不是斜杠..这样会自动转换为操作系统指定的格式
回答by rustyx
Use forward slashes in the path.
The ${basedir} placeholder is extrapolated only onceper Maven run. If this project is not the topmost project in your project hierarchy, then ${basedir} will be extrapolated to the location of the topmost project (i.e. the project where Maven started), not the current project.
在路径中使用正斜杠。
${basedir} 占位符每次 Maven 运行仅外推一次。如果这个项目不是你的项目层次结构中最顶层的项目,那么 ${basedir} 将被外推到最顶层项目的位置(即 Maven 开始的项目),而不是当前项目。
回答by Vanchinathan Chandrasekaran
To make it work both on windows and linux, you have to start using profiles. In that way, a particular profile will get activated based on the OS and the build will become portable.
要使其在 windows 和 linux 上都能运行,您必须开始使用配置文件。这样,特定的配置文件将根据操作系统激活,并且构建将变得可移植。
In each profile, you can define a property called jarPath (just an example) and refer that property in your dependency.
在每个配置文件中,您可以定义一个名为 jarPath 的属性(只是一个示例)并在您的依赖项中引用该属性。
Look into the OS tag and configuration tag of the profile. Make sure your build are always portable and less manual steps needs to be done.involved.
查看配置文件的 OS 标记和配置标记。确保您的构建始终是可移植的,并且需要完成更少的手动步骤。涉及。