简体   繁体   English

将本地jar添加到Spring启动应用程序的pom.xml

[英]Add local jar to pom.xml of Spring boot application

I have created two spring boot applications - foo and bar . 我创建了两个spring boot应用程序foobar I then created a jar file for bar with the command mvn clean package , which resulted in the bar-0.0.1-SNAPSHOT.jar file. 然后,我使用命令mvn clean packagebar创建了一个jar文件,生成了bar-0.0.1-SNAPSHOT.jar文件。 Using mvn install:install-file -Dfile=target/bar-0.0.1-SNAPSHOT.jar -DpomFile=pom.xml as mentioned in this post , I installed this jar to my local .m2 repository. 使用mvn install:install-file -Dfile=target/bar-0.0.1-SNAPSHOT.jar -DpomFile=pom.xml在提到这个职位 ,我安装了这个罐子我的本地库的.m2。

Then in the pom.xml of foo , I added the following dependency: 然后在foo的pom.xml中,添加了以下依赖项:

<dependency>
    <groupId>com.foobarcompany</groupId>
    <artifactId>bar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Now, when I run the foo app from STS, it runs correctly, but if I try to run it form terminal with mvn spring-boot:run or try to package foo with mvn clean package , I get the following error: 现在,当我从STS运行foo应用程序时,它可以正常运行,但是如果我尝试使用mvn spring-boot:run从终端运行它,或尝试使用mvn clean package打包foo ,则会出现以下错误:

package com.foobarcompany.bar.service does not exist

which is referring to a service present in the bar app that I'm calling from the foo app. 指的是我从foo应用中调用的bar应用中存在的服务。

As far as I understand, the dependency is not correctly added and the reason it works from STS is because both applications are under the same workspace. 据我了解,该依赖关系未正确添加,并且它在STS中起作用的原因是因为两个应用程序都在同一工作空间下。

I have even tried adding a local maven repository as mentioned in this answer but even that doesn't seem to work. 我什至尝试添加此答案中提到的本地Maven存储库,但即使这样似乎也不起作用。 Or maybe I'm doing it wrong. 也许我做错了。

Could anyone please tell me what the correct way to add a local jar to another application is? 谁能告诉我将本地jar添加到另一个应用程序的正确方法是什么?

You missed artifactid, version and groupid, please use below command. 您错过了artifactid,版本和groupid,请使用以下命令。

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id>
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

Reference: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html 参考: https : //maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

I have done mvn clean install to publish it to .m2 folder. 我已经完成了mvn clean install并将其发布到.m2文件夹。 After that I have added it as dependency (which you have already done). 之后,我将其添加为依赖项(您已经完成了)。 Last step is to add @ComponentScan(basePackages = { "com.foobarcompany.bar", "com.foobarcompany.foo" }) on your main class, where your @SpringBootApplication is placed. 最后一步是在放置@SpringBootApplication的主类上添加@ComponentScan(basePackages = {“ com.foobarcompany.bar”,“ com.foobarcompany.foo”})。 I can start application with mvn spring-boot:run. 我可以使用mvn spring-boot:run启动应用程序。 This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM