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

标签 java spring spring-boot maven

我希望在另一个项目中使用一个 SB 项目作为依赖项——而不使用父 pom 和本地。我在使用父 pom 并指定模块之前已经这样做了,但是我正在考虑拆分 repo 并且需要在没有父 pom 的情况下实现相同的目标。

我发现了一些 SO 帖子概述了这样做的方法,但它们似乎都不适合我。他们都涉及mvn install生成 Artifact ,以便它在本地存储库中可用。这似乎对我有用,直到它不起作用。

注意:我在公司环境中工作,我确实计划将这些 jar 部署到我们的内部 Nexus 存储库,但是,我想先弄清楚本地开发,然后再深入研究这条路线。

我的设置是两个空的 start.spring.io 项目(名称不同)。

.
├── 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


在一个项目中,test-client ,我定义了一个新类

// 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;
    }
}

只是一些基本的测试类,我将在我的消费者应用程序中制作一个 bean。

接下来,我运行 mvn clean install并确认它在我的 .m2/repository 中文件夹

enter image description here

现在在 test-application
//pom.xml

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

IntelliJ 自动导入,一切看起来都很好,没有红色。

接下来,在一个新文件内 TestClientConfig.java我开始实现我的客户:

enter image description here

IntellJ 从依赖项中提取新类并建议它们。但是,当我尝试导入时,事情并不顺利。

它将导入完整的包名,然后无法识别它:
enter image description here

而且我无法添加导入语句。

所以我被困在这一点上。我曾尝试在 IntelliJ 中调整一些设置,将编译后的 jar 包含为库或添加模块,但似乎没有什么能完全正常工作,而且这些选项似乎有点 hacky。

这是 zip 的链接:https://drive.google.com/open?id=13XGVzICO_QHn_ihM7NFtK3GobAxeqLYf

最佳答案

使用 Maven 模块,IntelliJ IDEA 将解决来自源而不是 jar 的依赖关系。通过您的设置,依赖关系从 jar 中解析。
.class文件需要位于 jar 的根目录而不是 BOOT-INF如果你想在其他项目中依赖这个 jar。类(class)在BOOT-INF因为您使用的是 spring-boot-loader申请builds the executable jar .

This document描述了如何解决该问题:

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.

关于java - 将spring boot maven项目作为依赖添加到另一个项目(本地),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60676216/

相关文章:

java - Spring Security 中针对 @PreAuthorize AccessDeniedException 的错误处理返回 500 而不是 401

java - 有没有办法强制在运行时加载类

java - Spring 数据 JPA 存储库不保存到数据库

java - 在多节点环境中仅触发一次调度作业

java - 如何在 Spring Boot OIDC 应用程序的 Controller 中获取用户详细信息?

java - 将可序列化对象作为参数传递时出现 GWT SerializationException

ajax - 将 Spring 应用程序从 Glassfish 迁移到 Tomcat

Java Hibernate java.lang.IllegalArgumentException : Unknown parameter position: 1

java - Spring Boot 错误 Controller 检索原始请求

java - 计算周、月和年的第一天和最后一天的最佳方法