简体   繁体   English

在不能添加本地Maven回购的情况下,如何在Maven项目(编译,构建,打包)中包含第3方jars?

[英]How include 3rd party jars in maven project (compile, build, package) where adding local maven repo is not an option?

We have a git project that has some 3rd party jars which are not available in any Maven repo and are in a "lib" folder in the project. 我们有一个git项目,其中包含一些第3方jar,它们在任何Maven存储库中都不可用,并且位于该项目的“ lib”文件夹中。 I need to include them for compiling, building and then package them into the WAR in WEB-INF/lib . 我需要包括它们进行编译,构建,然后将其打包到WEB-INF/lib的WAR中。

I cannot add them as a local maven repo from the command line because this projects needs to be buildable for anyone cloning the repo without requiring them to run extra commands (I have no way around this requirement). 我不能从命令行将它们添加为本地Maven存储库,因为对于克隆该存储库的任何人都需要该项目是可构建的,而不需要它们运行额外的命令(我无法解决此要求)。

I saw some people suggesting System scope but that then Maven won't package them into your WAR: 我看到有人建议使用系统范围,但是Maven不会将它们打包到您的WAR中:

<dependency>
    <groupId>com.roufid.tutorials</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>

How do I get these jars to be used for compiling/inspection, building and then packaged into the WAR? 我如何将这些罐子用于编译/检查,构建,然后打包到WAR中?

You can use : 您可以使用 :

  • System scope but make the packaging yourself via assembly plugin 系统范围,但通过assembly插件自行打包
  • A maven repo along with your project (ie maven repo on-the-fly, basically same as local repo but without having a extra moving part to worry about because this repo follows your project). 一个与您的项目一起的Maven回购(即即时Maven回购,与本地回购基本相同,但无需担心其他麻烦,因为该回购遵循您的项目)。

For Maven repo on-the-fly option, you can do as described here (which is, take any already existing Maven repo, which already contains your needed jars, such as your local one, put it in your project, and then reference this repo from your project using relative paths). 对于即时Maven存储库选项,您可以按照此处所述进行操作(也就是,将任何已经存在的Maven存储库(其中已包含所需的jar)(例如本地的jar)放入项目中,然后再引用它。使用相对路径从您的项目中回购)。

I'll assume you've verified that whatever mechanism you might use to distribute these jars would be in compliance with the relevant licenses. 我假设您已验证,可以用来分发这些jar的任何机制都将符合相关许可证。 If it would, then it seems there would be little reason for the jars' creators not to provide for official Maven distribution, so your best option might be to lobby for them to do that. 如果可以的话,那么罐子的创建者似乎就没有理由提供官方的Maven发行版了,所以您最好的选择可能是游说他们这样做。 But if not, and yet for some reason they'll allow for you distributing the jar (either through cloning of your repo, or via a separate Maven repo you maintain): 但是,如果不是这样,由于某种原因,它们将允许您分发jar(通过克隆您的存储库或通过您维护的单独的Maven存储库):

There are several ways. 有几种方法。 I give preference to approaches that don't put the jars in the git repo. 我倾向于不要将jar放入git repo中的方法。

Publish a Maven Repo 发布Maven回购

So it's possible to host a public-facing repo and serve the artifacts that way. 因此,可以托管一个面向公众的仓库,并以这种方式提供工件。 The pom can add your public-facing repo to the build, so that those who clone can build without having to run any special commands. pom可以将您的面向公众的仓库添加到构建中,以便克隆者可以构建而无需运行任何特殊命令。

Running your own repo isn't terribly difficult. 运行自己的仓库不是很困难。 The OSS versions of Nexus or Artifactory jFrog would probably be perfectly capable. Nexus或Artifactory jFrog的OSS版本可能会非常强大。

But, if we're assuming the authors' refusal to publish their own jars via Maven means they don't want them distributed that way, then there's no reason to spend much time on the details of this option. 但是,如果我们假设作者拒绝通过Maven发布自己的jar,则意味着他们不希望以这种方式分发jar,则没有理由在此选项的细节上花很多时间。 So moving on... 所以继续...

Distribution in the Git Repo 在Git Repo中分发

I guess this is what you're doing, though again if Maven distribution violates the license I'd make sure you're splitting hairs the right way in thinking that this doesn't. 我想这就是您正在执行的操作,尽管如果Maven发行版违反了许可证,我还是要确保您以正确的方式进行梳理,以为这样做不会。

So the question would be how to get Maven to deal with the artifacts distributed in this way, and again there are some options. 因此,问题将是如何使Maven处理以这种方式分布的工件,并且还有一些选择。

Your objection to putting the jars in the local repo is that it would require extra commands of the user; 您反对将罐子放入本地仓库中是因为它需要用户额外的命令; but actually this could be automated in the "validate" phase of the build. 但是实际上,这可以在构建的“验证”阶段自动进行。 Binding install:install-file to the validate phase should work. install:install-file绑定到validate阶段应该起作用。

Alternately, your objection to using system scope is that the file isn't copied into the final war. 或者,您反对使用system范围是该文件未复制到最终战争中。 You might be able to use the dependency plugin to force the issue, but I'm not sure of that. 您可能可以使用依赖插件来强制执行此问题,但是我不确定。 What I am sure of is you could treat the directory containing the jars as a web resource with suitable configuration in the war plugin. 我确定的是,您可以将包含jar的目录作为Web资源,并在war插件中进行适当配置。 (You'd want it to be treated as unfiltered and to map to the WEB-INF/lib folder.) (您希望将其视为未过滤的并映射到WEB-INF/lib文件夹。)

In any case, if you distribute jars (or other large binaries) in the git repo, I strongly recommend you look at git lfs . 无论如何,如果您在git repo中分发jar(或其他大型二进制文件),强烈建议您查看git lfs This will require one-time configuration by each of your users, but it will prevent your repo from gradually becoming bloated and unusable. 这将需要您的每个用户进行一次配置,但这将防止您的存储库逐渐变得and肿且无法使用。

Use forward slash (/) to backslash () in the systemPath. 在systemPath中使用正斜杠(/)来反斜杠()。

<dependency>
<groupId>com.roufid.tutorials</groupId>`enter code here`
<artifactId>example-app</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}\lib\yourJar.jar</systemPath>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM