Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

java - 是否可以在不安装jar的情况下将jar添加到maven 2 build classpath?(Can I add jars to maven 2 build classpath without installing them?)

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. (我有一个pom.xml文件,该文件定义了要使用的Web应用程序框架的依赖项,并且可以从该文件快速生成启动程序项目。) 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文件的第三方库,因此,不是手动为第三方库创建pom.xml文件并安装它,然后将依赖项添加到我的pom.xml ,我想告诉Maven:“除了定义的依赖项之外,还包括/lib所有jar。”)

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映射到单个依赖项,然后我可以命名/安装并链接到一个依赖项中,也足够。)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. (您将在Internet上找到的大多数答案都建议您将依赖项安装到本地存储库中,或者在pom指定“系统”范围,然后将依赖项与项目源一起分发。) But both of these solutions are actually flawed. (但是,这两种解决方案实际上都是有缺陷的。)

Why you shouldn't apply the "Install to Local Repo" approach (为什么不应该应用“安装到本地存储库”方法)

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. (否则,必须将此依赖项本地安装在与该项目一起工作的每台计算机上,这并没有更好的效果。)

Why you shouldn't apply the "System Scope" approach (为什么不应该应用“系统范围”方法)

The jars you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. (您使用“系统范围”方法依赖的jar既不会安装到任何存储库中,也不会附加到您的目标软件包中。) 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. (无论如何,您都不想依赖不推荐使用的功能。)

The static in-project repository solution (静态项目内存储库解决方案)

After putting this in your pom : (将其放入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>

for each artifact with a group id of form xyz Maven will include the following location inside your project dir in its search for artifacts: (对于每个组ID为xyz形式的工件,Maven在搜索工件时将在项目目录中包含以下位置:)

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

To elaborate more on this you can read this blog post . (要对此进行详细说明,可以阅读此博客文章 。)

Use Maven to install to project repo (使用Maven安装到项目仓库)

Instead of creating this structure by hand I recommend to use a Maven plugin to install your jars as artifacts. (我建议不要使用手动创建此结构,而是使用Maven插件将jar安装为工件。) So, to install an artifact to an in-project repository under repo folder execute: (因此,要将工件安装到repo文件夹下的项目内存储库中,请执行以下操作:)

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

If you'll choose this approach you'll be able to simplify the repository declaration in pom to: (如果选择这种方法,则可以将pom的存储库声明简化为:)

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

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. (由于对每个库执行安装命令有点烦人并且肯定容易出错,因此我创建了一个实用程序脚本 ,该脚本会自动将所有jar从lib文件夹安装到项目存储库,同时自动解析所有元数据(groupId,artifactId等)。来自文件名。) The script also prints out the dependencies xml for you to copy-paste in your pom . (该脚本还打印出依赖项xml,供您复制粘贴到pom 。)

Include the dependencies in your target package (在目标包中包含依赖项)

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. (创建项目内存储库后,将解决分配项目及其源代码的依赖关系的问题,但是从那时起,项目的目标工件将依赖于未发布的jar,因此在安装时它到存储库中,它将具有无法解决的依赖关系。)

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 . (您可以使用Assembly插件,也可以使用OneJar插件更好地做到这一点 。) The official documentaion on OneJar is easy to grasp. (OneJar上的官方文档很容易掌握。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...