【问题标题】:How to cherry pick ZIP contents with Gradle Distribution plugin?如何使用 Gradle Distribution 插件挑选 ZIP 内容?
【发布时间】:2014-09-20 16:41:06
【问题描述】:

我的 Gradle 构建当前在我的项目根目录中的 build 目录下生成以下目录结构:

myapp/
    src/
    build.gradle
    build/
        docs/
            groovydoc/* (all Groovydocs)
        libs/
            myapp-SNAPSHOT.jar
            myapp-SNAPSHOT-sources.jar
        reports/
            codenarc/
                main.html
        test-results/* (JUnit test results)

我想添加 distribution plugin(或任何可以实现我的目标的东西,真的)让 Gradle 生成具有以下目录结构的 ZIP 文件:

myapp-SNAPSHOT-buildreport.zip/
    tests/
        (JUnit tests from build/test-results above)
    reports/
        main.html (CodeNarc report from build/reports/codenarc above)
    api/
        (Groovydocs from build/docs above)
    source/
        myapp-SNAPSHOT-sources.jar (from build/libs above)
    bin/
        myapp-SNAPSHOT.jar (from build/libs above)

阅读插件的文档后,我不知道如何配置它来满足这些需求。很明显我需要运行gradle distZip,但至于如何实际配置它以生成所需的目录结构,它似乎没有提供任何文档/示例。有什么想法吗?

注意:JAR 的版本显然是SNAPSHOT,并通过-Pversion=SNAPSHOT 命令行参数传递到Gradle 构建中。

【问题讨论】:

    标签: plugins configuration gradle zip


    【解决方案1】:

    我可能不会使用分发插件,而是创建一个新的自定义Zip 任务。它看起来像这样:

    task buildreportZip(type: Zip, dependsOn: build) {
        classifier = 'buildreport'
    
        from('build/test-results') {
            into 'tests'
        }
    
        from('build/reports/codenarc') {
            into 'reports'
        }
    
        from('build/docs') {
            into 'api'
        }
    
        from(sourcesJar) { // or whatever you source jar task name is
            into 'source'
        }
    
        from(jar) {
            into 'bin'
        }
    }
    

    【讨论】:

      【解决方案2】:

      Gradle Distribution plugin 自动具有默认值(问题是文档没有告诉我们默认值,但 Gradle 项目默认结构是假定的)所以如果您的 Gradle 项目是公平的直截了当并且已经在使用src/main/groovysrc/main/java,您通常只需要...

      使用 CopySpec 反转模式,让您的 into{}(创建一个目录)包含 from{} 的内容,而不是相反,如下所示:

      apply plugin: 'groovy'
      apply plugin: 'eclipse'
      apply plugin: 'application'
      
      distributions {
          main {
              baseName= 'vz_sde_dm'
              contents {
                  into('bin/config') {
                      from 'config'
                  }
                  into('lib/samples') {
                      from 'samples'
                  }
              }
          }
      }
      

      请注意,我不需要为我的 contents{} 定义 from{},而只需要定义 into{}s 这是因为我已经在使用默认的 Gradle Groovy 项目布局并且只添加了 2 个额外文件夹(config 和 @987654331 @) 在我的 Eclipse 项目下,但与我的常规构建文件夹布局相比,我的分发需要这 2 个文件夹进入稍微不同的层次结构。

      【讨论】:

      • 非常感谢,gradle的文档太差了,我找不到你提到的信息
      【解决方案3】:

      我也在尝试制作自定义布局,但在弄清楚如何从 yourProject.zip/yourProject/lib 目录中排除 build/libs 中的项目输出(并排除一般的东西)并将其替换时遇到了真正的麻烦进入 yourProject.zip/yourProject。

      在数天的 API 中搜索和探索几个小时后,我终于找到了可以使用 Distribution 和底层 CopySpec 的实际配置(分别记录在 Gradle 5.6.1 的 herehere ,您只需将 URL 中的 5.6.1 替换为 current 即可获取最新的 API 文档,5.6.1 恰好是我正在使用的版本):

      distributions {
          main {
              baseName = appName
      
              contents {
                  filesMatching("**/${appName}.jar", {
                      if (it.getPath().contains('/lib/')) {
                          it.setPath(it.getPath().replace('lib/', ''))
                      }
                  })
      
                  into('config') {
                      exclude(['server.crt', 'spotbugs-exclusion-filters.xml'])
                      from 'src/main/resources'
                  }
              }
          }
      }
      

      对于排除,唯一有效的方法是匹配 glob 模式,并通过主发行版内容 CopySpec 的 filesMatching 方法使用闭包指定正确的操作(将其复制到根 dist 目录而不是 root/lib)。您还可以看到配置的目的地如何从根目录更改为根目录/config 目录。也感谢 Thad 的回答帮助指导我进行正确的构建配置。

      【讨论】:

        猜你喜欢
        • 2016-02-06
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        相关资源
        最近更新 更多