【问题标题】:Maven: add a dependency to a jar by relative pathMaven:通过相对路径将依赖项添加到 jar
【发布时间】:2016-04-09 09:16:00
【问题描述】:

我有一个专有的 jar,我想将它作为依赖项添加到我的 pom 中。

但我不想将它添加到存储库中。原因是我希望我常用的 maven 命令(如 mvn compile 等)开箱即用。 (无需开发人员自行将其添加到某个存储库中)。

我希望 jar 位于源代码控制中的 3rdparty 库中,并通过 pom.xml 文件中的相对路径链接到它。

这可以吗?怎么样?

【问题讨论】:

    标签: java maven-2 build-process build dependencies


    【解决方案1】:

    我希望 jar 位于源代码控制中的 3rdparty 库中,并通过 pom.xml 文件中的相对路径链接到它。

    如果您真的想要这个(请理解,如果您不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”并且不使用 system 范围依赖。应该避免system 作用域,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们带来的麻烦多于好处。

    因此,改为声明项目本地的存储库:

    <repositories>
      <repository>
        <id>my-local-repo</id>
        <url>file://${project.basedir}/my-repo</url>
      </repository>
    </repositories>
    

    使用install:install-filelocalRepositoryPath 参数在其中安装您的第三方库:

    罢工>

    mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \ 
                             -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                             -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
    

    更新: 使用 2.2 版插件时,install:install-file 似乎忽略了 localRepositoryPath。但是,它适用于 2.3 及更高版本的插件。所以使用插件的全限定名来指定版本:

    mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
                             -Dfile=<path-to-file> -DgroupId=<myGroup> \ 
                             -DartifactId=<myArtifactId> -Dversion=<myVersion> \
                             -Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
    

    maven-install-plugin documentation

    最后,像任何其他依赖项一样声明它(但没有system 范围):

    <dependency>
      <groupId>your.group.id</groupId>
      <artifactId>3rdparty</artifactId>
      <version>X.Y.Z</version>
    </dependency>
    

    恕我直言,这是一个比使用 system 范围更好的解决方案,因为您的依赖项将被视为一个好公民(例如,它将被包含在程序集中等等)。

    现在,我不得不提一下,在公司环境中处理这种情况的“正确方法”(可能不是这里的情况)是使用公司存储库。

    【讨论】:

    • 这是个好主意,但在 Maven 2.2.1 上,安装插件似乎忽略了 localRepositoryPath...
    • 为什么要声明本地仓库?为什么不让它和其他的一起进入 ~/.m2/。
    • @leif81 因为随后 repo 和库被检入 SCM 存储库 -> 任何进行源检出的人都拥有构建库/应用程序副本所需的一切。
    • 我遇到了与@lemon 相同的问题,我通过使用basedir/./my-local-repo 和一个. 来解决这个问题。
    • 包装必须是jar,因此-Dpackaging=jar
    【解决方案2】:

    使用system 范围。 ${basedir} 是你的 pom 的目录。

    <dependency>
        <artifactId>..</artifactId>
        <groupId>..</groupId>
        <scope>system</scope>
        <systemPath>${basedir}/lib/dependency.jar</systemPath>
    </dependency>
    

    但是,建议您将 jar 安装在存储库中,而不是将其提交给 SCM - 毕竟这是 maven 试图消除的。

    【讨论】:

    • 必须尽可能避免使用示波器系统。在存储库中安装 JAR 是更好的解决方案...
    • 是的,如果可能的话。他明确表示他不想将其放入存储库中。我添加了一条评论,指出这不是一个好习惯。但它确实有效。
    • groovy,我猜你的解决方案是迄今为止最可以接受的。我完全误读了这个问题
    • 是的 - 问题本身不包括最佳答案。将所有内容放在您的单个源代码控制服务器中与“开箱即用”无关;相反,一切都必须“控制”。签入 pom 的 & settings.xml(指向 internal 存储库),并为您的项目使用 两个 服务器:(1) 源代码控制,(2) 生成的工件控制。检查 jars 和检查 dll 一样有意义(我的旧公司实际上确实检查了 jars 和 lib.a/.so/.dll 的。我们的 p4 服务器之后太慢了,有些人偷偷使用 hg 一天日常工作。问题解决了吗?
    • 任何方式来指定一个包含 jars 的目录,这样我们就不必像 gradle 一样添加每个目录了吗?
    【解决方案3】:

    这是我之前在Can I add jars to maven 2 build classpath without installing them?的回答之外的另一种方法

    这将在使用多模块构建时绕过限制,尤其是在父项目之外的子项目中引用下载的 JAR 时。这还通过在构建过程中创建 POM 和 SHA1 文件来减少设置工作。它还允许文件驻留在项目中的任何位置,而无需固定名称或遵循 maven 存储库结构。

    这使用了 maven-install-plugin。为此,您需要设置一个多模块项目并有一个代表构建的新项目,以将文件安装到本地存储库并确保第一个。

    你的多模块项目 pom.xml 看起来像这样:

    <packaging>pom</packaging>
    <modules>
    <!-- The repository module must be first in order to ensure
         that the local repository is populated -->
        <module>repository</module>
        <module>... other modules ...</module>
    </modules>
    

    repository/pom.xml 文件将包含用于加载作为项目一部分的 JAR 的定义。以下是pom.xml文件的一些sn-ps。

    <artifactId>repository</artifactId>
    <packaging>pom</packaging>
    

    pom 打包阻止它进行任何测试或编译或生成任何 jar 文件。 pom.xml 的内容在使用 maven-install-plugin 的构建部分。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                            <id>com.ibm.db2:db2jcc</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>install-file</goal>
                            </goals>
                            <configuration>
                                <groupId>com.ibm.db2</groupId>
                                <artifactId>db2jcc</artifactId>
                                <version>9.0.0</version>
                                <packaging>jar</packaging>
                                <file>${basedir}/src/jars/db2jcc.jar</file>
                                <createChecksum>true</createChecksum>
                                <generatePom>true</generatePom>
                            </configuration>
                    </execution>
                    <execution>...</execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    要安装多个文件,只需添加更多执行。

    【讨论】:

    • 这是唯一适用于我的多模块项目的东西。由于未知原因,本地 方法不起作用。所以谢谢!
    【解决方案4】:

    这对我有用: 假设我有这个依赖

    <dependency>
        <groupId>com.company.app</groupId>
        <artifactId>my-library</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/my-library.jar</systemPath>
    </dependency>
    

    然后,像这样手动添加系统依赖项的类路径

    <Class-Path>libs/my-library-1.0.jar</Class-Path>
    

    完整配置:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <archive>
                <manifestEntries>
                    <Build-Jdk>${jdk.version}</Build-Jdk>
                    <Implementation-Title>${project.name}</Implementation-Title>
                    <Implementation-Version>${project.version}</Implementation-Version>
                    <Specification-Title>${project.name} Library</Specification-Title>
                    <Specification-Version>${project.version}</Specification-Version>
                    <Class-Path>libs/my-library-1.0.jar</Class-Path>
                </manifestEntries>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.company.app.MainClass</mainClass>
                    <classpathPrefix>libs/</classpathPrefix>
                </manifest>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/libs/</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案5】:

      我之前曾 written about a pattern 这样做过。

      它与 Pascal 提出的解决方案非常相似,尽管它将所有此类依赖项移动到一个专用的存储库模块中,因此如果它是一个多模块构建,您不必在任何使用依赖项的地方重复它。

      【讨论】:

        【解决方案6】:

        基本上,将其添加到 pom.xml:

        ...
        
        <repositories>
           <repository>
               <id>lib_id</id>
               <url>file://${project.basedir}/lib</url>
           </repository>
        </repositories>
        
        ...
        
        <dependencies>
          ...
          <dependency>
              <groupId>com.mylibrary</groupId>
              <artifactId>mylibraryname</artifactId>
              <version>1.0.0</version>
          </dependency>
          ...
        </dependencies>
        

        【讨论】:

          【解决方案7】:

          我们切换到 gradle,这在 gradle 中效果更好;)。我们只需指定一个文件夹,我们可以将罐子放入此类临时情况。我们仍然在典型的依赖管理部分(即与 maven 相同)中定义了大部分 jar。这只是我们定义的另一种依赖关系。

          所以现在基本上我们可以将任何我们想要的 jar 放到我们的 lib 目录中以进行临时测试,如果它不是某个地方的 maven 存储库。

          【讨论】:

          • 你能举个例子吗?
          • 它看起来像这样的解决方案:stackoverflow.com/a/23897638/458157 顺便说一句,很棒的建议,我完全同意,Maven 驱动这种用例非常复杂,而 Gradle 提供了一个更智能的解决方案,以我的拙见。
          【解决方案8】:

          Pascal 发布的解决方案的一个小补充

          当我遵循这条路线时,我在安装 ojdbc jar 时在 maven 中遇到错误。

          [INFO] --- maven-install-plugin:2.5.1:install-file (default-cli) @ validator ---
          [INFO] pom.xml not found in ojdbc14.jar
          

          添加-DpomFile后,问题解决了。

          $ mvn install:install-file -Dfile=./lib/ojdbc14.jar -DgroupId=ojdbc \
             -DartifactId=ojdbc -Dversion=14 -Dpackaging=jar -DlocalRepositoryPath=./repo \
             -DpomFile=~/.m2/repository/ojdbc/ojdbc/14/ojdbc-14.pom
          

          【讨论】:

            【解决方案9】:

            您可以使用 eclipse 生成一个可运行的 Jar : 导出/可运行 Jar 文件

            【讨论】:

            • 不确定是否回答了这个问题。他已经将文件作为 jar 文件。
            • Eclipse 支持 uberjar 或 shaded jar,所以它是一个解决方案,但不适用于 maven
            猜你喜欢
            • 2018-01-08
            • 1970-01-01
            • 2014-11-18
            • 2019-09-03
            • 1970-01-01
            • 2015-09-27
            • 2017-01-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多