Maven:将自定义外部 JAR 链接到我的项目的最佳方式? [英] Maven: best way of linking custom external JAR to my project?

查看:31
本文介绍了Maven:将自定义外部 JAR 链接到我的项目的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我学习 Maven 的头几天,但我仍在为基础知识苦苦挣扎.我有一个外部 .jar 文件(在公共存储库中不可用),我需要在我的项目中引用它,我正在尝试找出我最好的选择.

It's my first couple of days learning Maven and I'm still struggling with the basics. I have an external .jar file (not available in the public repos) that I need to reference in my project and I'm trying to figure out what my best option is.

这是一个没有库中央存储库的小规模项目,因此它必须是本地存储库(以某种方式添加到源代码管理中,不知道它是否应该以这种方式工作?)或 .jar 需要存储在任何正式存储库之外的磁盘上.

It's a small scale project without a central repository for libraries, so it has to be either a local repository (somehow added to source control, don't know if it's supposed to work that way?) or the .jar needs to be stored on disk outside of any formal repository.

1) 考虑到我希望项目和库都在源代码管理中,使用 maven 将 .jar 文件添加到我的项目引用中的最佳选择是什么?

1) What's my best option for adding the .jar file to my project's references with maven given that I want both the project and the library to be in source control?

2) 我似乎仍然无法让 Eclipse 看到依赖项.我手动将它添加到 pom 的部分,它在 m2eclipse 的依赖项列表中显示良好.mvn compile 和 mvn package 都成功了,但是运行程序的结果是:

2) I still can't seem to have Eclipse see the dependency. I manually added it to the section of the pom, and it shows up fine in the Dependencies list in m2eclipse. mvn compile and mvn package both succeed, but running the program results in:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
        LibraryStuff cannot be resolved to a type

这是将 POM 编辑为:

This is after editing the POM as:

<dependency>
  <groupId>stuff</groupId>
  <artifactId>library</artifactId>
  <version>1.0</version>
  <systemPath>${lib.location}/MyLibrary.jar</systemPath>
  <scope>system</scope>
</dependency>

我是否应该执行 mvn install:install-file 甚至认为我已经像上面那样编辑了 pom.xml?

Should I be executing mvn install:install-file even thought I already have the pom.xml edited as above?

谢谢!

推荐答案

我认为您应该使用 mvn install:install-file 用库 jar 填充您的本地存储库,然后您应该更改范围从系统编译.

I think you should use mvn install:install-file to populate your local repository with the library jars then you should change the scope from system to compile.

如果您从 maven 开始,我建议直接使用 maven 而不是 IDE 插件,因为它会增加额外的复杂性.

If you are starting with maven I suggest to use maven directly not IDE plugins as it adds an extra layer of complexity.

至于错误,您是否将所需的 jars 放在您的类路径中?如果您使用的是库中的类型,则还需要在运行时访问它.这与 maven 本身无关.

As for the error, do you put the required jars on your classpath? If you are using types from the library, you need to have access to it in the runtime as well. This has nothing to do with maven itself.

我不明白您为什么要将库置于源代码控制中 - 它用于源代码而不是二进制 jar.

I don't understand why you want to put the library to source control - it is for sources code not binary jars.

这篇关于Maven:将自定义外部 JAR 链接到我的项目的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆