Article directory
This section will continue First experience with Gradle After installation, create the first Hello World project. will be drawn from the project's Creation, initialization and execution to introduce. It also explains how to go Package and push to the server.
1. Premise
You need to install the environment locally firstGradle
. If it is not installed, please refer toGradle’s first experience Install.
2. Create the project and initialize it
1) Create project
Create a project folder (empty at this time) anywhereanywhere
2) Initialize project
Use the following command to initialize the project
gradle init
Seeing BUILD SUCCESSFUL means success.
3. Introduce the generated file structure
The following project files are automatically generated through the above init
Several main file contents will be posted below. (※ They are all automatically generated)
1) settings.gradle.kts
plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
}
rootProject.name = "gradle-demo"
include("app")
2) build.gradle.kts
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit Jupiter for testing.
testImplementation(libs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
// This dependency is used by the application.
implementation(libs.guava)
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
application {
// Define the main class for the application.
mainClass.set("gradle.demo.App")
}
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
3) App.java
package gradle.demo;
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
4) AppTest.java
package gradle.demo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}
4. Execution
Use the following command to execute
gradlew run
Modify the question (if there is no question below, skip this modification step)
, you need to modify the following code (※ For details, see [Gradle] Always download gradle-8.5-bin.zip when running The introduction of the article )
Continue execution
Saw the execution result: Hello World!
5. Package creation (optional)
Use the following commands to create tar packages and zip packages
gradlew build
Seeing BUILD SUCCESSFUL means success.
At this point, tar packages and zip packages will be generated
6. Push (optional)
If you want to learn more about what your build is doing behind the scenes, the best way is to publish your build. To do this, run the build task again with the --scan flag, using the command below
gradlew build --scan
Click on the URL in the console (https://gradle.com/s/juuuaabt6y3di)
※ If it is the first time, you will be asked to enter your mail address and go to mail for verification.
Verification mail is roughly as follows:
At this point, your first project created using Gradle is complete!