Gradle
Initializing a Gradle Project
Updated: November 11, 2024
$ mkdir TestGradle
$ cd $_
$ gradle init --type java-application --dsl groovy
Enter target Java version (min: 7, default: 21):
Project name (default: TestGradle):
Select application structure:
1: Single application project
2: Application and library project
Enter selection (default: Single application project) [1..2] 1
Select test framework:
1: JUnit 4
2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 1
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes
> Task :init
To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.8/samples/sample_building_java_applications.html
BUILD SUCCESSFUL in 8s
1 actionable task: 1 executed
$
gradle build
gradle run
To pass arguments, use the "--args=" command line option like this: "gradle run --args='arg1 arg2 arg3'"
Spring Boot Console Application
Updated: November 11, 2024
//
// app/build.gradle
//
plugins {
id 'application'
id "org.springframework.boot" version "3.3.5"
id 'io.spring.dependency-management' version '1.1.6'
}
repositories {
mavenCentral()
}
dependencies {
implementation libs.guava
implementation 'org.springframework.boot:spring-boot-starter'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
application {
mainClass = 'org.example.App'
}
//
// app/src/main/java/org/example/App.java
//
package org.example;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
class MyComponent {
public void doSomething() { System.out.println("Moo"); }
}
@SpringBootApplication
public class App implements CommandLineRunner {
private static Logger log = Logger.getLogger(App.class.getName());
@Autowired
MyComponent myComponent;
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
public void run(String... args) {
log.info("Running.");
myComponent.doSomething();
}
}
A Command Line Runner application can take advantage of auto-wiring by Spring.
Gradle Jobs with Different gradle-user-home
Updated: November 12, 2024
gradle -g $PWD build
gradle -g $PWD run --args='something'
This is to work around the "Timeout waiting to lock artifact cache" and "Starting a Gradle Daemon, 1 busy and 1 stopped Daemons could not be reused, use --status for details" (Also try "gradle --stop" and "gradle --status").
Setting the gradle-user-home creates the following subdirectories in the specific directory: caches, daemon, jdks, native, notification