简体   繁体   中英

Add spring boot maven project as dependency to another project (locally)

I am looking to use one SB project as a dependency in another project -- without using a parent pom and locally. I have done this before using a parent pom and specifying modules, but I am looking at splitting up the repo and need to achieve the same without the parent pom.

I have found a few SO posts outlining ways of doing this, but none of them seem to work for me. They all involve mvn install ing the artifact so that it's available in the local repo. And that seems to work for me, until it doesn't.

Note: I am working in a corporate environment and I do plan to deploy these jars to our internal Nexus repo, however, I would like to figure out local development first before diving down this route.

My set up is two empty start.spring.io projects (with different names).

.
├── test-application
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── example
│           │           └── testapplication
│           │               ├── TestApplication.java
│           │               └── TestClientConfig.java
│           └── resources
│               └── application.properties
│   
└── test-client
    ├── pom.xml
    └── src
        └── main
            ├── java
            │   └── com
            │       └── example
            │           └── testclient
            │               ├── TestClient.java
            │               └── TestClientApplication.java
            └── resources
                └── application.properties

In one project, test-client , I define a new class

// TestClient.java

public class TestClient {

    private String value;

    public TestClient(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

Just some basic test class that I will make into a bean in my consumer application.

Next, I run mvn clean install and verify that it's in my .m2/repository folder

在此处输入图片说明

And now in the test-application

//pom.xml

<dependency>
    <groupId>com.example</groupId>
    <artifactId>test-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

IntelliJ auto-imports and everything looks fine, no red.

Next, inside of a new file TestClientConfig.java I start implementing my client:

在此处输入图片说明

IntellJ picks up the new classes from the dependency and suggests them. However, when I try to import, things don't work out too well.

It will import the full package name and then not recognize it: 在此处输入图片说明

And I can't add an import statement.

So I am stuck at this point. I have tried finessing some settings in IntelliJ to include the compiled jar as a library or to add a module but nothing really seemed to work fully and those options seemed kind of hacky.

Here's a link to the zip: https://drive.google.com/open?id=13XGVzICO_QHn_ihM7NFtK3GobAxeqLYf

With Maven modules IntelliJ IDEA will resolve the dependencies from sources instead of jars. With your setup the dependencies are resolved from jars.

The .class files need to be in the root of the jar instead of BOOT-INF if you want to depend on this jar in the other projects. The classes are in BOOT-INF since you are using spring-boot-loader application which builds the executable jar .

This document describes how to workaround the problem:

In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them.

...
<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>

This will create two jars, one with the suffix exec as an executable jar, and another as a more typical jar that we can include in other projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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