set java 21 environment variable:

 export JAVA_HOME=$(/usr/libexec/java_home -v 21)

located in the same place as “gradle”

You execute publish in Gradle and also force Java version 21 via command

./gradlew publish -Dorg.gradle.java.home=$(/usr/libexec/java_home -v 21)

the artifact in nexus repository:

the build.gradle in java:

import java.util.Properties

plugins {
    id 'java-library'
    id 'maven-publish'
    id 'io.spring.dependency-management' version '1.1.7'
}

def env = new Properties()

def getAppName() {
    def props = new Properties()
    def propFile = file('src/main/resources/application.properties')
    if (propFile.exists()) {
        propFile.withInputStream { props.load(it) }
        return props.getProperty('spring.application.name')
    }
    return 'default-artifact-name'
}

def artifactName = getAppName()
group = 'com.leibnix'
version = '0.0.4'

// to consume:
repositories {
    mavenCentral()
    maven {
        name = "NexusRemote"
        url = "http://server.axxelin.com:8081/repository/maven-releases/"
        allowInsecureProtocol = true // Required if you are not using HTTPS locally
        credentials {
            username = env.getProperty("NEXUS_USERNAME")
            password = env.getProperty("NEXUS_PASSWORD")
        }
    }
}

java {
    toolchain { languageVersion = JavaLanguageVersion.of(21) }
    withSourcesJar()
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:3.4.1"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'com.fasterxml.jackson.core:jackson-databind'
}

tasks.named('jar') {
    archiveClassifier = ''
}

def envFile = rootProject.file(".env")
if (envFile.exists()) {
    envFile.withInputStream { env.load(it) }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = artifactName
            from components.java
            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionOf('runtimeClasspath')
                }
            }
        }
    }
    repositories {
        maven {
            name = "NexusRemote"
            url = "http://server.axxelin.com:8081/repository/maven-releases/"
            allowInsecureProtocol = true // Required if you are not using HTTPS locally
            credentials {
                username = env.getProperty("NEXUS_USERNAME")
                password = env.getProperty("NEXUS_PASSWORD")
            }
        }
    }
}

in maven-releases (in browse) you see your artifact: