【发布时间】:2021-07-23 21:08:19
【问题描述】:
**Gradle 新手
我需要使用现有的 spring boot 项目(它安装在本地 maven 存储库中)作为另一个项目(Spring boot gradle 项目)的依赖项。
使用 mavenLocal 我能够成功运行和构建 gradle 项目,但是当我尝试使用其他项目中存在的任何类时,我无法导入它。
我如何使用这个 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
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 扫描自动装配演示库项目中存在的类。
注意:如果我创建一个胖 spring boot 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);
}
}
***********已更新**********
我在组件扫描中的坏包是错误的。
它与 @ComponentScan("tech.thegamedefault.demolib") 一起工作正常
【问题讨论】:
-
添加依赖后是否刷新/重新加载gradle?能否验证一下jar文件是否发布到.m2文件夹中?
-
是的,它存在于 .m2 文件夹中......我还添加了一张图片,显示它正在被导入到 gradle 项目中
-
嗯..有些奇怪。你可以尝试为你的 SuperUtility 类使用完整的包名吗?
-
找不到那个包...使用eclipse添加maven项目的java构建路径使它可以工作,但只能在eclipse中工作。
-
好的。 “项目和外部依赖项”下的 demo-lib jar 没有包“tech.thegamedefault”。你确定你的 demo-lib 模块使用的是“org.springframework..”包吗?
标签: java spring-boot maven gradle