Adding custom maven dependency to gradle project

Viewed 951

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'.
3 Answers

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.

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

How to add a Maven project as a Gradle dependency?

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.

Related