5

I'm trying to deploy a jar + pom file to artifactory using gradle + artifactory plugin + maven-publish plugin.

I've tried multiple solutions from other sources like this and I think the spring-boot plugin is breaking stuff (because it edits the jar file)

The following script successfully uploads a .pom file, but not the .jar file generated by spring-boot. How can I get it to also upload that?

this is my build.gradle:

buildscript {
    ext {
        springBootVersion = '1.4.0.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.0"
    }
}
apply plugin: 'java'
apply plugin: 'spring-boot'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
apply from: "gradle/artifactory.gradle"

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

jar {
    baseName = 'BatchParser'

}

version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {

    compile('org.projectlombok:lombok:1.16.10')
    ...
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

and artifactory.gradle

artifactory {
    contextUrl = 'url'
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = 'user'
            password = 'password'
        }
        defaults {
            publications("mavenJava")
        }
    }
}

output of:

gradlew clean build artifactoryPublish


[buildinfo] Not using buildInfo properties file for this build.                  
:clean                      
:compileJava                                                                                                                
:processResources
:classes
:findMainClass
:jar
:bootRepackage                                                                                                        
:assemble
:compileTestJava                                                                  
:processTestResources UP-TO-DATE
:testClasses
:test                                                         
:check
:build
:generatePomFileForMavenJavaPublication                 
:artifactoryPublish
Deploying artifact: http://url/libs-release-local/BatchParser/1.0/BatchParser-1.0.pom
Deploying build descriptor to: http://url/api/build
Build successfully deployed. Browse it in Artifactory under http://url/webapp/builds/BatchParser/1471949957594
p.streef
  • 3,652
  • 3
  • 26
  • 50

1 Answers1

8

There's a very subtle mistake in your publishing block. from is missing which is causing Gradle to silently not include the jar file in the publication. You need to update your publishing block so that it looks like this:

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

If I were you, I'd open a Gradle usability bug for this. Silently doing nothing isn't very user friendly.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    Oh wouw, that is indeed rather unfriendly. Thanks for noticing my mistake! – p.streef Aug 24 '16 at 10:14
  • Interesting. I see publication of the jar prior to springboot adding its content. The recommend using the uploadArchives + mavenDeployer. Have you got those to work together? – Peter Kahn Oct 10 '17 at 20:18
  • We broke build and artifactoryPublish into two separate gradle invocations in our jenkins file. this led to remaking the jar without springboot. One call with build...artifactoryPublish, and no problems – Peter Kahn Oct 10 '17 at 20:37
  • 1
    > "Silently doing nothing isn't very user friendly." Silently doing nothing is, among other laxities, one of the corner stones of the whole groovy/gradle culture. Most gradle users consider this a feature (or collateral damage at best), not a bug. Just saying... – n1ghtm4n4g3r Sep 16 '19 at 15:07
  • Can we get rid of Groovy, then? It's not very user friendly. – Blake M. Mar 31 '22 at 17:11
  • @BlakeM. You could try writing your Gradle scripts in Kotlin. Note that I'm not sure if the same problem could occur with Kotlin but it may be worth a try. – Andy Wilkinson Mar 31 '22 at 18:30
  • Gradle does indeed publish the pre-boot jar and not the boot jar. The easiest solution I found was to write a Gradle plugin in Java that adds the bootJar task to the publication. https://pastebin.com/KVNpYMiM – Blake M. Apr 01 '22 at 20:50
  • 1
    @BlakeM. There’s no need to write a plugin: https://docs.spring.io/spring-boot/docs/2.6.x/gradle-plugin/reference/htmlsingle/#publishing-your-application.maven-publish – Andy Wilkinson Apr 02 '22 at 05:54
  • The contents of your link should be the accepted answer in addition to what's there right now. The original answer seems incomplete considering OP was trying to publish the Spring Boot jar, not the "plain old Java" jar. – Blake M. Apr 03 '22 at 12:30
  • The question’s about Spring Boot 1.x, a lot has changed since then. – Andy Wilkinson Apr 03 '22 at 16:14