简体   繁体   English

在依赖于非 Maven 库时将库部署到 Maven 存储库

[英]Deploying a library to Maven repo when it depends on non-Maven libraries

I run an open source library and am considering having it fully embrace Maven and upload it to to a central repository so that people can easily add it to their projects.我运行一个开源库,正在考虑让它完全接受 Maven 并将其上传到中央存储库,以便人们可以轻松地将它添加到他们的项目中。

The problem is that it depends on a couple of older libraries that do not exist on any Maven repos.问题在于它依赖于任何 Maven 存储库中都不存在的几个较旧的库。 Currently, that means a pom file has to use the system scope of the dependency.目前,这意味着 pom 文件必须使用依赖项的system范围。 I've also read about creating a local repository for the project to install the 3rd party libraries.我还阅读了有关为项目创建本地存储库以安装 3rd 方库的信息。

However, my impression is that neither of these approaches will work well when I deploy my library to a Maven repository.但是,我的印象是,当我将库部署到 Maven 存储库时,这两种方法都不能很好地工作。 That is, if it depends on external "system" or local repositories, then when someone adds my library to their pom file, they're not actually done.也就是说,如果它依赖于外部“系统”或本地存储库,那么当有人将我的库添加到他们的 pom 文件时,实际上并没有完成。 They also have to download the 3rd party libraries and manually install them or add them to their own local repository.他们还必须下载第 3 方库并手动安装它们或将它们添加到自己的本地存储库中。

What I'd like to have happen is for these couple of 3rd party libraries to simply be included in the jar file that I deploy to the central repository.我希望发生的是将这两个 3rd 方库简单地包含在我部署到中央存储库的 jar 文件中。 That way, if someone adds my library to their pom file, they really are done and don't have to worry about getting and installing the 3rd party libraries.这样,如果有人将我的库添加到他们的 pom 文件中,他们就真的完成了,不必担心获取和安装 3rd 方库。 Is it possible to do that?有可能这样做吗?

First off, I'll start by saying that you should back away as far as possible from the system scope.首先,我首先要说的是,您应该尽可能远离system范围。 You can refer to this answer for more information.您可以参考此答案以获取更多信息。

A way to circumvent your problem is indeed to include in the deployed JAR all the libraries that aren't present in Maven Central.规避问题的一种方法确实是在部署的 JAR 中包含 Maven Central 中不存在的所有库。 (Let's say you have installed those libraries in your local repository.) You can do that with themaven-shade-plugin , which is a plugin used to create uber jars. (假设您已经在本地存储库中安装了这些库。)您可以使用maven-shade-plugin来做到这一点,它是一个用于创建 uber jar 的插件。 The attribute artifactSet controls what will be included in the shaded JAR.属性artifactSet控制将包含在着色 JAR 中的内容。 In the following snippet, only the two dependencies groupId:artifactId and groupId2:artifactId2 will be included.在以下代码段中,仅包含两个依赖项groupId:artifactIdgroupId2:artifactId2

<plugin>
  <artifactId>maven-shade-plugin</artifactId>
  <version>2.4.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <artifactSet>
          <includes>
            <include>groupId:artifactId</include>
            <include>groupId2:artifactId2</include>
          </includes>
        </artifactSet>
      </configuration>
    </execution>
  </executions>
</plugin>

By default, the shaded JAR will replace your main artifact and this will be the JAR that will be deployed.默认情况下,阴影 JAR 将替换您的主要工件,这将是将被部署的 JAR。 The POM that will be deployed will not contain the dependency entries for the artifacts that were included in the shaded JAR , as such, a client depending on your deployed artifact won't be transitively-depending on them.将部署的 POM 将不包含包含在阴影 JAR 中的工件的依赖项条目,因此,依赖于您部署的工件的客户端不会传递依赖于它们。

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

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