Phrogz
Phrogz

Reputation: 303205

How to import 3rd party library using Maven into Java project using Gradle?

I want to use the sxcml-java library in my son's school's robotics code (currently a private repo).

The library uses Maven. I was able to successfully include the library in a test project using Maven.

However, I've just discovered that the existing robotics project code uses Gradle. I don't know either Maven or Gradle, and I haven't programmed in Java in almost 30 years.

How can I most easily use scxml-java - which itself has external 3rd party dependencies — in the robotics project?

This question is similar to this one, but the solution there was easy because both projects were using Gradle.

Upvotes: 0

Views: 341

Answers (2)

life888888
life888888

Reputation: 2999

If You install your jar or third party jar into maven local repo like ~/.m2

you can add mavenLocal()

repositories {
    mavenCentral()    
    // * Require by Use JAR install to Maven Local Repo your .m2
    mavenLocal()
}

then add implementation to dependencies

dependencies {
  implementation 'com.google.guava:guava:31.1-jre'
  implementation 'yourGroupId:yourArtifactId:yourVersion'
}

Please mapping yourGroupId , yourArtifactId, yourVersion from your pom.xml

If You only download third party jar into foler like /home/yourName/your-libs

you can add configurations

configurations {
    sxcml-java-lib
}

then add dependencies

dependencies {
  implementation 'com.google.guava:guava:31.1-jre'
  
  //sxcml-java-lib fileTree(dir: "${System.getProperty("user.home")}/libs", include: "*.jar")
  sxcml-java-lib fileTree(dir: "/home/yourName/your-libs", include: "*.jar")
}

Upvotes: 1

rph
rph

Reputation: 2639

Provided the package is published in an artifactory, which is the case (See here), you can just include it as any other Gradle dependency (using groupId, artifactId and version), regardless of what build system was used to build it in the first place.

dependencies {
    implementation 'com.nosolojava.fsm:scxml-java-implementation:1.0.1'
}

If you use IntelliJ IDEA, pasting the Maven dependency block into the build.gradle file will automatically convert it into the Gradle dependency format like the one above.

Please note however this does not apply to plugins, only to regular dependencies.

Upvotes: 1

Related Questions