简体   繁体   English

只有配置且没有来源的Gradle项目

[英]Gradle project with only configuration and no sources

I'd like to create a new Gradle project without any sources. 我想创建一个没有任何资源的新Gradle项目。 I'm going to put there some configuration files and I want to generate a zip file when I build. 我将在那里放置一些配置文件,我想在构建时生成一个zip文件。 With maven I'd use the assembly plugin. 使用maven我会使用程序集插件。 I'm looking for the easiest and lightest way to do this with Gradle. 我正在寻找使用Gradle执行此操作的最简单,最轻松的方法。 I wonder if I need to apply the java plugin even if I don't have any sources here, just because it provides some basic and useful tasks like clean, assemble and so on. 我想知道我是否需要应用java插件,即使我这里没有任何来源,只是因为它提供了一些基本和有用的任务,如清理,组装等。 Generating a zip is pretty straightforward, I know how to do that, but I don't know where and how to put the zip generation within the gradle world. 生成一个zip是非常简单的,我知道如何做到这一点,但我不知道在哪里以及如何将zip生成放在gradle世界中。

I've done it manually until now. 到目前为止我手动完成了。 In other words, for projects where all I want to do is create some kind of distro and I need the basic lifecycle tasks like assemble and clean, I've simply created those tasks along with the needed dependencies. 换句话说,对于我想要创建某种发行版的项目,我需要基本的生命周期任务,如汇编和清理,我只是创建了这些任务以及所需的依赖项。

But there is the 'base' plugin (mentioned under "Base plugins" of the "Standard Gradle Plugins" in the user's guide) that seems to fit the bill nicely for this functionality. 但是有一个'基础'插件(在用户指南中的“标准Gradle插件”的“基本插件”中提到)似乎很适合这个功能。 Note though that the user guide mentions that this and the other base plugins are not yet considered part of the Gradle API and are not really documented. 请注意,用户指南提到这个和其他基本插件尚未被认为是Gradle API的一部分,并且没有真正记录。

The results are pretty much identical to yours, the only difference being that there are no confusing java specific tasks that always remain UP-TO-DATE. 结果与您的结果完全相同,唯一的区别是没有令人困惑的Java特定任务始终保持最新状态。

apply plugin: 'base'

task dist(type: Zip) {
    from('solr')
    into('solr')
}

assemble.dependsOn(dist)

Sample run: 样品运行:

$ gradle clean assemble
:clean
:dist
:assemble

BUILD SUCCESSFUL

Total time: 2.562 secs

As far as I understood, it might sound strange but looks like I need to apply the java plugin in order to create a zip file. 据我所知,它可能听起来很奇怪,但看起来我需要应用java插件才能创建一个zip文件。 Furthermore it's handy to have available some common tasks like for example clean . 此外,提供一些常见任务(例如clean非常方便。 The following is my build.gradle : 以下是我的build.gradle

apply plugin: 'java'

task('dist', type: Zip) {
    from('solr')
    into('solr')
}

assemble.dependsOn dist

I applied the java plugin and defined my dist task which creates a zip file containing a solr directory with the content of the solr directory within my project. 我应用了java插件并定义了我的dist任务,它创建了一个zip文件,其中包含一个solr目录,其中包含我项目中solr目录的内容。 The last line is handy to have the task executed when I run the common gradle build or gradle assemble , since I don't want to explicitly call the dist task. 当我运行公共gradle buildgradle assemble ,最后一行很方便执行任务,因为我不想显式调用dist任务。 This way if I work with multiple projects I just need to execute gradle build on the parent to generate all the artifacts, including the configuration zip. 这样,如果我使用多个项目,我只需要在父项上执行gradle build来生成所有工件,包括配置zip。

Please let me know if you have better solutions and add your own answer! 如果您有更好的解决方案并添加自己的答案,请告诉我们!

You could just use the groovy plugin and use ant. 你可以使用groovy插件并使用ant。 I did something like this. 我做了这样的事。 I do also like javanna's answer. 我也喜欢javanna的回答。

  task jars(dependsOn: ['dev_jars']) << {
    def fromDir = file('/database-files/non_dev').listFiles().sort()

    File dist = new File("${project.buildDir}/dist")
    dist.mkdir()

    fromDir.each { File dir ->
    File destFile = new File("${dist.absolutePath}" + "/" + "database-connection-" +     dir.name + ".jar")
    println destFile.getAbsolutePath()
    ant.jar(destfile:destFile, update:false, baseDir:dir)
  }
}

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

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