Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Command from configurations.compile.collect

Tags:

gradle

I stumbled upon one article about how to make fat jar files in Gradle.

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

My questions are

  1. what is the jar element? Is it a way to declare properties. As I understand, I can define properties as follows:

    prop1{ prop2{ prop3.prop4=5 } }

which is equivalent to: prop1.prop2.prop3.prop4=5

  1. the from statement: Is it a method? I mean is it from (params).

I also found an article about ConfigurationContainer: https://gradle.org/docs/current/javadoc/org/gradle/api/artifacts/ConfigurationContainer.html.

However, I can't link the information to make a full picture here.

like image 934
TrongBang Avatar asked Feb 09 '15 23:02

TrongBang


People also ask

What are configurations in Gradle?

A “configuration” is a named grouping of dependencies. A Gradle build can have zero or more of them. A “repository” is a source of dependencies. Dependencies are often declared via identifying attributes, and given these attributes, Gradle knows how to find a dependency in a repository.

How do I run a Gradle test from command line?

Use the command ./gradlew test to run all tests.

What is runtimeClasspath in Gradle?

runtimeClasspath' contains the compiled classes of the source set, and task autowiring automatically adds the necessary task dependencies. Maybe you want 'sourceSets. main. compileClasspath'.

How do I list all tasks in Gradle?

To get an overview of all Gradle tasks in our project, we need to run the tasks task. Since Gradle 5.1, we can use the --group option followed by a group name. Gradle will then show all tasks belonging to the group and not the other tasks in the project.


1 Answers

  1. jar is a task name. It has nothing to do with properties. Tasks are the basic building block of every Gradle build. It is equivalent to tasks.jar

  2. from is a method of the CopySpec class. Many tasks extend from the CopySpec class and inherit the from method.

configurations.compile is a reference to the all of the artifacts defined in the compile configuration.

.collect {} is a method call with one argument that is a closure. {} is an empty closure.

{ it.isDirectory() ? it : zipTree(it) } is the closure (aka function) that returns a collection of files to be include in the JAR. it is the default argument of every closure that in the case, will be a JAR artifact instance from configurations.compile or a local file path instance. It could be a file path because configurations support both JARS and local files system paths of compiled .class files.

The logic in the closure is in the form of <conditional> ? <A> : <B> and means if <conditional> then return <A> else return <B>.

The conditional it.isDirectory() evaluates to True if the it variable is a directory. So if it is a directory we return it. If it's not a directory, then the code assumes it's a JAR.

zipTree(it) is a method that extracts all the files from a JAR and returns them as a collection.

All of the file (directories or exploded JARS) are returned from the closure and then "combined" by collect into a single collection (e.g. FileTree).

like image 157
cmcginty Avatar answered Sep 28 '22 04:09

cmcginty