Question

I have a Gradle project that depends on an external jar file. Currently I'm defining the dependency like this:

dependencies {
  compile files('/path/to/my/jar/library.jar')
}

However I want to include it as a project dependency instead, like this:

dependencies {
  compile project(':whatGoesHere?')
}

I assume I need to define a new Gradle project that contains the jar file but I don't know how to do this. I'm wondering about things like:

  1. Do I just need to create a new build.gradle or are there more steps?
  2. What would go in the build.gradle file?

Assume the new project contains nothing but the jar file (since it does). Also assume I know almost nothing about Gradle (because I don't!).

P.S. If it matters, this is an Android Gradle project.

Was it helpful?

Solution

As a roundup for our discussion, I'll bring simple example of "build.gradle" file, using maven local and maven central repositories:

apply plugin: 'maven'
apply plugin: 'java'

repositories {
  mavenLocal()
  mavenCentral()
}

dependencies {
  compile 'commons-io:commons-io:2.4'
  testCompile 'junit:junit:4.11'
}

Explanation:

  1. "apply plugin: 'maven'" enables maven plugin, which is needed for dependency download.

  2. "apply plugin: 'java'" enables java compilation tasks for your project.

  3. "repositories" declares one or more repositories (maven or ivy), from where artifacts (jar libraries) will be downloaded.

  4. "mavenLocal" refers to so-called local maven repository, which is located in "~/.m2/repository" folder on your computer. local maven repository effectively caches external repositories, but it also allows installation of local-only artifacts.

  5. "mavenCentral" refers to maven central.

  6. "dependencies" lists your project dependencies, either other projects or artifacts (jars).

  7. "compile" is a configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during compilation phase".

  8. "testCompile" is another configuration supported by "java" and "groovy" plugins, it tells gradle: "add these libraries to the classpath of the application during test phase".

  9. 'commons-io:commons-io:2.4' is "coordinates" of the artifact within maven repository, in form group:name:version.

You can search for well-known java libraries at address: http://mvnrepository.com/ and then include their coordinates in "build.gradle". You don't need to download anything - gradle does it for you automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top