mavengradlemaven-publish

jar file is not getting generated in maven local repository after running publishToMavenLocal


I am trying to get a jar generated so I can use it as a dependency for a different project. In the build.gradle, I have defined the maven-publish id and the publishing tasks, but only the following files are generated - but I need the custom-codegen-0.0.1-SNAPSHOT.jar

custom-codegen-0.0.1-SNAPSHOT-plain.jar
custom-codegen-0.0.1-SNAPSHOT.module
custom-codegen-0.0.1-SNAPSHOT.pom
maven-metadata-local.xml

build.gradle

plugins {
    id 'org.springframework.boot' version '2.5.6'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'maven-publish'
}

group = 'com.tmo5'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}


publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
        }
    }
    repositories {
        maven {
            def releasesRepoUrl = "$buildDir/repos/releases"
            def snapshotsRepoUrl = "$buildDir/repos/snapshots"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

Solution

  • Unless you are creating a Spring Boot application, the Spring Boot Gradle plugin should not be applied. It is not meant for creating Spring Boot libraries or Spring libraries in general. It is for creating Spring Boot applications. The Spring Boot Gradle plugin reacts to various plugins applied to the project as documented in the documentation.

    Second if you want your published artifact to be consumed by another, then you should use the java-library instead.

    With the said, your Gradle build should look something like:

    plugins {
        id 'java-library'
        id 'maven-publish'
    }
    
    group = 'com.tmo5'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        api platform('org.springframework.boot:spring-boot-dependencies:2.5.6')
        api 'org.springframework.boot:spring-boot-starter-web'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
    
    test {
        useJUnitPlatform()
    }
    
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                versionMapping {
                    usage('java-api') {
                        fromResolutionOf('runtimeClasspath')
                    }
                    usage('java-runtime') {
                        fromResolutionResult()
                    }
                }
            }
        }
        repositories {
            maven {
                def releasesRepoUrl = "$buildDir/repos/releases"
                def snapshotsRepoUrl = "$buildDir/repos/snapshots"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
            }
        }
    }