我可以将 jars 添加到 Maven 2 构建类路径而不安装它们吗? [英] Can I add jars to Maven 2 build classpath without installing them?

查看:21
本文介绍了我可以将 jars 添加到 Maven 2 构建类路径而不安装它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven 2 在开发的实验/快速而肮脏的模型阶段让我发疯.

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

我有一个 pom.xml 文件,它定义了我想要使用的 Web 应用程序框架的依赖项,我可以从该文件快速生成入门项目.但是,有时我想链接到尚未定义 pom.xml 文件的第 3 方库,而不是为手动安装第 3 方库并安装它,并将依赖项添加到我的 pom.xml 中,我只想告诉 Maven:除了我定义的依赖项之外,还包括 pom.xml 中的任何 jarscode>/lib 也是."

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.

推荐答案

流行方法的问题

您在 Internet 上找到的大多数答案都会建议您将依赖项安装到本地存储库或指定一个系统".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.

您依赖于系统范围"的 jars方法既不会安装到任何存储库,也不会附加到您的目标包.这就是为什么您的分发包在使用时无法解决该依赖项的原因.我相信这就是为什么使用系统范围甚至被弃用的原因.无论如何,您不想依赖已弃用的功能.

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>

对于具有 x.y.z 形式的组 ID 的每个工件,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

要详细说明这一点,您可以阅读此博客发布.

To elaborate more on this you can read this blog post.

我建议使用 Maven 插件将您的 jars 安装为工件,而不是手动创建此结构.因此,要将工件安装到 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 安装到项目存储库,同时从文件名自动解析所有元数据(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.

为了解决这个问题,我建议在你的目标包中包含这些依赖项.您可以使用 Assembly Plugin 或使用 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.

这篇关于我可以将 jars 添加到 Maven 2 构建类路径而不安装它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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