Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Groovy JAR w/ Gradle and publish it to in-house repo

I have a Groovy project and am trying to build it with Gradle. First I want a package task that creates a JAR by compiling it against its dependencies. Then I need to generate a Maven POM for that JAR and publish the JAR/POM to an in-house Artifactory repo. The build.gradle:

apply plugin: "groovy"
apply plugin: "maven-publish"

repositories {
    maven {
        name "artifactory01"
        url "http://myartifactory/artifactory/libs-release"
    }
}
dependencies {
    compile "long list starts here"
}

// Should compile up myapp-<version>.jar
jar {
}

// Should publish myapp-<version>.jar and its (generated) POM to our in-house Maven/Artifactory repo.
publishing {
    publications {
        myPublication(MavenPublication) {
            from components.java
            artifact sourceJar {
                classifier "source"
            }

            pom.withXml {
                // ???
            }
        }
    }
}

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

However I do not believe I have set up versioning correctly with my jar task (for instance, how could I get it creating myapp-1.2.1 vs. myapp-1.2.2? I also don't think I have my publications configuration set up correctly: what should go in pom.withXml?

like image 273
Venkat Avatar asked Aug 12 '14 07:08

Venkat


People also ask

How do I create a Gradle project in Groovy?

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 2: Groovy as implementation language. Next you can choose the DSL for writing buildscripts - 1 : Groovy or 2: Kotlin .

Is build Gradle in Groovy?

The Gradle build language This build language is available in Groovy and Kotlin.


2 Answers

You're more than welcome to use artifactory plugin for that. The documentation can be found in our user guide and below you can find a full working example of gradle build.

Run gradle build artifactoryPublish to build and publish the project.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1')
    }
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

group = 'com.jfrog.example'
version = '1.2-SNAPSHOT'
status = 'SNAPSHOT'

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.5'
    testCompile 'junit:junit:4.11'
}

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

publishing {
    publications {
        main(MavenPublication) {
            from components.java
            artifact sourcesJar
    }
}

artifactory {
    contextUrl = 'http://myartifactory/artifactory'
    resolve {
        repository {
            repoKey = 'libs-release'
        }
    }
    publish {
        repository {
            repoKey = 'libs-snapshot-local'
            username = 'whatever'
            password = 'whatever123'
        }
        defaults {
            publications 'main'
        }
    }
}
like image 110
JBaruch Avatar answered Sep 27 '22 20:09

JBaruch


package is a keyword in Java/Groovy, and you'd have to use a different syntax to declare a task with that name.

Anyway, the task declaration for package should be removed, as the jar task already serves that purpose. The jar task configuration (jar { from ... }) should be at the outermost level (not nested inside another task), but from configurations.compile is unlikely what you want, as that will include Jars of compile dependencies into the Jar (which regular Java class loaders can't deal with), rather than merging them into the Jar. (Are you even sure you need a fat Jar?)

Likewise, the publish task declaration should be removed, and replaced with publishing { publications { ... } }.

Also, the buildscript block should probably be removed, and repositories { ... } and dependencies { ... } moved to the outermost level. ( buildscript { dependencies { ... } } declares dependencies of the build script itself (e.g. Gradle plugins), not the dependencies of the code to be compiled/run.)

I suggest to check out the many self-contained example builds in the samples directory of the full Gradle distribution (gradle-all).

like image 40
Peter Niederwieser Avatar answered Sep 27 '22 21:09

Peter Niederwieser