如何发布实际更改为gradle的模块而不是全部(未更改)以设置CI?

时间:2017-06-01 14:54:16

标签: gradle gradlew gradle-3.0

我正在为一个项目设置CI模型。这个项目几乎有500个模块。我们每次只通过我们的CI工具更新工作区并构建实际更改的模块。我们正在使用gradle构建所有模块我的要求是仅将当前构建中更改的那些模块发布到nexus快照存储库。我知道有一个gradle任务来发布工件,但只发布到已更改的模块是必需的。

以下是示例。

A
B
C
D
E
F

如果B和F有变化,那么我想在nexus中只发布B和F模块,如果更改在A和F中,则只发布A和F模块。

类似于

的东西
 class IncrementalReverseTask extends DefaultTask {
      @InputDirectory
      def File inputDir

      @OutputDirectory
      def File outputDir

      @TaskAction
      void execute(IncrementalTaskInputs inputs) {
          if (!inputs.incremental)
              project.delete(outputDir.listFiles())

          inputs.outOfDate { change ->
              def targetFile = project.file("$outputDir/${change.file.name}")
              targetFile.text = change.file.text.reverse()
          }

          inputs.removed { change ->
              def targetFile = project.file("$outputDir/${change.file.name}")
              if (targetFile.exists()) {
                  targetFile.delete()
              }
          }
      }
  }

我尝试了以下方式并解决了这个问题

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.example'
            artifactId 'core'
            version '1.0-SNAPSHOT'
            from components.java

    }
    }
    repositories {
    maven {
        credentials {
            username "abcde"
            password "***********"
            }
            url "https://nexus.test.com/content/repositories/snapshots"
            }
            }
    }

task incrementalPublishToMavenRepository(type: IncrementalPublishToMavenRepository) {
  inputDir = file('.')
  publication = project.tasks.getByPath(":${project.name}:publishMavenPublicationToMavenRepository").publication
}

class IncrementalPublishToMavenRepository extends org.gradle.api.publish.maven.tasks.PublishToMavenRepository {
    @InputDirectory
    def File inputDir

    @OutputDirectory
    File generatedFileDir = project.file("${project.buildDir}/libs")

    @TaskAction
    void perform(IncrementalTaskInputs inputs) {
        println 'hello this should be executed ones'
    }
}

并低于错误

gradle jar incrementalPublishToMavenRepository

Configuration on demand is an incubating feature.
:core:compileJava UP-TO-DATE
:core:processResources NO-SOURCE
:core:classes UP-TO-DATE
:core:jar UP-TO-DATE
:app:compileJava UP-TO-DATE
:app:processResources UP-TO-DATE
:app:classes UP-TO-DATE
:app:jar UP-TO-DATE
:app:generatePomFileForMavenPublication
:app:incrementalPublishToMavenRepository FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:incrementalPublishToMavenRepository'.
> The 'repository' property is required

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

3 个答案:

答案 0 :(得分:1)

我发现使用maven-publish插件的另一种方法

"[0-9]*\.\?[0-9]\+[eE][+-][0-9]*"
                ^^

这是输出

apply plugin: 'maven-publish'

publishing {
    publications {
        maven(MavenPublication) {
            groupId 'com.example'
            artifactId 'core'
            version '1.0-SNAPSHOT'
            from components.java
        }
    }
}

publishing {
    repositories {
        maven {
        credentials {
            username "abcedf"
            password "***********"
            }
            // change to point to your repo, e.g. http://my.org/repo
            url "https://repo.test.com/content/repositories/snapshots"
        }
    }
}


task incrementalPublishToMavenRepository(type: IncrementalPublishToMavenRepository) {
  inputDir = file('src')
  publication = project.tasks.getByPath(":${project.name}:publishMavenPublicationToMavenRepository").publication
  repository = project.tasks.getByPath(":${project.name}:publishMavenPublicationToMavenRepository").repository
}

class IncrementalPublishToMavenRepository extends org.gradle.api.publish.maven.tasks.PublishToMavenRepository {
    @InputDirectory
    def File inputDir

    @OutputDirectory
    File generatedFileDir = project.file("${project.buildDir}/libs")

    @TaskAction
    void perform(IncrementalTaskInputs inputs) {
        println 'hello this should be executed ones'
    }
}

如果我在app模块中进行更改,只需上传应用程序并说出核心模块的UP-TO-DATE,这是输出

gradle jar incrementalPublishToMavenRepository
Configuration on demand is an incubating feature.
:core:compileJava UP-TO-DATE
:core:processResources NO-SOURCE
:core:classes UP-TO-DATE
:core:jar UP-TO-DATE
:app:compileJava UP-TO-DATE
:app:processResources UP-TO-DATE
:app:classes UP-TO-DATE
:app:jar UP-TO-DATE
:app:generatePomFileForMavenPublication
:app:incrementalPublishToMavenRepository UP-TO-DATE
:core:generatePomFileForMavenPublication
:core:incrementalPublishToMavenRepository UP-TO-DATE

答案 1 :(得分:0)

Gradle中的每个任务都有输入和输出属性以启用incremental task capabilities,因此可以为publishing任务配置此功能,以便将工件发布到存储库。

<强>更新

嗯,要实现完整的解决方案,需要更多的努力,但是这里有一个想法如何使用上述功能:

apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

class IncrementalPublishToMavenLocal extends org.gradle.api.publish.maven.tasks.PublishToMavenLocal {
    @Input
    int fileCount = 10

    @OutputDirectory
    File generatedFileDir = project.file("${project.buildDir}/libs")

    @TaskAction
    void perform(IncrementalTaskInputs inputs) {
        println 'hello this should be executed ones'
    }
}

task incrementalPublishToMavenLocal(type: IncrementalPublishToMavenLocal) {
  publication = project.tasks.getByPath(":${project.name}:publishMavenJavaPublicationToMavenLocal").publication
}

现在您可以使用./gradlew incrementalPublishToMavenLocal执行增量发布。希望这有助于前进。

答案 2 :(得分:0)

我尝试在不创建jar文件的情况下以下面的方式跳过uploadArchives方法

subprojects {
    apply plugin: 'java'
    apply plugin: 'maven'

    repositories {
        mavenCentral()
    }

    uploadArchives {
    repositories {
       mavenDeployer {
             repository(url: "https://repo.test.com/nexus/repos/snapshots") {
             authentication(userName: "abcdef", password: "******")
}
             pom.version = "1.1-SNAPSHOT"
             pom.groupId = "com.test"
       }
    }
}
uploadArchives {
  onlyIf { jar.didWork }
}

}

以下是使用clean

运行时的输出
:app:clean
:core:clean
:core:compileJava
:core:processResources NO-SOURCE
:core:classes
:core:jar
:app:compileJava
:app:processResources
:app:classes
:app:jar
:app:copyLicense
:app:startScripts
:app:distTar
:app:distZip
:app:uploadArchives
:core:uploadArchives

以下是没有干净的

运行时的输出
:core:compileJava UP-TO-DATE
:core:processResources NO-SOURCE
:core:classes UP-TO-DATE
:core:jar UP-TO-DATE
:app:compileJava UP-TO-DATE
:app:processResources UP-TO-DATE
:app:classes UP-TO-DATE
:app:jar UP-TO-DATE
:app:copyLicense UP-TO-DATE
:app:startScripts UP-TO-DATE
:app:distTar UP-TO-DATE
:app:distZip UP-TO-DATE
:app:uploadArchives SKIPPED
:core:uploadArchives SKIPPED