我可以在没有安装的情况下将jar添加到maven 2 build classpath吗? [英] Can I add jars to maven 2 build classpath without installing them?

查看:69
本文介绍了我可以在没有安装的情况下将jar添加到maven 2 build classpath吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven2在实验/快速和肮脏的模拟开发阶段让我发疯。

Maven2 is driving me crazy during the experimentation / quick and dirty mock-up phase of development.

我有一个 pom.xml 文件,它定义了我想要使用的web-app框架的依赖关系,我可以从该文件快速生成启动项目。但是,有时我想链接到尚未定义 pom.xml 文件的第三方库,因此不要创建 pom手动安装第三方lib的.xml 文件,并将依赖项添加到我的 pom.xml ,我想告诉Maven:除了我定义的依赖项,还包括 / lib 中的任何jar。

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."

看起来这应该很简单,但如果是的话,我会遗漏一些东西。

It seems like this ought to be simple, but if it is, I am missing something.

任何关于如何做到这一点的指针非常感谢。如果没有这个,如果有一种简单的方法将maven指向 / lib 目录并轻松创建 pom.xml 将所有封闭的jar映射到单个依赖项,然后我可以命名/安装并一次性链接就足够了。

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.

推荐答案

流行方法的问题



您在互联网上找到的大多数答案都会建议您将依赖项安装到本地存储库或指定系统 pom 中的范围,并使用项目源分发依赖关系。但这两种解决方案实际上都存在缺陷。

Problems of popular approaches

Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom and distribute the dependency with the source of your project. But both of these solutions are actually flawed.

当您将依赖项安装到本地存储库时,它仍然存在。只要它可以访问此存储库,您的分发工件就可以正常工作。问题是在大多数情况下,此存储库将驻留在您的本地计算机上,因此无法在任何其他计算机上解决此依赖关系。显然,使您的工件依赖于特定的机器并不是处理事情的方法。否则,必须在使用该项目的每台计算机上本地安装此依赖项,这不是更好。

When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.

您使用系统范围方法所依赖的罐子既不会安装到任何存储库,也不会连接到目标软件包。这就是为什么您的分发包在使用时无法解决该依赖关系的原因。我认为这就是为什么系统范围的使用甚至被弃用的原因。无论如何,您不希望依赖已弃用的功能。

The jars you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature.

把它放在你的 pom 之后:

<repository>
    <id>repo</id>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/repo</url>
</repository>

每个工件的

,其组ID为 xyz Maven将在项目目录中包含以下位置以搜索工件:

for each artifact with a group id of form x.y.z Maven will include the following location inside your project dir in its search for artifacts:

repo/
| - x/
|   | - y/
|   |   | - z/
|   |   |   | - ${artifactId}/
|   |   |   |   | - ${version}/
|   |   |   |   |   | - ${artifactId}-${version}.jar

要详细说明,请阅读< a href =http://blog.dub.podval.org/2010/01/maven-in-project-repository.html\"rel =noreferrer>此博文。

我建议使用Maven插件安装你的罐子,而不是手工创建这个结构文物。因此,要将工件安装到 repo 文件夹下的项目内存储库中执行:

Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. So, to install an artifact to an in-project repository under repo folder execute:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

如果您选择这种方法,您将能够简化存储库声明在 pom 中:

If you'll choose this approach you'll be able to simplify the repository declaration in pom to:

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>



辅助脚本



执行安装后每个lib的命令有点烦人且肯定容易出错,我创建了一个实用程序脚本自动将所有jar从 lib 文件夹安装到项目存储库,同时自动从文件名解析所有元数据(groupId,artifactId等)。该脚本还打印出依赖项xml,以便在 pom 中进行复制粘贴。

A helper script

Since executing installation command for each lib is kinda annoying and definitely error prone, I've created a utility script which automatically installs all the jars from a lib folder to a project repository, while automatically resolving all metadata (groupId, artifactId and etc.) from names of files. The script also prints out the dependencies xml for you to copy-paste in your pom.

当您创建项目内存储库时,您将解决使用其源分发项目依赖项的问题,但从那时起您的项目的目标工件将取决于未发布的jar,因此当您将其安装到存储库时,它将具有无法解析的依赖项。

When you'll have your in-project repository created you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published jars, so when you'll install it to a repository it will have unresolvable dependencies.

为了解决这个问题,我建议在目标包中包含这些依赖项。您可以使用装配插件或更好地使用 OneJar插件。关于OneJar的官方文档很容易掌握。

To beat this problem I suggest to include these dependencies in your target package. This you can do with either the Assembly Plugin or better with the OneJar Plugin. The official documentaion on OneJar is easy to grasp.

这篇关于我可以在没有安装的情况下将jar添加到maven 2 build classpath吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆