Java Maven:通过相对路径向 jar 添加依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2229757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:59:20  来源:igfitidea点击:

Maven: add a dependency to a jar by relative path

javamaven-2build-processbuilddependencies

提问by flybywire

I have a proprietary jar that I want to add to my pom as a dependency.

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

But I don't want to add it to a repository. The reason is that I want my usual maven commands such as mvn compile, etc, to work out of the box. (Without demanding from the developers a to add it to some repository by themselves).

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

I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.

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

Can this be done? How?

这能做到吗?如何?

采纳答案by Pascal Thivent

I want the jar to be in a 3rdparty lib in source control, and link to it by relative path from the pom.xml file.

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

If you really want this (understand, if you can't use a corporate repository), then my advice would be to use a "file repository" local to the project and to not usea systemscoped dependency. The systemscoped should be avoided, such dependencies don't work well in many situation (e.g. in assembly), they cause more troubles than benefits.

如果你真的想这个(明白,如果你不能使用公司资源库),那么我的建议是使用“文件库”本地的项目,并没有使用一个system范围依赖。该system作用域应避免这种依赖不会在许多情况下很好地工作(例如,在组装),他们会大于收益的麻烦。

So, instead, declare a repository local to the project:

因此,相反,声明一个项目本地存储库:

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

Install your third party lib in there using install:install-filewith the localRepositoryPathparameter:

使用中有你安装第三方的libinstall:install-filelocalRepositoryPath参数:

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

Update:It appears that install:install-fileignores the localRepositoryPathwhen using the version 2.2 of the plugin. However, it works with version 2.3 and later of the plugin. So use the fully qualified name of the plugin to specify the version:

更新:使用 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

maven-install-plugin 文档

Finally, declare it like any other dependency (but without the systemscope):

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

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

This is IMHO a better solution than using a systemscope as your dependency will be treated like a good citizen (e.g. it will be included in an assembly and so on).

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

Now, I have to mention that the "right way" to deal with this situation in a corporate environment (maybe not the case here) would be to use a corporate repository.

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

回答by Bozho

Using the systemscope. ${basedir}is the directory of your pom.

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

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

However it is advisable that you install your jar in the repository, and not commit it to the SCM - after all that's what maven tries to eliminate.

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

回答by Brett Porter

I've previously written about a patternfor doing this.

我以前写过关于这样做的模式

It is very similar to the solution proposed by Pascal, though it moves all such dependencies into a dedicated repository module so that you don't have to repeat it everywhere the dependency is used if it is a multi-module build.

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

回答by Archimedes Trajano

This is another method in addition to my previous answer at Can I add jars to maven 2 build classpath without installing them?

除了我之前在Can I add jars to maven 2 build classpath without install它们?

This will get around the limit when using multi-module builds especially if the downloaded JAR is referenced in child projects outside of the parent. This also reduces the setup work by creating the POM and the SHA1 files as part of the build. It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.

这将在使用多模块构建时绕过限制,特别是如果下载的 JAR 在父项目之外的子项目中被引用。这也通过创建 POM 和 SHA1 文件作为构建的一部分来减少设置工作。它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循 maven 存储库结构。

This uses the maven-install-plugin. For this to work, you need to set up a multi-module project and have a new project representing the build to install files into the local repository and ensure that one is first.

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

You multi-module project pom.xml would look like this:

您的多模块项目 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>

The repository/pom.xml file will then contain the definitions to load up the JARs that are part of your project. The following are some snippets of the pom.xml file.

然后,repository/pom.xml 文件将包含加载作为项目一部分的 JAR 的定义。以下是 pom.xml 文件的一些片段。

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

The pom packaging prevents this from doing any tests or compile or generating any jar file. The meat of the pom.xml is in the build section where the maven-install-plugin is used.

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>

To install more than one file, just add more executions.

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

回答by Dean Hiller

we switched to gradle and this works much better in gradle ;). we just specify a folder we can drop jars into for temporary situations like that. We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven). This is just one more dependency we define.

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

so basically now we can just drop any jar we want into our lib dir for temporary testing if it is not a in maven repository somewhere.

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

回答by veeseekay

One small addition to the solution posted by Pascal

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

When I followed this route, I got an error in maven while installing ojdbc jar.

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

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

After adding -DpomFile, the problem was resolved.

添加-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

回答by Fulgencio Jara

Basically, add this to the pom.xml:

基本上,将其添加到 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>

回答by user1942990

You can use eclipse to generate a runnable Jar : Export/Runable Jar file

您可以使用 eclipse 生成可运行的 Jar : Export/Runable Jar 文件

回答by RufusSC2

This is working for me: Let's say I have this dependency

这对我有用:假设我有这种依赖性

<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>

Then, add the class-path for your system dependency manually like this

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

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

Full config:

完整配置:

<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>