Publishing Gradle snapshots locally
Asked Answered
S

3

10

Here is my dummy project's build.gradle file:

apply plugin: 'groovy'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
    compile 'org.slf4j:jcl-over-slf4j:1.7.7'

    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.10.8'
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

When I run gradle clean build -Pversion=1.2.3, this packages up all my code and creates a build/libs/dummy-1.2.3.jar for me.

I would like to know the absolute bare minimum amount of Gradle code necessary so that I could publish my "dummy" JARs to my local Maven cache (mavenLocal()). Additionally, how does this work with versioning? I could always specify a concrete version for the dummy JAR, but when I'm publishing locally, it makes more sense (to me, at least) to publish SNAPSHOT versions. I could just run gradle clean build -Pversion=0.1.SNAPSHOT, but then my concern is whether or not other local projects would pick up the latest SNAPSHOT versions.

So again:

  1. What's the bare minimum code to publish dummy locally?
  2. Any way, when publishing locally, to specify a SNAPSHOT version that other projects always pick up the latest copy of?
  3. What would other local projects need to use to pick up this SNAPSHOT? Something like compile ':dummy:LATEST'?
Standoffish answered 3/1, 2015 at 7:38 Comment(1)
Has the question you asked been answered? If so accept the answer please.Senary
S
9

Here I've prepared sample project for you, this is the bare minimum when it comes to build.gradle configuration.

  1. You need to add apply plugin: 'maven' and set group = 'somegroup'. maven plugin gives install task and group is required to install artifact in maven repository.

    Then run gradle clean install. If no version passed, it will evaluate to unspecified, if no artifactId configured it will evaluate to project.name. Here You can find how to configure other maven properties.

    Installing snapshot for local development is definitely a good idea.

  2. If You'd like other project to always pick the latest version of SNAPSHOT You need to add the following piece of code to build.gradle scripts. It guarantees resolving to the latest version.

    configurations.all {
       resolutionStrategy {
         cacheChangingModulesFor 0, 'seconds'
       }
    }
    
  3. First of all You need to add snapshot repo to repositories block (just a sample) - with local maven repo, this step is not needed:

    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
    

    Dependency should be specified as:

    group:artifact:0.+
    

Feel free to ask in case of any questions.

Senary answered 3/1, 2015 at 14:12 Comment(0)
L
2

1) You just need to do a

gradle install

2) Specify whatever version you like - a SNAPSHOT version makes sense for active development. Once you think your library is less likely to change you should definitely go for a non-snapshot version.

3) You have to specify a dependency to the version you have in your local repo, just as for 3rd party libraries.

Levi answered 3/1, 2015 at 13:25 Comment(1)
Which plugin is needed to perform that? I get this when I try: ``` Task 'install' not found in root project 'kotti'. ```Euphony
H
0

I needed to publish a snapshot update to a library locally to pick up in a project that depended on it. Both are using Gradle. Here's what I did to make that work:

In the library:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.1'
    id 'io.spring.dependency-management' version '1.1.5'
    id 'jacoco'
    id 'maven-publish'
...

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId "${archivesBaseName}"
            artifact jar
            artifact sourceJar
            pom.withXml {
                asNode().appendNode('description', archivesBaseName)
            }
        }
    }
}

Then in my project needing it:

repositories {
    mavenLocal()
    maven {
        url "$artifactory_url"
        allowInsecureProtocol = true
        credentials {
            username = 'someuser'
            password = 'somepass'
        }
    }
}

The key there is mavenLocal() first. Then in the library project run the command:

    gradle clean build
    gradle publishToMavenLocal

as noted: Publish Java artifact to Maven Local with Gradle

Once that is done, I refreshed my configuration (to add the new local Maven repo) and dependencies and it worked. My IDE is IntelliJ.

Howell answered 9/7 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.