将 spring boot maven 项目作为依赖添加到另一个项目(本地)

AKr*_*h95 4 java spring maven spring-boot

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

Run Code Online (Sandbox Code Playgroud)

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

.
??? 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

Run Code Online (Sandbox Code Playgroud)

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

// 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Cra*_*der 6

使用 Maven 模块,IntelliJ IDEA 将解决来自源而不是 jar 的依赖项。通过您的设置,依赖项是从 jars 解析的。

如果您想在其他项目中依赖此 jar,则这些.class文件需要位于 jar 的根目录中BOOT-INFBOOT-INF因为您使用的spring-boot-loader构建可执行文件 jar 的应用程序,所以这些类都在。

本文档介绍了如何解决此问题:

为了与另一个项目共享类,最好的方法是创建一个包含共享类的单独 jar,然后使其成为依赖它们的所有模块的依赖项。

...
<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

这将创建两个 jar,一个带有后缀 exec 作为可执行 jar,另一个作为我们可以包含在其他项目中的更典型的 jar。