[英]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.