-
Notifications
You must be signed in to change notification settings - Fork 0
Gradle
Use SDKman https://gradle.org/install/#with-a-package-manager
sdk install gradle 8.7
touch gradle=8.4 > .sdkmanrc
sdk env install
Check version
gradle --version
Create configuration
vi ~/.gradle/gradle.properties
Use configuration
echo "export GRADLE_USER_HOME=~/.gradle" >> ~/.zshrc
All dependencies will be downloaded here https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home
Check the folder is used
./gradlew --debug | grep "Gradle user home"
Check structure https://docs.gradle.org/current/userguide/directory_layout.html#dir:gradle_user_home
List:
- dependencies are stored in the local repository
~/.gradle/caches/modules-2
- global properties, if used, are stored in
~/.gradle/gradle.properties
, see precedence rules
Parameter:
--debug
--exclude-task
https://docs.gradle.org/current/userguide/command_line_interface.html
Bare minimum:
- one
gradle
application - one package, one class
The resulting structure is:
└── my-application
└── src
├── main
│ └── java
│ └── myPackage
│ └── MyClass.java
└── test
└── java
└── package
└── MyClassTest.java
Well, you don't have to store source file in src/main/java
.
sourceSets {
main {
java {
srcDirs '$DIRECTORY'
}
}
}
https://stackoverflow.com/questions/31077844/add-another-java-source-directory-to-gradle-script
├── gradle 1
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew 2
└── gradlew.bat 2
├── settings.gradle 3
└── app
├── build.gradle 4
1 Generated folder for wrapper files 2 Gradle wrapper start scripts 3 Settings file to define build name and subprojects 4 Build script of app project
Create application:
- in a new folder named
<APPLICATION_NAME>
- in package
<APPLICATION_NAME>
- using Groovy syntax
- using Java 21 and sdkman
- using Junit as test framework
export APPLICATION_NAME=<APPLICATION_NAME>;
mkdir $APPLICATION_NAME; cd $APPLICATION_NAME; gradle init --type java-application --project-name=$APPLICATION_NAME --dsl=groovy --test-framework=junit-jupiter --package=$APPLICATION_NAME --incubating --java-version 21; echo "java=21.0.2-tem" > .sdkmanrc
Gradle will not create a directory by itself. It'll do it, and the name will be configurable once this PR is merged.
Run and test with wrapper
./gradlew build
./gradlew run
./gradlew test
https://spring.io/guides/gs/gradle
Create structure
export APPLICATION_NAME=basics
mkdir --parents src/main/java/$APPLICATION_NAME
Create a class
vi src/main/java/$APPLICATION_NAME/Main.java
Add some output
package <APPLICATION_NAME>;
public class Main {
public static void main(String[] args) {
System.out.println("Main has been executed");
}
}
Create build.gradle
echo "apply plugin: 'java'" > build.gradle
Try to build
gradle build
Select your JDK version
echo "java=21.0.2-tem" > .sdkmanrc
Add the JDK version to Gradle
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
Add the wrapper
gradle wrapper
Make the application runnable
plugins {
id 'application'
}
application {
mainClass = '<APPLICATION_NAME>.Main'
}
Build
./gradlew build
Run from Gradle
./gradlew run
Run from java
java --class-path=build/classes/java/main basics.Main
Start your IDE
intellij-idea-ultimate . &
Setup project SDK (Project structure) Open Gradle window (import if needed)
Settings/Gradle : select JDK
Run the Main
class
Settings/Gradle : Test with IDEAJ (quicker)
Run the Main
class
Run the Main
class in Debug
https://docs.gradle.org/current/userguide/java_testing.html
Create test structure
mkdir --parents src/test/java/$APPLICATION_NAME
Create test file
vi src/test/java/$APPLICATION_NAME/MainTest.java
Type in
package <APPLICATION_NAME>;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
class MainTest {
@Test
void test1() {
assertNotEquals("foo", "bar");
}
}
Add dependencies provider
repositories {
mavenCentral()
}
Add two dependency
- the first is the test assertion
- the second is the test runner
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
Tell Gradle to start JUnit in test
goal
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
exceptionFormat "full"
}
}
Run the tests
./gradlew test
Settings/Gradle : Test with IDEAJ (quicker)
Create application, choosing:
- type of project: application (not basic)
- build script DSL: use Groovy (not Kotlin)
Init
mkdir hello && cd hello
git init
gradle init
If you use gradle in version 8.3, this is no longer required.
It will be asked during init
.
Setup IntelliJ:
- setup SDK
- import Gradle project (right-click on build.gradle file)
- open Gradle window
- run code
- from Gradle Tool view, run application/run
- open the application file and run main from the gutter
- run tests
- settings "Build/Build tools/Gradle"
- in "Gradle user home", check value is valid
- in "Run test using", choose "Intellij IDEA" instead of Gradle
- open AppTest.java and run using Play in gutter, should pass
- change assertion to make it fail
- settings "Build/Build tools/Gradle"
List
./gradlew tasks
Task classification
There are two classes of tasks that can be executed: Actionable tasks have some action(s) attached to do work in your build: compileJava. Lifecycle tasks are tasks with no actions attached: assemble, build.
Typically, a lifecycle tasks depends on many actionable tasks, and is used to execute many tasks at once.
You can view which actionable tasks eg. test
are linked on lifecycle task eg. build
using IntelliJ
gradle test
Using class name, in CLI
./gradlew test --tests TestClassName
Using test name, in CLI
./gradlew test --tests org.package.myTestClass.myTestMethod
Using test name, in build.gradle
test {
filter {
includeTestsMatching "*myTestMethod*"
}
}
https://docs.gradle.org/current/userguide/java_testing.html#test_filtering
./gradlew test --info --tests PermissionUtilisateurRepositoryITest
https://docs.gradle.org/current/userguide/logging.html#sec:choosing_a_log_level
Using --debug
would literally blow up your screen!
Configure logging
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
exceptionFormat "full"
}
}
Or use this plugin https://github.com/radarsh/gradle-test-logger-plugin
And replace tesrtLogging
by testLogger
test {
useJUnitPlatform()
testlogger {
theme 'mocha'
showSimpleNames true
}
}
gradle build
Build tasks
-----------
assemble - Assembles the outputs of this project.
bootBuildImage - Builds an OCI image of the application using the output of the bootJar task
bootJar - Assembles an executable jar archive containing the main classes and their dependencies.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
generateGitProperties - Generate a git.properties file.
jar - Assembles a jar archive containing the classes of the 'main' feature.
resolveMainClassName - Resolves the name of the application's main class.
resolveTestMainClassName - Resolves the name of the application's test main class.
testClasses - Assembles test classes.
Generate artifact gradle assemble
Run the tests Assemble
assemble
is similar to Maven’s package
and comes from base
plugin (applied automatically by java and other plugins).
When you run it, it generates the artifact in build/libs/<APPLICATION>-<VERSION>.jar
gradle assemble --console=verbose
https://tomgregory.com/gradle/gradle-assemble-task-essentials/
Enable proxy
systemProp.http.proxyHost=[PROXY SERVER]
systemProp.http.proxyPort=[PROXY PORT]
To ensure the proper Gradle version is used. https://medium.com/@bherbst/understanding-the-gradle-wrapper-a62f35662ab7
Well, so do we need sdkman to install gradle at all ?
If you use gradle in version 8.3, this is no longer required.
It is automatically performed during init
.
Handful:
- install (running wrapper task) :
gradle wrapper
- build :
gradlew build
- upgrade :
gradlew wrapper --gradle-version <VERSION>
- get current version :
gradlew -v
Specify to use wrapper in build.gradle
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
Check you commit the following files:
settings.gradle
gradle-wrapper.jar
gradle-wrapper.properties
Matching .gitignore
file https://github.com/github/gitignore/blob/main/Gradle.gitignore
Sync
./gradlew build --refresh-dependencies
Remove forcefully for all projects
rm -rf $HOME/.gradle/caches/
https://stackoverflow.com/questions/13565082/how-can-i-force-gradle-to-redownload-dependencies
In IDE, run "bootRun" task in debug
https://github.com/liquibase/liquibase-gradle-plugin/blob/master/doc/usage.md
Given test class name
TEST_CLASS_NAME='MyTest' ; ./gradlew test --tests $TEST_CLASS_NAME
https://github.com/nebula-plugins/gradle-lint-plugin/wiki
https://docs.gradle.org/current/userguide/build_init_plugin.html#supported_gradle_build_types Values:
java-application
java-gradle-plugin
java-library
It has the following features:
- Uses the “application” plugin to produce a command-line application implemented in Java
- Uses the “mavenCentral” dependency repository
- Uses JUnit 4 for testing
- Has directories in the conventional locations for source code
- Contains a sample class and unit test, if there are no existing source or test files
https://docs.gradle.org/current/userguide/build_init_plugin.html#sec:java_application