簡體   English   中英

Spring 引導項目 jar 作為另一個項目中的依賴項

[英]Spring Boot project jar as dependency in another project

** gradle 的新功能

我需要使用現有的 spring 啟動項目(它安裝在本地 maven 存儲庫中)作為另一個項目(Spring 啟動 gradle 項目)的依賴項。

使用 mavenLocal 我能夠成功運行和構建 gradle 項目,但是當我嘗試使用其他項目中存在的任何 Class 時,我無法導入它。

我如何使用歸類在此 gradle 項目中的 maven 項目?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>tech.thegamedefault</groupId>
    <artifactId>demo-lib</artifactId>
    <version>0.0.1</version>
    <name>demo-lib</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

演示主要 build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'tech.thegamedefault'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'


configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'tech.thegamedefault:demo-lib:0.0.1'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

在此處輸入圖像描述

在此處輸入圖像描述

**** 更新 ****

最終目標是能夠通過使用@Component 掃描自動連接演示庫項目中存在的 class。

注意:如果我創建一個胖 spring 引導 jar 並將其導入另一個 maven 項目,我就能夠做到這一點。

package tech.thegamedefault.demomain;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import tech.thegamedefault.demolib.utility.SuperUtility;

@SpringBootApplication
@ComponentScan("tech.thegamedefault.utility")
public class DemoMainApplication {

    @Autowired
    SuperUtility superUtility;

    @PostConstruct
    public void callSomethingSuper() {
        System.out.println(superUtility.getSomethingSuper());
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoMainApplication.class, args);
    }

}

使用默認 maven 構建插件時遇到以下錯誤在此處輸入圖像描述

*********** 更新 **********

我在組件掃描中的錯誤 package 是錯誤的。

它與 @ComponentScan("tech.thegamedefault.demolib") 一起工作正常

由於您使用 spring-boot-maven-plugin 來構建 demo-lib 的可運行 jar,因此它打包的類與普通的 jar 略有不同。 您的課程在 BOOT-INF/classes 中。而不是在根目錄中。

您可以簡單地刪除 spring-boot-maven-plugin 並依靠默認插件來創建 jar 一切都應該沒問題。

其他解決方案是使用配置文件來選擇使用 spring-boot-maven-plugin。 例如:默認情況下,它會生成可運行的 jar。 並使用自定義配置文件跳過它。

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM