Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: is it possible to publish to Gradle's own local cache?

Tags:

gradle

I know that I can use the maven plugin coupled with mavenLocal() to install an artifact and use it locally.

However investigating this a bit further, I notice that this means the artifacts are installed to Mavens's standard ~/.m2, but at the same time Gradle's own cache lives under ~/.gradle/caches in a different format.

This seems wasteful to me, a) working with two local caches, and b) having to add mavenLocal() to all projects. I was wondering if there is a way to publish an artifact to Gradle's ~/.gradle/caches ?

like image 393
Amir Abiri Avatar asked Feb 17 '16 15:02

Amir Abiri


People also ask

Does Gradle allow share build cache?

The build cache allows you to share and reuse unchanged build and test outputs across the team. This speeds up local and CI builds since cycles are not wasted re-building components that are unaffected by new code changes. NOTE: Gradle Enterprise Build Cache supports both Gradle and Maven build tool environments.

Where does Gradle store build cache?

The local build cache uses a system directory to store tasks outputs. The default location is the Gradle user home directory that points to $USER_HOME/. gradle/caches. Every time we run the build in our system, artifacts will be stored here.

How do I change the Gradle cache folder?

You can't change the location of the cache specifically, but you can change the Gradle user home directory (the . gradle directory) which the cache is located in by using the -g command line argument.


2 Answers

Note that the local Maven repository is not (really) a cache, and that the Gradle cache is not a repository. Gradle uses its cache only to cache remote artifacts, it should not copy artifacts retrieved from local Maven repositories there. In turn, you cannot publish artifacts to the Gradle cache.

So the approach to publish to and use mavenLocal() should not be as wasteful as you think. Also, you do not have to add mavenLocal() to all projects of a multi-project separately. You can simply use something like allprojects { repositories { mavenLocal() } } in your root project. Or if you want mavenLocal() in all your independent Gradle projects you could even try adding it to ~/.gradle/init.gradle.

like image 110
sschuberth Avatar answered Sep 28 '22 01:09

sschuberth


Here is an example with code. As it's not possible to publish into Gradle. The workaround is to publish into the maven and use it in Gradle.

Step 1 publish the code to local maven repository /users/jay/.m2/repository/

Step2 - Use the local maven repo code in another project.

Step #1 (Publish to local maven repo)

apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

Step #2 (Use the local maven repo in your gradle project)

repositories {

    // ..... Other repositories

    mavenLocal()
}


dependencies {
    compile 'com.jai:myapp:1.0.0'
}
like image 42
Jay Avatar answered Sep 28 '22 00:09

Jay