Blog
Gradle lernen - ein Open-Source-Werkzeug zur Automatisierung von Builds

Gradle ist ein Open-Source-Build-Automatisierungstool, das auf Flexibilität und Leistung ausgerichtet ist. Gradle Build-Skripte werden mit einer Groovy- oder Kotlin-DSL geschrieben. Der Schwerpunkt von Gradle liegt auf der Erstellung Ihrer Software. Gradle benötigt Java zum Betrieb. Gradle wurde 2007 veröffentlicht, hat eine großartige Erfolgsbilanz, ist kampferprobt und ist ein stabiles Build-Tool. Gradle ist in der Branche weit verbreitet, um die Erstellung von Software zu automatisieren. Gradle verfügt über eine hervorragende Dokumentation mit mehreren Tutorials und einem YouTube-Kanal mit Tutorials und Vorträgen.
Gradle ist in hohem Maße erweiterbar. Mit Hilfe von Plugins und Tasks kann Gradle erweitert werden, um alle möglichen Aufgaben auszuführen, wie z.B. Quellcode erstellen, Dokumentation generieren, Artefakte erstellen, Code generieren und vieles mehr. Gradle kann Java, Groovy, Scala, Python und native Sprachen wie C und C++ erstellen. Es gibt eine Vielzahl von Community-Plugins, mit denen Gradle noch viel mehr kann. Lassen Sie uns herausfinden, wie Sie dieses nützliche Tool verwenden können.
Einige Annahmen
Bevor wir fortfahren, gehe ich davon aus, dass Sie einen Mac haben, mit Homebrew eingerichtet sind und Erfahrungen mit JVM-Sprachen wie Java oder Kotlin haben. Gradle ist sehr flexibel und unterstützt mehrere Sprachen für DSL, aber wir werden die Kotlin-DSL verwenden.
Installation
Auf einem Mac kann gradle einfach mit installiert werden:
$ brew install gradle
$ gradle --version
Welcome to Gradle 5.0!
Here are the highlights of this release:
- Kotlin DSL 1.0
- Task timeouts
- Dependency alignment aka BOM support
- Interactive `gradle init`
For more details see https://docs.gradle.org/5.0/release-notes.html
------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------
Build time: 2018-11-26 11:48:43 UTC
Revision: 7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987
Kotlin DSL: 1.0.4
Kotlin: 1.3.10
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 1.8.0_172 (Oracle Corporation 25.172-b11)
OS: Mac OS X 10.14.1 x86_64
Erstellen Sie eine Java-Anwendung
Lassen Sie uns ein neues Projekt erstellen, das eine Java-Anwendung erstellen wird.
$ pwd
~/projects
$ mkdir helloworld-java
$ cd helloworld-java
Um ein neues Projekt zu erstellen, benötigen wir die Aufgabe init, die konfiguriert werden kann. Schauen wir uns die Hilfe zu dieser Aufgabe an.
$ gradle help --task :init
Options
--dsl Set the build script DSL to be used in generated scripts.
--package Set the package for source files.
--project-name Set the project name.
--test-framework Set the test framework to be used.
--type Set the type of project to generate.
Available values are:
java-application
java-library
kotlin-application
kotlin-library
scala-library
Um ein java-application Projekt zu erstellen, geben Sie Folgendes ein:
$ gradle init
--type java-application
--dsl kotlin
--test-framework junit
--package com.github.binxio.helloworld
--project-name helloworld-java
Die folgenden Dateien und Verzeichnisse werden erstellt:
.
├── build.gradle.kts
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── github
│ │ └── binxio
│ │ └── helloworld
│ │ └── App.java
│ └── resources
└── test
├── java
│ └── com
│ └── github
│ └── binxio
│ └── helloworld
│ └── AppTest.java
└── resources
Grundlegender Arbeitsablauf
Gradle verfügt über einen grundlegenden Arbeitsablauf wie das Erstellen eines neuen Projekts, das Testen von Code, das Ausführen von Code und das Erstellen des Projekts zu einem Artefakt.
# clean the project
$ gradle clean
1 actionable task: 1 executed
# run the tests
$ gradle test
3 actionable tasks: 3 executed
# run the application
$ gradle run
Hello world.
# create a distribution zip
$ gradle distZip
4 actionable tasks: 4 executed
# view the contents of the zip
$ unzip -l ./build/distributions/helloworld-java.zip
Archive: ./build/distributions/helloworld-java.zip
Length Date Time Name
--------- ---------- ----- ----
0 12-02-2018 15:39 helloworld-java/
0 12-02-2018 15:39 helloworld-java/lib/
1259 12-02-2018 15:39 helloworld-java/lib/helloworld-java.jar
2740721 12-02-2018 15:05 helloworld-java/lib/guava-26.0-jre.jar
19936 12-02-2018 15:05 helloworld-java/lib/jsr305-3.0.2.jar
193322 12-02-2018 15:05 helloworld-java/lib/checker-qual-2.5.2.jar
13704 12-02-2018 15:05 helloworld-java/lib/error_prone_annotations-2.1.3.jar
8764 12-02-2018 15:05 helloworld-java/lib/j2objc-annotations-1.1.jar
3482 12-02-2018 15:05 helloworld-java/lib/animal-sniffer-annotations-1.14.jar
0 12-02-2018 15:39 helloworld-java/bin/
2510 12-02-2018 15:39 helloworld-java/bin/helloworld-java.bat
5518 12-02-2018 15:39 helloworld-java/bin/helloworld-java
--------- -------
2989216 12 files
Aufgaben
Wenn ein Projekt mit der Aufgabe init erstellt wurde, wurde eine Reihe von Plugins konfiguriert. Diese Plugins fügen Grade Aufgaben hinzu, mit denen es allgemeine Aufgaben ausführen kann, die für die erfolgreiche Erstellung von Software erforderlich sind. Um aufzulisten, welche Aufgaben verfügbar sind, geben Sie
$ gradle tasks
------------------------------------------------------------
Application tasks
-----------------
run - Runs this project as a JVM application
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Projekt-Konfiguration
Ein Gradle-Projekt wird durch zwei Dateien definiert: build.gradle.kts, in der das Projekt, die Abhängigkeiten und die Aufgaben definiert werden, und settings.gradle.kts, in der die Einstellungen in Form von Schlüssel-Wert-Paaren festgelegt werden.
build.gradle.kts:
plugins {
// Apply the java plugin to add support for Java
java
// Apply the application plugin to add support for building an application
application
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is found on compile classpath of this component and consumers.
implementation("com.google.guava:guava:26.0-jre")
// Use JUnit test framework
testImplementation("junit:junit:4.12")
}
application {
// Define the main class for the application
mainClassName = "com.github.binxio.helloworld.App"
}
settings.gradle.kts:
rootProject.name = "helloworld-java"
Eine Java-Bibliothek erstellen
Um einen java-library Typ zu erstellen:
$ pwd
~/projects
# create a new project dir
$ mkdir helloworld-lib
$ cd helloworld-lib
# initialize a project
$ gradle init
--type java-library
--dsl kotlin
--test-framework junit
--package com.github.binxio.helloworld
--project-name helloworld-lib
# run the test
$ gradle test
3 actionable tasks: 2 executed, 1 up-to-date
# create a jar
$ gradle assemble
# view the contents of the jar
unzip -l build/libs/helloworld-lib.jar
Archive: build/libs/helloworld-lib.jar
Length Date Time Name
--------- ---------- ----- ----
0 12-02-2018 15:48 META-INF/
25 12-02-2018 15:48 META-INF/MANIFEST.MF
0 12-02-2018 15:48 com/
0 12-02-2018 15:48 com/github/
0 12-02-2018 15:48 com/github/binxio/
0 12-02-2018 15:48 com/github/binxio/helloworld/
388 12-02-2018 15:48 com/github/binxio/helloworld/Library.class
--------- -------
413 7 files
Erstellen Sie eine Kotlin-Anwendung
Um einen kotlin-application Typ zu erstellen:
$ pwd
~/projects
# create a new project dir
$ mkdir helloworld-kotlin
$ cd helloworld-kotlin
# initialize a project
$ gradle init
--type kotlin-application
--dsl kotlin
--test-framework kotlintest
--package com.github.binxio.helloworld
--project-name helloworld-kotlin
# run the test
$ gradle test
5 actionable tasks: 5 executed
# create a jar
$ gradle run
Hello world.
$ gradle distZip
4 actionable tasks: 4 executed
# view the contents of the zip
$ unzip -l ./build/distributions/helloworld-kotlin.zip
Archive: ./build/distributions/helloworld-kotlin.zip
Length Date Time Name
--------- ---------- ----- ----
0 12-02-2018 15:55 helloworld-kotlin/
0 12-02-2018 15:55 helloworld-kotlin/lib/
2410 12-02-2018 15:55 helloworld-kotlin/lib/helloworld-kotlin.jar
13769 12-02-2018 15:53 helloworld-kotlin/lib/kotlin-stdlib-jdk8-1.3.10.jar
3137 12-02-2018 15:53 helloworld-kotlin/lib/kotlin-stdlib-jdk7-1.3.10.jar
1181292 12-02-2018 15:53 helloworld-kotlin/lib/kotlin-stdlib-1.3.10.jar
151024 12-02-2018 15:53 helloworld-kotlin/lib/kotlin-stdlib-common-1.3.10.jar
17536 12-02-2018 15:53 helloworld-kotlin/lib/annotations-13.0.jar
0 12-02-2018 15:55 helloworld-kotlin/bin/
2493 12-02-2018 15:55 helloworld-kotlin/bin/helloworld-kotlin.bat
5498 12-02-2018 15:55 helloworld-kotlin/bin/helloworld-kotlin
--------- -------
1377159 11 files
Erstellen einer Kotlin-Bibliothek
Um einen kotlin-library Typ zu erstellen:
$ pwd
~/projects
# create a new project dir
$ mkdir helloworld-kotlin-library
$ cd helloworld-kotlin-library
# initialize a project
$ gradle init
--type kotlin-application
--dsl kotlin
--test-framework kotlintest
--package com.github.binxio.helloworld
--project-name helloworld-kotlin-library
# run the test
$ gradle test
5 actionable tasks: 5 executed
# create a jar
$ gradle assemble
2 actionable tasks: 1 executed, 1 up-to-date
# view the contents of the jar
$ unzip -l build/libs/helloworld-lib.jar
unzip -l build/libs/helloworld-kotlin-library.jar
Archive: build/libs/helloworld-kotlin-library.jar
Length Date Time Name
--------- ---------- ----- ----
0 12-02-2018 15:58 META-INF/
25 12-02-2018 15:58 META-INF/MANIFEST.MF
55 12-02-2018 15:58 META-INF/helloworld-kotlin-library.kotlin_module
0 12-02-2018 15:58 com/
0 12-02-2018 15:58 com/github/
0 12-02-2018 15:58 com/github/binxio/
0 12-02-2018 15:58 com/github/binxio/helloworld/
1146 12-02-2018 15:58 com/github/binxio/helloworld/AppKt.class
766 12-02-2018 15:58 com/github/binxio/helloworld/App.class
--------- -------
1992 9 files
Erstellen einer Scala-Bibliothek
Um einen scala-library Typ zu erstellen:
$ pwd
~/projects
# create a new project dir
$ mkdir helloworld-scala-library
$ cd helloworld-scala-library
# initialize a project
$ gradle init
--type scala-library
--dsl kotlin
--test-framework scalatest
--package com.github.binxio.helloworld
--project-name helloworld-scala-library
# run the test
$ gradle test
5 actionable tasks: 5 executed
# create a jar
$ gradle assemble
2 actionable tasks: 1 executed, 1 up-to-date
# view the contents of the jar
$ unzip -l build/libs/helloworld-scala-library.jar ✔ 985 16:03:42
Archive: build/libs/helloworld-scala-library.jar
Length Date Time Name
--------- ---------- ----- ----
0 12-02-2018 16:03 META-INF/
25 12-02-2018 16:03 META-INF/MANIFEST.MF
0 12-02-2018 16:03 com/
0 12-02-2018 16:03 com/github/
0 12-02-2018 16:03 com/github/binxio/
0 12-02-2018 16:03 com/github/binxio/helloworld/
737 12-02-2018 16:03 com/github/binxio/helloworld/Library.class
--------- -------
762 7 files
Fazit
Gradle ist ein Build-Automatisierungstool, das die meisten Programmiersprachen erstellen kann. Das Tolle an Gradle ist, dass es für jede Anwendung dieselben Plugins und dieselbe Konfiguration verwendet. In diesem Projekt haben wir ein Java-, Kotlin- und Scala-Projekt erstellt, Tests durchgeführt und Build-Artefakte erstellt. Kotlin ist das Build-Tool der Wahl, wenn Sie Kotlin-Projekte erstellen.
Verfasst von
Dennis Vriend
Contact