DEV Community

Query Filter
Query Filter

Posted on

exec-10

plugins {
    id 'java'
    id 'distribution'
    id 'maven-publish'
}

group = 'com.example'
version = '1.0.0'

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

repositories {
    mavenCentral()
}

dependencies {
    // Add runtime dependencies here
    // implementation 'org.springframework.boot:spring-boot-starter:3.2.0'
}

// -----------------------------------------------------------------------------
// 1. Distribution Configuration
// -----------------------------------------------------------------------------
distributions {
    main {
        distributionBaseName = project.name
        contents {
            // Package compiled JAR into lib/
            into('lib') {
                from tasks.named('jar')
                from configurations.runtimeClasspath
            }
            // Include resources or configuration files into config/
            into('config') {
                from 'src/main/resources'
            }
        }
    }
}

// Ensure the TAR task produces a compressed .tar.gz archive
tasks.named('distTar', Tar) {
    compression = Compression.GZIP
    archiveExtension = 'tar.gz'
}

// Disable ZIP archive creation completely across build lifecycle
tasks.named('distZip') {
    enabled = false
}

// -----------------------------------------------------------------------------
// 2. Manifest Generation Task
// -----------------------------------------------------------------------------
def manifestFile = layout.buildDirectory.file("reports/manifest.json")

tasks.register('generateManifest') {
    outputs.file(manifestFile)
    doLast {
        def manifestContent = """{
  "appName": "${project.name}",
  "version": "${project.version}",
  "buildTime": "${new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'")}",
  "archive": "${project.name}-${project.version}.tar.gz"
}"""
        manifestFile.get().asFile.parentFile.mkdirs()
        manifestFile.get().asFile.text = manifestContent
    }
}

// Guarantee manifest is generated during assembly
tasks.named('assemble') {
    dependsOn tasks.named('generateManifest')
}

// -----------------------------------------------------------------------------
// 3. Artifactory Publishing (Maven Publish Plugin)
// -----------------------------------------------------------------------------
publishing {
    publications {
        mavenTar(MavenPublication) {
            groupId = project.group
            artifactId = project.name
            version = project.version

            // Publish the .tar.gz distribution file
            artifact(tasks.named('distTar')) {
                extension 'tar.gz'
            }

            // Publish the manifest as an auxiliary artifact
            artifact(manifestFile) {
                classifier 'manifest'
                extension 'json'
                builtBy tasks.named('generateManifest')
            }
        }
    }

    repositories {
        maven {
            name = "artifactory"
            // Replace with your Artifactory repository URL
            url = uri("${artifactory_contextUrl}/libs-release-local")
            credentials {
                username = project.findProperty('artifactory_user') ?: System.getenv('ARTIFACTORY_USER')
                password = project.findProperty('artifactory_password') ?: System.getenv('ARTIFACTORY_PASSWORD')
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)