简体   繁体   English

Maven:通过相对路径添加对jar的依赖

[英]Maven: add a dependency to a jar by relative path

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.原因是我希望我常用的 maven 命令(例如mvn compile等)开箱即用。 (Without demanding from the developers a to add it to some repository by themselves). (不需要开发人员自己将其添加到某个存储库)。

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 位于源代码管理的第 3 方库中,并通过 pom.xml 文件的相对路径链接到它。

Can this be done?这可以做到吗? How?如何?

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 use a system scoped dependency.如果你真的想要这个(理解,如果你不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”,而不是使用system范围的依赖项。 The system scoped should be avoided, such dependencies don't work well in many situation (eg in assembly), they cause more troubles than benefits.应该避免system作用域,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们造成的麻烦多于好处。

So, instead, declare a repository local to the project:因此,相反,声明一个项目本地存储库:

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

Install your third party lib in there using install:install-file with the localRepositoryPath parameter:使用带有localRepositoryPath参数的install:install-file第三方库:

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

Update: It appears that install:install-file ignores the localRepositoryPath when using the version 2.2 of the plugin.更新:在使用 2.2 版插件时, install:install-file似乎忽略了localRepositoryPath However, it works with version 2.3 and later of the plugin.但是,它适用于 2.3 版及更高版本的插件。 So use the fully qualified name of the plugin to specify the version:所以使用插件的全限定名来指定版本:

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 system scope):最后,像任何其他依赖项一样声明它(但没有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 system scope as your dependency will be treated like a good citizen (eg 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.现在,我不得不提到在公司环境中处理这种情况的“正确方法”(这里可能不是这种情况)是使用公司存储库。

Using the system scope.使用system范围。 ${basedir} is the directory of your pom. ${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 试图消除的。

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.这将在使用多模块构建时绕过限制,特别是如果下载的 JAR 在父项目之外的子项目中被引用。 This also reduces the setup work by creating the POM and the SHA1 files as part of the build.这也通过创建 POM 和 SHA1 文件作为构建的一部分来减少设置工作。 It also allows the file to reside anywhere in the project without fixing the names or following the maven repository structure.它还允许文件驻留在项目中的任何位置,而无需修复名称或遵循 maven 存储库结构。

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

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.然后,repository/pom.xml 文件将包含加载作为项目一部分的 JAR 的定义。 The following are some snippets of the pom.xml file.以下是 pom.xml 文件的一些片段。

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

The pom packaging prevents this from doing any tests or compile or generating any jar file. pom 包装阻止它进行任何测试或编译或生成任何 jar 文件。 The meat of the pom.xml is in the build section where the maven-install-plugin is used. 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.要安装多个文件,只需添加更多执行。

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>

I've previously written about a pattern for 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 提出的解决方案非常相似,尽管它将所有这些依赖项移动到一个专用的存储库模块中,这样您就不必在使用依赖项的任何地方重复它,如果它是一个多模块构建。

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>

we switched to gradle and this works much better in gradle ;).我们切换到 gradle,这在 gradle 中效果更好;)。 we just specify a folder we can drop jars into for temporary situations like that.我们只是指定一个文件夹,我们可以将 jars 放入这样的临时情况。 We still have most of our jars defined i the typicaly dependency management section(ie. the same as maven).我们仍然在典型的依赖管理部分(即与 maven 相同)定义了大部分 jar。 This is just one more dependency we define.这只是我们定义的另一种依赖。

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 存储库。

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

I was facing with the same issue, and it works just removing the DlocalRepositoryPath parameter and defining the correct path from current location in the Dfile parameter:我遇到了同样的问题,它只需删除 DlocalRepositoryPath 参数并在 Dfile 参数中从当前位置定义正确的路径即可:

mvn install:install-file -Dfile=./repo/com/tridion/cd_core/1.0/cd_core-1.0.jar -DgroupId=com.tridion -DartifactId=cd_core -Dversion=1.0 -Dpackaging=jar

Note: Apache Maven 3.8.6注:Apache Maven 3.8.6

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

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

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