java - 本地 jar 不包含在类路径 (`<scope>system</scope>` 中)

标签 java maven-2

我正在尝试使用 maven 和 eclipse 构建我的应用程序。 我依赖于本地计算机上的 3rd 方 jar。 这是我的 pom.xml

<dependency>
    <groupId>sourceforge.net</groupId>
   <artifactId>zipdiff</artifactId>
   <version>0.4</version>
   <scope>system</scope>
   <systemPath>C:/gelcap/lib/zipdiff-0.4.jar</systemPath>
</dependency>

<dependency>
    <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.11</version>
</dependency>

当我运行 mvn : install 时,它会为我的项目创建 war 文件。 但问题是它不将 zipdiff.jar 文件包含到 web-inf/lib 文件夹中,它只包含下载的文件。我还需要包括从我的本地系统复制文件,但 Maven 会忽略它们。 我没有想过为什么会发生为什么 Maven 没有将具有系统范围的文件包含到我的 war 文件中。 请告诉我如何解决这个问题。 提前致谢

最佳答案

I have not got any thought why it is happening why maven is not including files with system scope to my war file. Please give me any idea how this problem can be resolved.

这是设计使然,system范围内的依赖项应该作为 documented 提供.

实际上,我已经多次写过(herehereherehere)system应避免作用域依赖。大多数情况下,它们是一种不好的做法,人们在滥用它们,而且它们带来的麻烦几乎总是多于好处。

让我引用 Dependency Scopes迷你指南,如果你想要一个“官方”的观点:

  • 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 systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.

所以,不要使用 system范围,要么:

  • 通过 install:install-file 将您的库添加到本地存储库.这是一种使事情正常进行的快速而肮脏的方法,如果您一个人,它可能是一种选择,但它会使您的构建不可移植。
  • 安装并运行“企业存储库”,例如 Nexus、Archiva 或 Artifactory,并通过 deploy:deploy-file 添加您的库.这是理想的场景。
  • 按照 this previous answer 中的说明设置基于文件的存储库并将你的图书馆放在那里。如果您没有公司存储库但需要作为一个团队工作并且不想牺牲可移植性,那么这是最佳折衷方案。

请停止使用 system范围。

关于java - 本地 jar 不包含在类路径 (`<scope>system</scope>` 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280834/

相关文章:

java - Mockito 测试用例忽略注释

maven-2 - 嵌套 Maven 属性在引用 POM 时进行评估,而不是定义 POM

maven-2 - 监控 nexus repo

xml - 将 JDBC url 从 Maven 传递到 hibernate.cfg.xml

java - 如何使用Spring Boot从数据库创建实体类?

java - Android 的 localhost 是什么?

java - StyledEditorKit 文本对齐无法正常工作

java - 如何检查二维数组中的所有线是否具有相同的大小

java - Maven FindBugs 插件

java - 什么是 maven settings.xml 文件以及它与 pom.xml 有何不同