我已经使用maven项目创建了依赖项jar,但是现在我必须将此maven依赖项添加到我的gradle项目中。
在.m2
目录中可用的依赖关系
我从intellij得到以下错误。
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
> Cannot convert URL 'com.example.auth.security:common:0.0.1-SNAPSHOT.jar' to a file.
请找到我的build.gradle
文件
plugins {
id 'org.springframework.boot' version '2.1.16.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.example.auth'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation files('com.example.auth.security:common:0.0.1-SNAPSHOT.jar') --> getting error on this line.
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
compileOnly 'org.projectlombok:lombok'
implementation('io.jsonwebtoken:jjwt:0.9.1')
}
更新1
A problem occurred evaluating root project 'auth-center'.
> Supplied String module notation '0.0.1-SNAPSHOT' is invalid. Example notations: 'org.gradle:gradle-core:2.2', 'org.mockito:mockito-core:1.9.5:javadoc'.
答案 0 :(得分:3)
您将需要添加一个本地Maven存储库
repositories {
maven { url new File(pathToYourM2Directory).toURI().toURL() }
}
此外,依赖项的声明不正确。应该是
dependencies {
implementation group: 'com.example.auth.security', name: 'common', version '0.0.1-SNAPSHOT'
}
您还可以修复files
依赖性。但是,使用本地Maven回购协议更具可持续性,因为通过这种方式解析工件,如果在本地或远程解析工件,则对构建过程是透明的。
答案 1 :(得分:1)
因为我没有足够的声誉,所以无法发表评论。我相信您不应该尝试将Maven依赖项添加到gradle项目中。取而代之的是,将Maven依赖项托管在其他位置,并配置gradle以从那里获取依赖项。作为参考,您可以看一下这个答案
答案 2 :(得分:0)
这是导入自定义Java库(.jar)的另一种方法。
首先需要做的是,在项目下的任意位置创建一个文件夹(在我的情况下为/src/lib
),然后将JAR文件放入该文件夹。然后,将以下代码写下到您的Gradle文件中。
dependencies {
//Library Auto Implement
//Replace with your folder URI
implementation(fileTree("./src/lib"))
}
然后Gradle将从该文件夹中实现您的JAR文件,您就可以开始使用了。