一、问题发生背景
android studio是Arctic Fox版本,gradle
插件版本是7.2,JDK11。创建groovy model
实现 javassist 时,groovy model的 build.gradle
如下:
apply plugin: 'groovy'
apply plugin: 'maven'
repositories {
mavenCentral()
}
uploadArchives{
// 打包成一个jar 引用jar 生成这个三个信息
repositories.mavenDeployer {
pom.groupId = 'com.javassist'
pom.artifactId = 'modify'
pom.version = '1.0.0'
repository(url: uri('../repo'))
}
}
dependencies {
//gradle sdk
compile gradleApi()
//groovy sdk
compile localGroovy()
compile 'org.javassist:javassist:3.20.0-GA'
}
结果报错:
Caused by: org.gradle.api.plugins.UnknownPluginException: Plugin with id 'maven' not found.
二、分析解决
Gradle 7
版本将maven
插件移除了,用maven publish
插件替代。
解决如下:
plugins {
id 'groovy'
id 'maven-publish'
}
publishing {
publications {
publish2Local(MavenPublication) {
groupId = 'com.javassist'
artifactId = 'modify'
version = '1.0.0'
from components.java
}
}
repositories {
maven {
url = "$buildDir/repo"
}
}
}
dependencies {
//gradle sdk
implementation gradleApi()
//groovy sdk
implementation localGroovy()
implementation 'org.javassist:javassist:3.20.0-GA'
}
如果报 ‘Gradle Libs’ was added by unknown code
:
Build was configured to prefer settings repositories over project repositories
but repository ‘Gradle Libs’ was added by unknown code
解决方法
问题的原因就是Gradle希望开发者将所有仓库配置都在这里设置,而不是根据每个项目自己去导入
settings.gradle
中,修改或去除repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
dependencyResolutionManagement {
// repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
repositories {
google()
mavenCentral()
}
}