How to build a Groovy JAR w / Gradle and publish it to an internal repo

I have a Groovy project and am trying to build it using Gradle. First, I want a package task that creates a JAR, compiling it depending on its dependencies. Then I need to create a Maven POM for this JAR and publish the JAR / POM for Artifactory's own refactoring. 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 that I correctly configured version control with my jar task (for example, how could I create it myapp-1.2.1 vs. myapp-1.2.2 ?) I also do not think that I have publications configured correctly: what should go in pom.withXml ?

+6
source share
2 answers

You can more than use the artifactory plugin for this. The documentation can be found in our user manual and below you can find a complete working example of gradle assembly.

Run gradle build artifactoryPublish to create 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' } } } 
+12
source

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

In any case, the task declaration for package must be removed, since the jar task already serves this purpose. The configuration of the jar task ( jar { from ... } ) should be at the most remote level (not nested in another task), but from configurations.compile it is unlikely that you want, since this will include Jars from dependency compilation in the Jar (which regular java cool loaders can't handle it) rather than merging them with jar. (Are you even sure you need a fat jar?)

Similarly, the publish task declaration must be removed and replaced with publishing { publications { ... } } .

In addition, the buildscript block buildscript probably be removed, and repositories { ... } and dependencies { ... } moved to the outermost level. ( buildscript { dependencies { ... } } declares the dependencies of the script collector itself (for example, Gradle), and not the dependency on the compiled / executed code.)

I suggest checking out many standalone build examples in the samples directory of the full Gradle distribution ( gradle-all ).

+2
source

Source: https://habr.com/ru/post/973697/


All Articles