0

I have created two spring boot applications - foo and bar. 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. 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.

Then in the pom.xml of foo, I added the following dependency:

<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:

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.

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.

I have even tried adding a local maven repository as mentioned in this answer but even that doesn't seem to work. 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?

Kshitij Bajracharya
  • 811
  • 2
  • 14
  • 37
  • You should run mvn install to publish this JAR to your local .m2. Use mvn deploy task to publish it to a remote repository that you have write access to. – duffymo May 20 '19 at 12:35
  • You can't use a spring boot application as a dependency. If you need to share code make a separate module name it like `commons-code` or similar and use that as a dependency. – khmarbaise May 20 '19 at 12:45
  • @duffymo I have run mvn install from bar, and it is available in my local .m2. But when I add this dependency in foo and then run mvn install, it cannot find the service defined in bar – Kshitij Bajracharya May 21 '19 at 04:58
  • @khmarbaise Can I convert an existing spring Spring boot application with packaging war into a multi-module application? Also, if there is a separate project baz that needs to use the services defined in bar, is it possible to use this module? – Kshitij Bajracharya May 21 '19 at 05:00
  • Yes of course you can... – khmarbaise May 21 '19 at 10:04

2 Answers2

1

You missed artifactid, version and groupid, please use below command.

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

Mahadev Shinde
  • 119
  • 3
  • 9
0

I have done mvn clean install to publish it to .m2 folder. 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. I can start application with mvn spring-boot:run. This worked for me.

  • I had previously added @ComponentScan(basePackages = "com.foobarcompany") in the main class which should have picked both foo and bar. I tried with your method as well but it still doesn't work. – Kshitij Bajracharya May 21 '19 at 04:29