Java Maven:如何将代表中不可用的 jar 包含到 J2EE 项目中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1164043/
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
Maven: How to include jars, which are not available in reps into a J2EE project?
提问by samson
in my J2EE project I've a couple of dependencies, which are not available in any Maven repository, because they're proprietary libraries. These libraries need to be available at runtime, so that have to be copied to target/.../WEB-INF/lib ...
在我的 J2EE 项目中,我有几个依赖项,它们在任何 Maven 存储库中都不可用,因为它们是专有库。这些库需要在运行时可用,因此必须将它们复制到 target/.../WEB-INF/lib ...
Right now, I'm listing them as system dependency in my POM, but with this method the problem is, that aren't being copied to the target build during compilation. Also this method is not very elegant.
现在,我在我的 POM 中将它们列为系统依赖项,但是使用这种方法的问题是,在编译过程中不会将它们复制到目标构建中。这种方法也不是很优雅。
So which is the best way to integrate them in Maven?
那么将它们集成到 Maven 中的最佳方式是什么?
Note: I don't want to create my own Maven repository.
注意:我不想创建自己的 Maven 存储库。
采纳答案by Rich Seller
As you've said you don't want to set up your own repository, perhaps this will help.
正如您所说,您不想建立自己的存储库,也许这会有所帮助。
You can use the install-file goalof the maven-install-plugin to install a file to the local repository. If you create a script with a Maven invocation for each file and keep it alongside the jars, you (and anyone else with access) can easily install the jars (and associated pom files) to their local repository.
您可以使用maven-install-plugin的install-file 目标将文件安装到本地存储库。如果您为每个文件创建一个带有 Maven 调用的脚本并将其保存在 jar 旁边,则您(以及其他任何具有访问权限的人)可以轻松地将 jar(和关联的 pom 文件)安装到他们的本地存储库中。
For example:
例如:
mvn install:install-file -Dfile=/usr/jars/foo.jar -DpomFile=/usr/jars/foo.pom
mvn install:install-file -Dfile=/usr/jars/bar.jar -DpomFile=/usr/jars/bar.pom
or just
要不就
mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0 -Dpackaging=jar
You can then reference the dependencies as normal in your project.
然后,您可以在项目中照常引用依赖项。
However your best bet is still to set up an internal remote repository and I'd recommend using Nexusmyself. It can run on your development box if needed, and the overhead is minimal.
然而,您最好的选择仍然是建立一个内部远程存储库,我建议自己使用Nexus。如果需要,它可以在您的开发箱上运行,并且开销很小。
回答by dfa
you can install them in a private, local repository (e.g. .m2/repository under your home directory): more details here
您可以将它们安装在私有的本地存储库中(例如,您的主目录下的 .m2/repository):此处有更多详细信息
回答by Gregory Mostizky
You need to set up a local repository that will host such libraries. There are a number of projects that do exactly that. For example Artifactory.
您需要设置一个本地存储库来托管此类库。有许多项目正是这样做的。例如Artifactory。
回答by Erik Clairiot
If I am understanding well, if what you want to do is to export dependencies during the compilation phase so there will be no need to retrieve manually each needed libraries, you can use the mojo copy-dependencies.
如果我理解得很好,如果您想要做的是在编译阶段导出依赖项,因此无需手动检索每个需要的库,您可以使用 mojo copy-dependencies。
Hope it can be useful in your case (examples)
希望它对您的情况有用(示例)
回答by Corehpf
Continue to use them as a system dependency and copy them over to target/.../WEB-INF/lib ... using the Maven dependency plugin:
继续将它们用作系统依赖项并将它们复制到 target/.../WEB-INF/lib ... 使用 Maven 依赖项插件:
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html
回答by Ric Jafe
For people wanting a quick solution to this problem:
对于想要快速解决此问题的人:
<dependency>
<groupId>LIB_NAME</groupId>
<artifactId>LIB_NAME</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/YOUR_LIB.jar</systemPath>
</dependency>
just give your library a unique groupID and artifact name and point to where it is in the file system. You are good to go.
只需给您的库一个唯一的 groupID 和工件名称,并指向它在文件系统中的位置。你已准备好出发。
Of course this is a dirty quick fix that will ONLY work on your machine and if you don't change the path to the libs. But some times, that's all you want, to run and do a few tests.
当然,这是一个肮脏的快速修复,仅适用于您的机器,并且如果您不更改库的路径。但有时,这就是您想要的,运行并进行一些测试。
EDIT: just re-red the question and realised the user was already using my solution as a temporary fix. I'll leave my answer as a quick help for others that come to this question. If anyone disagrees with this please leave me a comment. :)
编辑:只是重新提出问题并意识到用户已经在使用我的解决方案作为临时解决方案。我会留下我的答案,作为对遇到此问题的其他人的快速帮助。如果有人不同意这一点,请给我留言。:)
回答by Jonathan Hendler
Install alone didn't work for me.
单独安装对我不起作用。
mvn deploy:deploy-file -Durl=file:///home/me/project/lib/ \
-Dfile=target/jzmq-2.1.3-SNAPSHOT.jar -DgroupId=org.zeromq \
-DartifactId=zeromq -Dpackaging=jar -Dversion=2.1.3
回答by Paul
@Ric Jafe's solution is what worked for me.
@Ric Jafe 的解决方案对我有用。
This is exactly what I was looking for. A way to push it through for research test code. Nothing fancy. Yeah I know that that's what they all say :) The various maven plugin solutions seem to be overkill for my purposes. I have some jars that were given to me as 3rd party libs with a pom file. I want it to compile/run quickly. This solution which I trivially adapted to python worked wonders for me. Cut and pasted into my pom. Python/Perl code for this task is in this Q&A: Can I add jars to maven 2 build classpath without installing them?
这正是我正在寻找的。一种通过研究测试代码推动它的方法。没有什么花哨。是的,我知道这就是他们所说的 :) 各种 Maven 插件解决方案似乎对我的目的来说太过分了。我有一些罐子作为带有 pom 文件的第 3 方库提供给我。我希望它能够快速编译/运行。这个我简单地适应 python 的解决方案为我创造了奇迹。剪切并粘贴到我的 pom 中。此任务的 Python/Perl 代码在此问答中:我可以将 jars 添加到 maven 2 构建类路径而不安装它们吗?
def AddJars(jarList):
s1 = ''
for elem in jarList:
s1+= """
<dependency>
<groupId>local.dummy</groupId>
<artifactId>%s</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/manual_jars/%s</systemPath>
</dependency>\n"""%(elem, elem)
return s1
回答by mostar
Create a repository folder under your project. Let's take
在您的项目下创建一个存储库文件夹。让我们来
${project.basedir}/src/main/resources/repo
Then, install your custom jar to this repo:
然后,将您的自定义 jar 安装到此 repo:
mvn install:install-file -Dfile=[FILE_PATH] \
-DgroupId=[GROUP] -DartifactId=[ARTIFACT] -Dversion=[VERS] \
-Dpackaging=jar -DlocalRepositoryPath=[REPO_DIR]
Lastly, add the following repo and dependency definitions to the projects pom.xml:
最后,将以下 repo 和依赖项定义添加到项目 pom.xml 中:
<repositories>
<repository>
<id>project-repo</id>
<url>file://${project.basedir}/src/main/resources/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>[GROUP]</groupId>
<artifactId>[ARTIFACT]</artifactId>
<version>[VERS]</version>
</dependency>
</dependencies>
回答by Apurva Singh
None of the solutions work if you are using Jenkins build!! When pom is run inside Jenkins build server.. these solutions will fail, as Jenkins run pom will try to download these files from enterprise repository.
如果您使用 Jenkins 构建,这些解决方案都不起作用!!当 pom 在 Jenkins 构建服务器中运行时.. 这些解决方案将失败,因为 Jenkins run pom 将尝试从企业存储库下载这些文件。
Copy jars under src/main/resources/lib (create lib folder). These will be part of your project and go all the way to deployment server. In deployment server, make sure your startup scripts contain src/main/resources/lib/* in classpath. Viola.
复制 src/main/resources/lib 下的 jars(创建 lib 文件夹)。这些将成为您项目的一部分,并一直到部署服务器。在部署服务器中,确保您的启动脚本在类路径中包含 src/main/resources/lib/*。中提琴。