简体   繁体   English

Spring 引导项目 jar 作为另一个项目中的依赖项

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

**New to gradle ** gradle 的新功能

I have a requirement to use an existing spring boot project (It is installed in local maven repository) as a dependency in another project (Spring boot gradle project).我需要使用现有的 spring 启动项目(它安装在本地 maven 存储库中)作为另一个项目(Spring 启动 gradle 项目)的依赖项。

Using mavenLocal i am able to successfully run and build gradle project, but when i try to use any Class that is present in other project then i am unable to import it.使用 mavenLocal 我能够成功运行和构建 gradle 项目,但是当我尝试使用其他项目中存在的任何 Class 时,我无法导入它。

How can i use maven project classed in this gradle project?我如何使用归类在此 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>

demo-main build.gradle演示主要 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()
}

在此处输入图像描述

在此处输入图像描述

**** UPDATED **** **** 更新 ****

End goal is to able to autowire the class that is present in the demo-lib project by using @Component scan.最终目标是能够通过使用@Component 扫描自动连接演示库项目中存在的 class。

Note: If i create a fat spring boot jar and import it in another maven project than i am able to do that.注意:如果我创建一个胖 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);
    }

}

Facing below error while using default maven build plugin使用默认 maven 构建插件时遇到以下错误在此处输入图像描述

*********** UPDATED ********** *********** 更新 **********

My bad package in Component scan was wrong.我在组件扫描中的错误 package 是错误的。

Its working fine with @ComponentScan("tech.thegamedefault.demolib")它与 @ComponentScan("tech.thegamedefault.demolib") 一起工作正常

Since you are using spring-boot-maven-plugin to build the runnable jar of demo-lib, its packaging the classes slightly different than a normal jar.由于您使用 spring-boot-maven-plugin 来构建 demo-lib 的可运行 jar,因此它打包的类与普通的 jar 略有不同。 Your classes are inside BOOT-INF/classes.. not at the root.您的课程在 BOOT-INF/classes 中。而不是在根目录中。

You can simply remove the spring-boot-maven-plugin and rely on the default plugin to create the jar and everything should be fine.您可以简单地删除 spring-boot-maven-plugin 并依靠默认插件来创建 jar 一切都应该没问题。

Other solution would be to use profiles to optionally use the spring-boot-maven-plugin.其他解决方案是使用配置文件来选择使用 spring-boot-maven-plugin。 eg: by default it would produce the runnable jar.例如:默认情况下,它会生成可运行的 jar。 And use a custom profile to skip it.并使用自定义配置文件跳过它。

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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Heroku 中的引导项目作为对另一个本地 Spring 引导项目的依赖项? - Spring boot project in Heroku as dependency to another local Spring Boot project? 将一个 spring-boot 项目作为 jar 添加到另一个项目中 - Add one spring-boot project as a jar to another project 将 spring boot maven 项目作为依赖添加到另一个项目(本地) - Add spring boot maven project as dependency to another project (locally) 在Spring Boot项目中找不到依赖项 - Dependency not found in Spring Boot project 如何在另一个Spring启动项目中添加本地spring启动项目作为依赖项 - How to add local spring boot project as a dependency in another spring boot project 如何在不改变原有pom的情况下,在新的spring-boot项目中添加spring-boot jar作为依赖 - How to add a spring-boot jar as a dependency in a new spring-boot project without changing the original pom 将生成的胖 jar 复制到 Spring 引导项目中的另一个目标文件夹 - Copy resulting fat jar to another target folder in Spring boot project 在另一个项目中运行 spring 引导而不使用 @SpringBootApplication 注释作为 jar 库 - Run spring boot without @SpringBootApplication annotation as jar library in another project 如何从spring boot项目创建jar,我们要在另一个spring boot应用中使用的jar? - how to create jar from spring boot project , this jar we want to use in another spring boot app? Spring Boot会为另一个项目抛出带有maven依赖关系的ClassNotFoundException - Spring Boot throws ClassNotFoundException with maven dependency for another project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM