Gradle离线模式下指定本地插件失败求助(Spring Boot项目)
问题背景
无网络环境下使用Gradle构建Spring Boot项目,项目依赖已放入根目录libs目录,这部分工作正常。需使用org.springframework.boot插件打包,已将spring-boot-gradle-plugin-2.2.0.RELEASE.jar与dependency-management-plugin-1.0.11.RELEASE.jar放入项目plugins目录,但多种配置方法均无效。
项目目录结构
projectRoot ├── build.gradle ├── gradle │ └── wrapper ├── gradlew ├── gradlew.bat ├── libs │ └── xxx.jar ├── plugins │ ├── dependency-management-plugin-1.0.11.RELEASE.jar │ └── spring-boot-gradle-plugin-2.2.0.RELEASE.jar ├── resource ├── settings.gradle └── src ├── main └── test
当前配置文件
settings.gradle
pluginManagement { repositories { mavenLocal() flatDir { dirs 'plugins' } } } rootProject.name = 'example'
build.gradle
plugins { id 'org.springframework.boot' version '2.2.0.RELEASE' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '1.0.0-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenLocal() } dependencies { implementation files('libs/xxx.jar','libs/others_of_libs.jar') }
离线构建错误信息
执行命令 ./gradlew build --offline 时出现以下错误:
Plugin [id: 'org.springframework.boot', version: '2.2.0.RELEASE'] was not found in any of the following sources: - Gradle Core Plugins (plugin is not in 'org.gradle' namespace) - Plugin Repositories (could not resolve plugin artifact 'org.springframework.boot:org.springframework.boot.gradle.plugin:2.2.0.RELEASE') Searched in the following repositories: maven(file:/User/myName/example/plugins/)
已尝试的无效配置
曾尝试使用buildscript配置,仍报相同错误:
buildscript { repositories { mavenLocal() maven { url('plugins') } } dependencies { classpath fileTree(include: ['*.jar'], dir: 'plugins') classpath files('plugins/dependency-management-plugin-1.0.11.RELEASE.jar') classpath files('plugins/spring-boot-gradle-plugin-2.2.0.RELEASE.jar') classpath "io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:1.0.11.RELEASE" classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE" } }
解决方案
错误原因
Gradle的plugins DSL通过插件ID查找对应的marker artifact(如org.springframework.boot:org.springframework.boot.gradle.plugin:2.2.0.RELEASE),而非直接查找插件实现jar。你仅放入了插件的核心实现jar,缺少关联ID的marker artifact,导致Gradle无法解析插件ID。
方案1:安装插件到本地Maven仓库(推荐)
通过Maven命令将插件jar安装到本地仓库,让Gradle能通过插件ID正常解析:
- 安装Spring Boot插件:
mvn install:install-file -Dfile=plugins/spring-boot-gradle-plugin-2.2.0.RELEASE.jar -DgroupId=org.springframework.boot -DartifactId=spring-boot-gradle-plugin -Dversion=2.2.0.RELEASE -Dpackaging=jar
- 安装依赖管理插件:
mvn install:install-file -Dfile=plugins/dependency-management-plugin-1.0.11.RELEASE.jar -DgroupId=io.spring.dependency-management -DartifactId=dependency-management-plugin -Dversion=1.0.11.RELEASE -Dpackaging=jar
- 同时需要安装对应的marker artifact(可从有网络环境下载后安装):
- Spring Boot插件对应marker jar:
org.springframework.boot:org.springframework.boot.gradle.plugin:2.2.0.RELEASE.jar - 依赖管理插件对应marker jar:
io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:1.0.11.RELEASE.jar
- Spring Boot插件对应marker jar:
完成后,原settings.gradle和build.gradle配置即可正常工作,执行离线构建命令即可成功。
方案2:跳过plugins DSL,直接用buildscript加载插件
无需依赖marker artifact,直接通过buildscript指定插件classpath并应用插件,修改build.gradle如下:
buildscript { dependencies { // 直接加载本地插件jar classpath files('plugins/spring-boot-gradle-plugin-2.2.0.RELEASE.jar') classpath files('plugins/dependency-management-plugin-1.0.11.RELEASE.jar') } } // 直接应用插件的类或ID apply plugin: org.springframework.boot.gradle.plugin.SpringBootPlugin apply plugin: io.spring.gradle.dependencymanagement.DependencyManagementPlugin apply plugin: 'java' group = 'com.example' version = '1.0.0-SNAPSHOT' sourceCompatibility = '1.8' repositories { // 本地依赖仓库 flatDir { dirs 'libs' } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) }
此配置无需修改settings.gradle,直接执行./gradlew build --offline即可完成离线构建。
内容的提问来源于stack exchange,提问作者leowangzz

