Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration compile and runtime for gradle

Tags:

java

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

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? I look up google, but couldn't find any answer. I'm really new to gradle

like image 300
Joedie 123 Avatar asked Jan 31 '16 20:01

Joedie 123


People also ask

What is configurations runtime Gradle?

Introduction. Gradle dependencies are grouped into sets called configurations. Different configurations are used for building classpath for the major two tasks — compile classpath is used for compilation and runtime classpath is used for running the application.

What is compile Gradle?

Compile − The dependencies required to compile the production source of the project. Runtime − The dependencies required by the production classes at runtime. By default, it also includes the compile time dependencies. Test Compile − The dependencies required to compile the test source of the project.

What is the difference between compile and implementation in Gradle?

Fortunately, the implementation dependency configuration provides the same functionality as compile. You should always use implementation rather than compile for dependencies, as compile is now deprecated or removed in the case of Gradle 7+.

What is compile classpath in Gradle?

Annotation Type CompileClasspath Attaching this annotation to a property means that changes that do not affect the API of the classes in classpath will be ignored. The following kinds of changes to the classpath will be ignored: Changes to the path of jar or top level directories.


1 Answers

For starters, you do not need both configurations.compile and configurations.runtime. In gradle, the compile time dependencies are already included in runtime config automatically - which makes compile a subset of runtime. 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. collect is groovy for: do the following operation for every element of a collection and return the result as a set. 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.

it is groovy shorthand for iterator - so it represents each element of the aforesaid collection.

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

(See zipTree reference here)

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.

like image 132
RaGe Avatar answered Sep 19 '22 11:09

RaGe