添加自定义的Maven依赖到Gradle项目。

huangapple go评论80阅读模式
英文:

Adding custom maven dependency to gradle project

问题

我已经使用Maven项目创建了依赖的JAR文件,但现在我需要将这个Maven依赖项添加到我的Gradle项目中。

依赖项位于我的.m2目录中。

我从IntelliJ收到以下错误。

执行任务“:compileJava”失败。

无法解析配置“:compileClasspath”的所有文件。
无法将URL“com.example.auth.security:common:0.0.1-SNAPSHOT.jar”转换为文件。

请查找我的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')  // 在此行上出现错误。
    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

发生了一个问题,评估根项目“auth-center”时。

提供的字符串模块标记“0.0.1-SNAPSHOT”无效。示例标记:“org.gradle:gradle-core:2.2”,“org.mockito:mockito-core:1.9.5:javadoc”。

英文:

I have created dependency jar using maven project but now i have to add this maven dependency into my gradle project.

Depencency available in my .m2 directory

Am getting the below error from 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.

Please find my build.gradle file

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')

}

Update 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'.

答案1

得分: 4

你需要像这样添加一个本地的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仓库更可持续,因为通过这种方式解析构件,构建过程可以透明地确定构件是在本地还是远程解析的。

英文:

You will need to add a local maven repository like this

repositories {
  maven { url new File(pathToYourM2Directory).toURI().toURL() }
}

Additionally the declaration of the dependency is not correct. It should be

dependencies {
  implementation group: 'com.example.auth.security', name: 'common', version '0.0.1-SNAPSHOT'
}

You can as well fix the files dependency. However using a local maven repo is more sustainable as by resolving artifacts this way it is transparent for the build process if an artifact is resolved locally or remote.

答案2

得分: 1

无法评论,因为我声望不足。我认为你不应该尝试将Maven依赖项添加到Gradle项目中。相反,将Maven依赖项托管在其他地方,并配置Gradle从那里获取依赖项。供参考,你可以查看这个答案:

https://stackoverflow.com/questions/36057574/how-to-add-a-maven-project-as-a-gradle-dependency

英文:

Can't comment since I don't have sufficient reputation. I believe you shouldn't be trying to add the maven dependency to the gradle project. Instead, host the maven dependency elsewhere and configure gradle to pull dependencies from there. For reference, you can take a look at this answer

https://stackoverflow.com/questions/36057574/how-to-add-a-maven-project-as-a-gradle-dependency

答案3

得分: 0

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

您需要做的是首先在您的项目下创建一个文件夹,可以放在任何地方,例如在我的情况下是/src/lib,然后将JAR文件放入该文件夹中。接下来,在您的Gradle文件中写入以下代码。

dependencies {
	//库自动实现
    //替换为您的文件夹路径
	implementation(fileTree("src/lib"))
}

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

英文:

This is another way to import your custom java library(.jar).

What you need to do is that first, make a folder wherever you want under your project, in my case /src/lib, and put JAR file into that folder. Then, write down the below code into your Gradle file.

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

Then Gradle will implement your JAR files from that folder, and you are ready to go.

huangapple
  • 本文由 发表于 2020年8月5日 01:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63252543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定