Java 我可以将 jars 添加到 maven 2 构建类路径而不安装它们吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/364114/
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
Can I add jars to maven 2 build classpath without installing them?
提问by
Maven2 is driving me crazy during the experimentation / quick and dirty mock-up phase of development.
Maven2 在开发的实验/快速和肮脏的模型阶段让我发疯。
I have a pom.xml
file that defines the dependencies for the web-app framework I want to use, and I can quickly generate starter projects from that file. However, sometimes I want to link to a 3rd party library that doesn't already have a pom.xml
file defined, so rather than create the pom.xml
file for the 3rd party lib by hand and install it, and add the dependency to my pom.xml
, I would just like to tell Maven: "In addition to my defined dependencies, include any jars that are in /lib
too."
我有一个pom.xml
文件,它定义了我想要使用的 Web 应用程序框架的依赖项,我可以从该文件快速生成入门项目。但是,有时我想链接到尚未pom.xml
定义文件的 3rd 方库,因此与其pom.xml
手动为 3rd 方库创建文件并安装它,然后将依赖项添加到我的pom.xml
.告诉 Maven:“除了我定义的依赖项之外,还要包括其中的任何 jar /lib
。”
It seems like this ought to be simple, but if it is, I am missing something.
看起来这应该很简单,但如果是这样,我就错过了一些东西。
Any pointers on how to do this are greatly appreciated. Short of that, if there is a simple way to point maven to a /lib
directory and easily create a pom.xml
with all the enclosed jars mapped to a single dependency which I could then name / install and link to in one fell swoop would also suffice.
非常感谢有关如何执行此操作的任何指示。简而言之,如果有一种简单的方法将 maven 指向一个/lib
目录并轻松创建一个pom.xml
将所有封闭的 jar 映射到单个依赖项的方法,然后我可以一举命名/安装并链接到该依赖项也足够了。
回答by javamonkey79
This doesn't answer how to add them to your POM, and may be a no brainer, but would just adding the lib dir to your classpath work? I know that is what I do when I need an external jar that I don't want to add to my Maven repos.
这并没有回答如何将它们添加到您的 POM 中,并且可能很简单,但是只是将 lib 目录添加到您的类路径中是否有效?我知道当我需要一个我不想添加到我的 Maven 存储库的外部 jar 时,我会这样做。
Hope this helps.
希望这可以帮助。
回答by Pyrolistical
For throw away code only
仅用于丢弃代码
set scope == system and just make up a groupId, artifactId, and version
设置范围==系统,只组成一个groupId,artifactId,和版本
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swingx</artifactId>
<version>0.9.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>
Note: system dependencies are not copied into resulted jar/war
(see How to include system dependencies in war built using maven)
注意:系统依赖项不会复制到结果 jar/war 中
(请参阅如何在使用 maven 构建的 war 中包含系统依赖项)
回答by Ed Brannin
Note: When using the System scope (as mentioned on this page), Maven needs absolute paths.
注意:当使用系统范围(如本页所述)时,Maven 需要绝对路径。
If your jars are under your project's root, you'll want to prefix your systemPath values with ${basedir}.
如果您的 jars 位于项目的根目录下,您需要使用 ${basedir} 为 systemPath 值添加前缀。
回答by toad
Maven install pluginhas command line usage to install a jar into the local repository, POM is optional but you will have to specify the GroupId, ArtifactId, Version and Packaging (all the POM stuff).
Maven 安装插件使用命令行将 jar 安装到本地存储库中,POM 是可选的,但您必须指定 GroupId、ArtifactId、Version 和 Packaging(所有 POM 内容)。
回答by Brian Fox
You really ought to get a framework in place via a repository and identifying your dependencies up front. Using the system scope is a common mistake people use, because they "don't care about the dependency management." The trouble is that doing this you end up with a perverted maven build that will not show maven in a normal condition. You would be better off following an approach like this.
您确实应该通过存储库并预先确定您的依赖项来获得一个框架。使用系统作用域是人们常犯的错误,因为他们“不关心依赖管理”。麻烦的是,这样做你最终会得到一个变态的 maven 构建,它不会在正常情况下显示 maven。你会断下像一个更好的办法这个。
回答by Balint Pato
A strange solution I found:
我发现了一个奇怪的解决方案:
using Eclipse
使用 Eclipse
- create simple (non-maven) java project
- add a Main class
- add all the jars to the classpath
- export Runnable JAR (it's important, because no other way here to do it)
- select Extract required libraries into generated JAR
- decide the licence issues
- tadammm...install the generated jar to your m2repo
- add this single dependency to your other projects.
- 创建简单的(非 maven)java 项目
- 添加一个主类
- 将所有罐子添加到类路径
- 导出 Runnable JAR(这很重要,因为这里没有其他方法可以做到)
- 选择将所需的库提取到生成的 JAR 中
- 决定许可证问题
- tadammm...将生成的 jar 安装到您的 m2repo
- 将此单个依赖项添加到您的其他项目中。
cheers, Balint
干杯,巴林特
回答by Cephalopod
Even though it does not exactly fit to your problem, I'll drop this here. My requirements were:
即使它不完全适合您的问题,我也会把它放在这里。我的要求是:
- Jars that can not be found in an online maven repository should be in the SVN.
- If one developer adds another library, the other developers should not be bothered with manually installing them.
- The IDE (NetBeans in my case) should be able find the sources and javadocs to provide autocompletion and help.
- 在在线 Maven 存储库中找不到的 jar 应该在 SVN 中。
- 如果一个开发人员添加了另一个库,其他开发人员不应该费心手动安装它们。
- IDE(在我的例子中是 NetBeans)应该能够找到源代码和 javadoc 来提供自动完成和帮助。
Let's talk about (3) first: Just having the jars in a folder and somehow merging them into the final jar will not work for here, since the IDE will not understand this. This means all libraries have to be installed properly. However, I dont want to have everyone installing it using "mvn install-file".
让我们先谈谈 (3):只是将 jar 放在一个文件夹中并以某种方式将它们合并到最终的 jar 中将在这里不起作用,因为 IDE 不会理解这一点。这意味着必须正确安装所有库。但是,我不想让每个人都使用“mvn install-file”安装它。
In my project I needed metawidget. Here we go:
在我的项目中,我需要元小部件。开始了:
- Create a new maven project (name it "shared-libs" or something like that).
- Download metawidget and extract the zip into src/main/lib.
- The folder doc/api contains the javadocs. Create a zip of the content (doc/api/api.zip).
- Modify the pom like this
- Build the project and the library will be installed.
- Add the library as a dependency to your project, or (if you added the dependency in the shared-libs project) add shared-libs as dependency to get all libraries at once.
- 创建一个新的 maven 项目(将其命名为“shared-libs”或类似名称)。
- 下载 metawidget 并将 zip 解压缩到 src/main/lib。
- 文件夹 doc/api 包含 javadocs。创建内容的 zip (doc/api/api.zip)。
- 像这样修改pom
- 构建项目并安装库。
- 将库作为依赖项添加到您的项目,或者(如果您在共享库项目中添加了依赖项)添加共享库作为依赖项以一次获取所有库。
Every time you have a new library, just add a new execution and tell everyone to build the project again (you can improve this process with project hierachies).
每次你有了一个新的库,只需添加一个新的执行并告诉大家重新构建项目(你可以通过项目层次结构改进这个过程)。
回答by Praneel PIDIKITI
This is how we add or install a local jar
这就是我们添加或安装本地 jar 的方式
<dependency>
<groupId>org.example</groupId>
<artifactId>iamajar</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/iamajar.jar</systemPath>
</dependency>
i gave some default groupId and artifactId because they are mandatory :)
我给了一些默认的 groupId 和 artifactId 因为它们是强制性的:)
回答by Alex Lehmann
If you want a quick and dirty solution, you can do the following (though I do not recommend this for anything except test projects, maven will complain in length that this is not proper).
如果您想要一个快速而肮脏的解决方案,您可以执行以下操作(尽管我不建议将其用于测试项目以外的任何内容,maven 会详细地抱怨这不合适)。
Add a dependency entry for each jar file you need, preferably with a perl script or something similar and copy/paste that into your pom file.
为您需要的每个 jar 文件添加一个依赖项,最好使用 perl 脚本或类似的东西,然后将其复制/粘贴到您的 pom 文件中。
#! /usr/bin/perl
foreach my $n (@ARGV) {
$n=~s@.*/@@;
print "<dependency>
<groupId>local.dummy</groupId>
<artifactId>$n</artifactId>
<version>0.0.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/$n</systemPath>
</dependency>
";
回答by mschonaker
The problem with systemPath
is that the dependencies' jars won't get distributed along your artifacts as transitive dependencies. Try what I've posted here: Is it best to Mavenize your project jar files or put them in WEB-INF/lib?
问题systemPath
在于依赖项的 jars 不会作为传递依赖项沿着您的工件分发。试试我在这里发布的内容:最好是将项目 jar 文件 Maven 化还是将它们放在 WEB-INF/lib 中?
Then declare dependencies as usual.
然后像往常一样声明依赖项。
And please read the footer note.
请阅读页脚说明。