0

我有 2 个应该导入到 Maven 项目中的 JAR。我按照本教程(单击此处)并将这些 JAR 导入到我的 maven 项目中。基本上,我在终端中执行了这段代码: mvn install:install-file -Dfile=myfile.jar -DgroupId=mygroup -DartifactId=com.mygroup.project -Dversion=1.0 -Dpackaging=jar -DlocalRepositoryPath=lib -DcreateChecksum=true 然后我将库导入到我的 Maven 项目中。

这一切都很好。但是,我要导入的 JAR 本身应该很少有依赖项。据我了解,Maven 自动处理内部依赖项。现在我有一个依赖项列表(带有组 ID、人工制品 ID 和版本),但我不明白我在哪里写这些。在库的 1.0 文件夹中,有一个名为 myjar-1.0.pom 的文件。我尝试在那里编写依赖项,但没有用。

你能告诉我一种手动告诉 Maven 加载一些依赖项的方法吗?

我还尝试在主pom.xml中指定这些依赖项,但它会导致错误 - 说找不到repo-url/dependency/file.pom。所以我想它只需要在内部依赖中提到 - 但我无法找到手动定义它们的方法。我需要在这些库中创建一个 pom.xml,还是我遗漏了什么?

4

3 回答 3

0

假设您有以下 JAR 和依赖项:

myMavenProject
--> library1.jar
    --> library2.jar
    --> library3.jar

(我假设您的,library1不是Maven 项目)library2library3

对于每个library1,library2library3,mvn install:install-file像您之前所做的一样执行:

mvn install:install-file -Dfile=library1.jar -DgroupId=com.mygroup.project -DartifactId=library1 -Dversion=1.0 -Dpackaging=jar

(请注意,我在这里交换了你的groupIdartifactId

然后在您的 Maven 项目中,更新您pom.xml的以下内容:

<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library1</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library2</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>com.mygroup.project</groupId>
    <artifactId>library3</artifactId>
    <version>1.0</version>
</dependency>

除非您有非常具体的要求,否则我不确定您为什么要-DlocalRepositoryPath=lib为 Maven 提供额外的选项,而不是使用本地 M2 存储库。

于 2013-07-24T02:46:08.293 回答
0

您正在遵循的教程可能有效,但它不是Maven 方式!如果你沿着这条路走,你将一路有麻烦!

您最好从maven 用户中心maven 书籍和资源页面阅读一些教程。Maven:例如,权威指南可在线免费获得。

Stephen Connolly 在他的一篇非常好的博客文章中提到了为什么您当前采用的方法不好以及如何做得更好(Stephen 是一位活跃的 maven 开发人员)。

于 2013-07-24T07:54:46.607 回答
0

你可以这样做:

  1. 为你的 jar 编写 pom,并在 pom.xml 中声明依赖项。
  2. 用户 mvn install:install-file -Dfile=path-to-your-artifact-jar -DpomFile=path-to-pom 来安装你的 jar。

链接:http ://maven.apache.org/plugins/maven-install-plugin/examples/custom-pom-installation.html

于 2013-07-24T02:36:34.597 回答