简体   繁体   English

如何在 Gradle 中创建两个任务

[英]How do I create two tasks in Gradle


I have a Java project with Gradle as build system.我有一个使用 Gradle 作为构建系统的 Java 项目。
I need to create two one-purpose build tasks , but with different versions of jar file.我需要创建两个单一用途的构建任务,但使用不同版本的jar文件。
Specifically, i need:具体来说,我需要:

  • for build task in local machine use -LOCAL version for .jar (for example projectName-LOCAL.jar )对于本地机器中的构建任务,使用-LOCAL版本为 .jar(例如projectName-LOCAL.jar

  • for build task in GitLab CI\\CD system use numbered version ( projectName-1.1.1.jar ) GitLab CI\\CD系统中的构建任务使用编号版本( projectName-1.1.1.jar

The version for GitLab CI\\CD taken from commit tag, from GitLab predefined ENV variables. GitLab CI\\CD 的版本取自提交标签,来自 GitLab 预定义的 ENV 变量。

So, i have a default build.gradle file, where:所以,我有一个默认的build.gradle文件,其中:

group = 'projectName'
version = 'LOCAL'
sourceCompatibility = '11'

jar {
    manifest {
        attributes "Main-Class": "projectName.Application"
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

This task must run on local machine and create projectName-LOCAL.jar .此任务必须在本地机器上运行并创建projectName-LOCAL.jar What he's doing is.他正在做的是。
How can I implement this in another task with numbered version especially for GitLab CI\\CD?我如何在另一个带有编号版本的任务中实现这一点,特别是对于 GitLab CI\\CD?

Gradle by default names jar files as ${baseName}-${appendix}-${version}-${classifier}.${extension} . Gradle 默认将 jar 文件命名为${baseName}-${appendix}-${version}-${classifier}.${extension}

So you can simply change version depending on a condition.因此,您可以根据条件简单地更改version

Something like this should work:这样的事情应该工作:

ext {
    isLocal = System.getenv('ENV') == 'LOCAL'
}

jar {
    if (isLocal) version = 'LOCAL'
}

For working with local, you can use ENV=LOCAL ./gradlew jar and use your normal commands without the ENV part for GitLab.对于本地工作,您可以使用ENV=LOCAL ./gradlew jar并使用您的正常命令,而无需 GitLab 的 ENV 部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM