Txuritan Downloads Mods Posts Stories Story Archive

Using a local folder as a Maven repository using Gradle

Just learned that you are able to use a local folder as a Maven repository using the maven-publish Gradle plugin.

A normal build.gradle would look something like this (auto generated by Intellij Idea).

group 'com.github.txuritan.maven.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

We will first need the maven-publish plugin add that below the java plugin.

apply plugin: 'java'
apply plugin: 'maven-publish'

We will now add the task to publish the .jar to the Maven repository.

task taskDeobfJar(type: Jar, dependsOn: "reobfJar") {
    from sourceSets.main.output
    classifier = "dev"
}

task taskSourcesJar(type: Jar, dependsOn: "sourceJar") {
    from sourceSets.main.allJava
    classifier = "sources"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact taskDeobfJar
            artifact taskSourcesJar
        }
    }
    repositories {
        maven {
            url "file://C:/Users/txurt/maven-repository"
        }
    }
}

"file://C:/Users/txurt/maven-repository" is where my local Maven repository is on my machine. Note to Windows users DO NOT USE \\ AS A PATH SEPARATOR this will cause the following task to crash.

So your local Maven repository has now been made and contains a dependency, but if you use Intellij Idea like I do you will not be able to index it.

To be able to index your repository you will a external jar (for now until I make the Gradle plugin) this one to be exact.

Download and move it to your Gradle project root (where your build.gradle is).

Then add the following to your build.gradle to be able to index your local Maven repository.

task indexRepository(type: JavaExec) {
    main = "-jar"
    args = [
            "indexer-cli-6.0-SNAPSHOT.jar",
            "-r",
            "C:/Users/txurt/maven-repository/",
            "-i",
            "C:/Users/txurt/maven-repository/.index",
            "-d",
            "C:/Users/txurt/maven-repository/.index"
    ]
}

In the end your entire build.gradle file should look like this.

group 'com.github.txuritan.maven.test'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

task taskDeobfJar(type: Jar, dependsOn: "reobfJar") {
    from sourceSets.main.output
    classifier = "dev"
}

task taskSourcesJar(type: Jar, dependsOn: "sourceJar") {
    from sourceSets.main.allJava
    classifier = "sources"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact taskDeobfJar
            artifact taskSourcesJar
        }
    }
    repositories {
        maven {
            url "file://C:/Users/txurt/maven-repository"
        }
    }
}

task indexRepository(type: JavaExec) {
    main = "-jar"
    args = [
            "indexer-cli-6.0-SNAPSHOT.jar",
            "-r",
            "C:/Users/txurt/maven-repository/",
            "-i",
            "C:/Users/txurt/maven-repository/.index",
            "-d",
            "C:/Users/txurt/maven-repository/.index"
    ]
}
Copyright © 2015-2018 Ian Cronkright