3

我正在为一个项目设置 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.
4

3 回答 3

1

我找到了另一种使用 maven-publish 插件的方法

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'
    }
}

这是输出

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

如果我在应用程序模块中进行更改,这是输出,它只是上传应用程序并为核心模块说 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
:app:processResources UP-TO-DATE
:app:classes
:app:jar
:app:generatePomFileForMavenPublication
:app:incrementalPublishToMavenRepository
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar.sha1
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.jar.md5
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom.sha1
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/app-1.0-20170609.180436-15.pom.md5
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml.sha1
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/1.0-SNAPSHOT/maven-metadata.xml.md5
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml.sha1
Upload https://repo.test.com/content/repositories/snapshots/com/example/app/maven-metadata.xml.md5
hello this should be executed ones
:core:generatePomFileForMavenPublication
:core:incrementalPublishToMavenRepository UP-TO-DATE
于 2017-06-09T18:10:42.397 回答
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
于 2017-06-08T18:13:23.213 回答
0

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

更新

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

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来执行增量发布。希望这将有助于前进。

于 2017-06-02T23:09:59.327 回答