简体   繁体   English

gradle的配置编译和运行时

[英]Configuration compile and runtime for gradle

I set up a classpath for my jar reference inside a jar.('classpath: 'wee.jar'), but apparently, I also need to type the following in my jar task 我在jar中为我的jar引用设置了一个类路径。('classpath:'wee.jar'),但显然,我还需要在我的jar任务中输入以下内容

from {
    configurations.compile.collect {
        it.isDirectory() ? it : zipTree(it)
    }
    configurations.runtime.collect {
        it.isDirectory() ? it : zipTree(it)
    }

Can Someone explain to me what does from, configuration.compile.collect, runtime, and isDirectory and zipTree do? 有人可以向我解释一下,configuration.compile.collect,runtime,isDirectory和zipTree是做什么的? I look up google, but couldn't find any answer. 我查了谷歌,但找不到任何答案。 I'm really new to gradle 我真的很傻

For starters, you do not need both configurations.compile and configurations.runtime . 对于初学者,您不需要configurations.compileconfigurations.runtime In gradle, the compile time dependencies are already included in runtime config automatically - which makes compile a subset of runtime. 在gradle中,编译时依赖项已经自动包含在运行时配置中 - 这使得编译成为运行时的子集。 Depending on what you're trying to achieve, you'll only need one or the other. 根据你想要达到的目标,你只需要一个或另一个。 So let's take this snippet: 让我们来看看这个片段:

configurations.compile.collect {
    it.isDirectory() ? it : zipTree(it)
}

A configuration represents a collection of artifacts and their dependencies. 配置表示工件及其依赖项的集合。 compile and runtime are among configs that are added by the java plugin. compileruntime属于java插件添加的配置。 collect is groovy for: do the following operation for every element of a collection and return the result as a set. collect is groovy for:对集合的每个元素执行以下操作,并将结果作为集合返回。 So effectively the line of code translates to - for all dependencies declared in configurations.compile , do the following and return the results as a set. 因此,有效的代码行转换为 - 对于configurations.compile声明的所有依赖项,执行以下操作并将结果作为集合返回。

it is groovy shorthand for iterator - so it represents each element of the aforesaid collection. it是迭代器的常规简写 - 所以它代表了上述集合的每个元素。

if `it` is a directory
    include it as is, 
else
    unpack the file and then include it

( See zipTree reference here ) 请参阅zipTree参考

Putting the whole thing together, the code is taking all compile time dependency directories and all unpacked compile time jars and including that into the jar you're building. 将整个事物放在一起,代码将所有编译时依赖目录和所有解压缩的编译时间jar包括在你正在构建的jar中。

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

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