Gradle run java application

Building Java Applications with libraries Sample

To prepare your software project for growth, you can organize a Gradle project into multiple subprojects to modularize the software you are building. In this guide, you’ll learn how to structure such a project on the example of a Java application. However, the general concepts apply for any software you are building with Gradle. You can follow the guide step-by-step to create a new project from scratch or download the complete sample project using the links above.

What you’ll build

You’ll build a Java application that consists of an application and multiple library projects.

What you’ll need

  • A text editor or IDE — for example IntelliJ IDEA
  • A Java Development Kit (JDK), version 8 or higher — for example AdoptOpenJDK
  • The latest Gradle distribution

Create a project folder

Gradle comes with a built-in task, called init , that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew .

The first step is to create a folder for the new project and change directory into it.

Run the init task

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 3: Java as implementation language. Afterwards, select 2: Add library projects . Next you can choose the DSL for writing buildscripts — 1 : Groovy or 2: Kotlin . For the other questions, press enter to use the default values.

Читайте также:  Как остановить scanner java

The output will look like this:

$ gradle init Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 2 Split functionality across multiple subprojects?: 1: no - only one application project 2: yes - application and library projects Enter selection (default: no - only one application project) [1..2] 2 Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] 3 Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit 4) [1..4] Project name (default: demo): Source package (default: demo): BUILD SUCCESSFUL 2 actionable tasks: 2 executed

The init task generates the new project with the following structure:

├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle.kts (3) ├── buildSrc │ ├── build.gradle.kts (4) │ └── src │ └── main │ └── kotlin (5) │ ├── demo.java-application-conventions.gradle.kts │ ├── demo.java-common-conventions.gradle.kts │ └── demo.java-library-conventions.gradle.kts ├── app │ ├── build.gradle.kts (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── app │ │ ├── App.java │ │ └── MessageUtils.java │ └── test (8) │ └── java │ └── demo │ └── app │ └── MessageUtilsTest.java ├── list │ ├── build.gradle.kts (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── list │ │ └── LinkedList.java │ └── test (8) │ └── java │ └── demo │ └── list │ └── LinkedListTest.java └── utilities ├── build.gradle.kts (6) └── src └── main (7) └── java └── demo └── utilities ├── JoinUtils.java ├── SplitUtils.java └── StringUtils.java
├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle (3) ├── buildSrc │ ├── build.gradle (4) │ └── src │ └── main │ └── groovy (5) │ ├── demo.java-application-conventions.gradle │ ├── demo.java-common-conventions.gradle │ └── demo.java-library-conventions.gradle ├── app │ ├── build.gradle (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── app │ │ ├── App.java │ │ └── MessageUtils.java │ └── test (8) │ └── java │ └── demo │ └── app │ └── MessageUtilsTest.java ├── list │ ├── build.gradle (6) │ └── src │ ├── main (7) │ │ └── java │ │ └── demo │ │ └── list │ │ └── LinkedList.java │ └── test (8) │ └── java │ └── demo │ └── list │ └── LinkedListTest.java └── utilities ├── build.gradle (6) └── src └── main (7) └── java └── demo └── utilities ├── JoinUtils.java ├── SplitUtils.java └── StringUtils.java
1 Generated folder for wrapper files
2 Gradle wrapper start scripts
3 Settings file to define build name and subprojects
4 Build script of buildSrc to configure dependencies of the build logic
5 Source folder for convention plugins written in Groovy or Kotlin DSL
6 Build script of the three subprojects — app , list and utilities
7 Java source folders in each of the subprojects
8 Java test source folders in the subprojects
Читайте также:  Тег STYLE, атрибут media

You now have the project setup to build a Java application which is modularized into multiple subprojects.

Источник

Building Java Applications Sample

This guide demonstrates how to create a Java application with Gradle using gradle init . You can follow the guide step-by-step to create a new project from scratch or download the complete sample project using the links above.

What you’ll build

You’ll generate a Java application that follows Gradle’s conventions.

What you’ll need

  • A text editor or IDE — for example IntelliJ IDEA
  • A Java Development Kit (JDK), version 8 or higher — for example AdoptOpenJDK
  • The latest Gradle distribution

Create a project folder

Gradle comes with a built-in task, called init , that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew .

The first step is to create a folder for the new project and change directory into it.

Run the init task

From inside the new project directory, run the init task using the following command in a terminal: gradle init . When prompted, select the 2: application project type and 3: Java as implementation language. Next you can choose the DSL for writing buildscripts — 1 : Groovy or 2: Kotlin . For the other questions, press enter to use the default values.

The output will look like this:

$ gradle init Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] 2 Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] 3 Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Groovy) [1..2] 1 Select test framework: 1: JUnit 4 2: TestNG 3: Spock 4: JUnit Jupiter Enter selection (default: JUnit 4) [1..4] Project name (default: demo): Source package (default: demo): BUILD SUCCESSFUL 2 actionable tasks: 2 executed

The init task generates the new project with the following structure:

├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle.kts (3) └── app ├── build.gradle.kts (4) └── src ├── main │ └── java (5) │ └── demo │ └── App.java └── test └── java (6) └── demo └── AppTest.java
├── gradle (1) │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew (2) ├── gradlew.bat (2) ├── settings.gradle (3) └── app ├── build.gradle (4) └── src ├── main │ └── java (5) │ └── demo │ └── App.java └── test └── java (6) └── demo └── AppTest.java
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
5 Default Java source folder
6 Default Java test source folder

You now have the project setup to build a Java application.

Review the project files

The settings.gradle(.kts) file has two interesting lines:

rootProject.name = "demo" include("app")

Источник

Оцените статью