【问题标题】:Adding custom maven dependency to gradle project [duplicate]将自定义 Maven 依赖项添加到 gradle 项目 [重复]
【发布时间】:2020-11-24 21:12:57
【问题描述】:

我已经使用 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'.

【问题讨论】:

标签: java maven gradle


【解决方案1】:

您需要像这样添加一个本地 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 存储库更具可持续性,因为通过这种方式解析工件,如果工件在本地或远程解析,构建过程是透明的。

【讨论】:

  • 这仍然报错
  • @Karthikeyan 请用新错误更新问题
  • @Karthikeyan 和你的确切依赖声明请
【解决方案2】:

由于我没有足够的声誉,无法发表评论。我相信您不应该尝试将 maven 依赖项添加到 gradle 项目中。相反,在其他地方托管 maven 依赖项并配置 gradle 以从那里提取依赖项。作为参考,你可以看看这个答案

How to add a Maven project as a Gradle dependency?

【讨论】:

    【解决方案3】:

    这是导入自定义 java 库 (.jar) 的另一种方式。

    您需要做的是,首先,在项目下的任意位置创建一个文件夹,在我的例子中为/src/lib,然后将 JAR 文件放入该文件夹中。然后,将以下代码写入您的 Gradle 文件。

    dependencies {
        //Library Auto Implement
        //Replace with your folder URI
        implementation(fileTree("./src/lib"))
    }
    

    然后,Gradle 将从该文件夹实现您的 JAR 文件,然后您就可以开始了。

    【讨论】:

    • 仅供参考,我正在使用 Kotlin DSL。
    猜你喜欢
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2019-12-13
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多