在www.example.com修改版本的Gradle发布插件gradle.properties可以支持Jfrog Artifactory发布发布版本

xeufq47z  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(126)

所需的所有步骤如下。
1.从版本中删除'-SNAP'后缀并提交到git repo
1.在此提交上创建新的git标签

  1. Artifactory在Jfrog上使用发布版本而不是-SNAP版本部署
    1.在build.gradle中增加版本(如pom.xml),并添加“-SNAP”后缀。
    我使用了最受欢迎的Gradle发布插件(https://github.com/researchgate/gradle-release),但无法在Jfrog上使用发布版本部署artifactory(它总是使用SNAP版本)。
    下面是我的示例代码…
    gradle.build 文件
publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'in.org'
            artifactId 'some-service'
            version "${version}"
            from components.java
        }
    }
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = version.endsWith('SNAP') ? "${snapshot_repository}" : "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications("mavenJava")
            publishArtifacts = true
            publishPom = true
        }
    }
    resolve {
        repository {
            repoKey = "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

release {
    versionPropertyFile = 'gradle.properties'
    failOnCommitNeeded = false
    failOnPublishNeeded = false
    git {
        requireBranch.set('release')
    }
}

artifactoryDeploy.shouldRunAfter preTagCommit, checkCommitNeeded

updateVersion.doLast {
    def buildGradle = file('gradle.properties')
    buildGradle.text = buildGradle.text.replaceFirst(~/version = (\d++\.\d++\.\d++)/, 'version = \'$1\'')
}

gradle.properties 文件

artifactory_user=*****
artifactory_password=*****
artifactory_contextUrl=https://jfrog.in/artifactory
version=1.0.1-SNAPSHOT
snapshot_repository=gradle-dev-local
release_repository=gradle-release-local
sz81bmfz

sz81bmfz1#

我们需要使项目模块版本根据环境动态变化。如果env是prod,我们应该从版本中删除-SNAP,如下所述。

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.7.9")
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '4.29.0')
        classpath 'net.researchgate:gradle-release:3.0.2'
    }
}
    
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: "com.jfrog.artifactory"
apply plugin: 'org.springframework.boot'
apply plugin: 'net.researchgate.release'

def springboot_version = '2.7.3'
def environment = hasProperty('env') ? env : null
def artifactory_publish_version = "${environment}" != "prod" ? "${version}" : "${version}".replace("-SNAP", "")

repositories {
    jcenter()
    mavenCentral()
    maven { //Url for local dependencies in dev/qa env
        url "${artifactory_contextUrl}/gradle-dev-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
}
def artifactory_publish_version = "${environment}" != "prod" ? "${version}" : "${version}".replace("-SNAP", "")

configurations { // this needs to be add for springboot projects
   [apiElements, runtimeElements].each {
       it.outgoing.artifacts.removeIf 
       { it.buildDependencies.getDependencies(null).contains(jar) }
       it.outgoing.artifact(bootJar)
   }
}

jar {
    enabled = false
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId 'your-org'
            artifactId 'your-service'
            version "${artifactory_publish_version}"
            from components.java
        }
    }
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = artifactory_publish_version.endsWith('SNAP') ? "${snapshot_repository}" : "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publications("mavenJava")
            publishArtifacts = true
            publishPom = true
        }
    }
    resolve {
        repository {
            repoKey = "${release_repository}"
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
    }
}

release { // To create git tag
    git {
        requireBranch.set('release')
    }
}

相关问题