Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanest way in Gradle to get the path to a jar file in the gradle dependency cache

I'm using Gradle to help automate Hadoop tasks. When calling Hadoop, I need to be able to pass it the path to some jars that my code depends on so that Hadoop can send that dependency on during the map/reduce phase.

I've figured out something that works, but it feels messy and I'm wondering if there's a feature I'm missing somewhere.

This is a simplified version of my gradle script that has a dependency on the solr 3.5.0 jar, and a findSolrJar task that iterates through all of the jar files in the configuration to find the right one:

apply plugin: 'groovy'  repositories {     mavenCentral() }  dependencies {     compile 'org.apache.solr:solr-solrj:3.5.0' }  task findSolrJar() {      println project.configurations.compile*.toURI().find { URI uri -> new File(uri).name == 'solr-solrj-3.5.0.jar'} } 

running this gives me output like this:

gradle findSolrJar                                                                                                                                                                                                                                                            file:/Users/tnaleid/.gradle/caches/artifacts-8/filestore/org.apache.solr/solr-solrj/3.5.0/jar/74cd28347239b64fcfc8c67c540d7a7179c926de/solr-solrj-3.5.0.jar :findSolrJar UP-TO-DATE  BUILD SUCCESSFUL  Total time: 2.248 secs 

Is there a better way to do this?

like image 297
Ted Naleid Avatar asked Mar 06 '12 04:03

Ted Naleid


People also ask

How do I cache Gradle dependencies?

Generally, you can refresh dependencies in your cache with the command line option --refresh-dependencies. You can also delete the cached files under ~/. gradle/caches . With the next build Gradle would attempt to download them again.

Where does Gradle Place jar files?

The Jar is created under the $project/build/libs/ folder.

Where is the Gradle cache?

Gradle caches artifacts in USER_HOME/. gradle folder. The compiled scripts are usually in the . gradle folder in your project folder.

How can I create an executable jar with dependencies using Gradle?

Right click the module > Open module settings > Artifacts > + > JAR > from modules with dependencies. Set the main class.


Video Answer


1 Answers

Your code can be simplified a bit, for example project.configurations.compile.find { it.name.startsWith("solr-solrj-") }.

like image 159
Peter Niederwieser Avatar answered Sep 22 '22 06:09

Peter Niederwieser