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

查看:125
本文介绍了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)将.jar文件添加到的最佳选择是什么?鉴于我希望项目和库都在源代码控制中,我的项目引用maven?

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的Dependencies列表中显示正常。 mvn compile和mvn包都成功,但运行程序会导致:

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填充本地存储库,然后你应该将范围从system更改为compile。

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.

至于错误,你做了吗?你的类路径上需要的罐子?如果您使用库中的类型,则还需要在运行时访问它。这与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天全站免登陆