java Maven:指向多个 jar 的系统依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3490519/
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-10-30 02:03:03  来源:igfitidea点击:

Maven: System dependency pointing to multiple jars

javamaven-2

提问by digiarnie

Is it possible to define a dependency in the pom such that it has the scope of system but points to multiple jars?

是否可以在 pom 中定义一个依赖项,使其具有 system 范围但指向多个 jars?

I'm sure this is quite unorthodox, however, I was just wondering whether this was even possible. So something like:

我敢肯定这是非常不正统的,但是,我只是想知道这是否可能。所以像:

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

采纳答案by Pascal Thivent

First (and I'll never repeat it enough), using systemscoped dependencies is discouraged unless you know exactly what you're doing. From Dependency Scopes:

首先(我永远不会重复它),system除非您确切地知道自己在做什么,否则不鼓励使用作用域依赖项。从依赖范围

system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessary in certain edge cases. The system scope includes the <systemPath>element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPathelement can refer to environment variables in its path: ${JAVA_HOME}for instance.

system:在项目生命周期的某个阶段需要此依赖项,但它是特定于系统的。不鼓励使用此范围:这被认为是“高级”类型的功能,只有在您真正了解其使用的所有后果时才应使用,如果不是实际上无法量化,这可能非常困难。根据定义,此范围使您的构建不可移植。在某些边缘情况下可能有必要。系统范围包括<systemPath>指向本地机器上此依赖项的物理位置的 元素。因此,它用于指代某些预期存在于给定本地机器上而不是存储库中的工件;并且其路径可能因机器而异。这systemPathelement 可以在其路径中引用环境变量:${JAVA_HOME}例如。

Now, to strictly answer your question, declaring a dependency with a systemscope that would point on several jars is "possible" IFthe dependency has a MANIFEST.MFlisting other JARs relatively in its Class-Pathentry. Something like this (assuming the "root" dependency is in lib):

现在,为了严格回答您的问题,如果依赖项在其条目中相对列出了其他 JAR ,则声明具有system指向多个 jar的范围的依赖项是“可能的” 。像这样(假设“根”依赖项在 中):MANIFEST.MFClass-Pathlib

Class-Path: ../lib/bar.jar ../lib/foo.jar

But I do NOT recommend this approach, especially in your particular case. Instead, have a look at this previous answerwhere I describe how to setup a file-based repository.

但我不推荐这种方法,尤其是在您的特定情况下。相反,请查看之前的答案,我描述了如何设置基于文件的存储库。

回答by user1195526

As far as I understand, you are looking for a simple way to manage dependencies to local jar files (located in '${basedir}/lib/foo/' folder in your case). Using addjars-maven-pluginthat's simple. Just add the following declaration to your pom:

据我了解,您正在寻找一种简单的方法来管理对本地 jar 文件的依赖项(在您的情况下位于 '${basedir}/lib/foo/' 文件夹中)。使用addjars-maven-plugin很简单。只需将以下声明添加到您的 pom 中:

<plugin>
  <groupId>com.googlecode.addjars-maven-plugin</groupId>
  <artifactId>addjars-maven-plugin</artifactId>
  <version>1.0.2</version>
  <executions>
    <execution>
        <goals>
            <goal>add-jars</goal>
        </goals>
        <configuration>
            <resources>
                <resource>
                    <directory>${basedir}/lib/foo</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
  </executions>
</plugin>

回答by naikus

I've never done this but according to maven's core concepts, I think it may not be possible because every artifact is represented by a single entity (a jar, zip, tar, etc.). Hence it may not be possible to have multiple jars representing a single artifact.

我从来没有这样做过,但根据 maven 的核心概念,我认为这可能是不可能的,因为每个工件都由一个实体(jar、zip、tar 等)表示。因此,不可能有多个 jars 代表一个工件。

Morever system scopedependencies are assumed always available and not looked up in the repo. These should be only limited to jvm or jdk related dependencies (which are now provided by the jdk but earlier were available as separate downloads)

Morever系统范围依赖项被假定始终可用并且不在 repo 中查找。这些应该仅限于与 jvm 或 jdk 相关的依赖项(现在由 jdk 提供,但之前可以单独下载)

回答by aleksander_si

I have a similar problem; the proprietary software I am using does not have a public Maven repository, but is packaged with close to 200 jars. I have approached the problem as follows:

我有一个类似的问题; 我使用的专有软件没有公共 Maven 存储库,但打包了近 200 个 jar。我已经解决了这个问题,如下所示:

cd /folder
ls *.jar > folder.txt

Then replace each line of folder.txt

然后替换每一行folder.txt

^(.+)$

with

<dependency><groupId>folder</groupId><artifactId></artifactId><version>1.0</version><scope>system</scope><systemPath>${folder.root}/folder/</systemPath></dependency>

Copy the results to your pom.xml.

将结果复制到您的 pom.xml。