Gradle start java project

Getting Started with Gradle

In this tutorial, we’ll create a Gradle project, will run and test it, and run the executable JAR file using Gradle.

The project used in this tutorial can be found on GitHub.

Step 1. Create a project

Let’s create a Gradle project with Java.

Create a new Gradle Project with IntelliJ IDEA

New project Gradle

  1. On the welcome screen, click New Project .
  2. On the page that opens, let’s specify our project’s name (FizzBuzz) and the location.
  3. Let’s select the Java option, which is what we need for our project and Gradle since we are creating a Gradle project.
  4. IntelliJ IDEA automatically adds a project SDK (JDK) in the JDK field. In our tutorial we use the open JDK 14 version. You can change the selected JDK, IntelliJ IDEA will download the appropriate Gradle version. The only thing you need to have is the internet connection. Let’s leave the default Groovy for Gradle DSL and unselect the Add sample code option since we’re going to add our own code from scratch.
  5. We can use the default information for ArtifactId which basically is the name of our project and leave the default information in the GroupId field. Click Create .

After we’ve created our project and it finished indexing, let’s see what is inside:

    IntelliJ IDEA creates a project with the build.gradle file including the following code:

As you can see, IntelliJ IDEA conveniently adds a test dependency. IntelliJ IDEA supports code completion inside the build.gradle file. So, if we decide to add more dependencies, IntelliJ IDEA will quickly locate their names and versions.

  • IntelliJ IDEA also creates the src folder with main and test subdirectories in the Project tool window. Gradle project view
  • IntelliJ IDEA enables the dedicated Gradle tool window with a liked project and its default tasks. We will use this window to run our tasks. Gradle tool windowIf you closed this window, you can always access it from the main menu by selecting View | Tool Windows | Gradle .
  • The Gradle settings in our project are used to store the information about the linked projects, Gradle JVM, and build actions. You can quickly access them from the Gradle tool window (click on the toolbar). the Gradle settingsAs you can see, the build and test actions are delegated to Gradle. Also, the Gradle wrapper was used to determine Gradle for our project.
  • The project structure ( Control+Alt+Shift+S ) contains information about the project’s JDK and a language level used in the project. Project Structure
  • Step 2. Add Java code

    Now let’s create a Java application that outputs the first 100 FizzBuzz numbers.

    Add a Java class to the Gradle project

    Create a new class dialog

    1. In the Project tool window open the src folder.
    2. Click the main directory then right-click the java subdirectory and from the list select New | Package .
    3. In the New Package dialog, let’s enter a name of our package which is com.gradle.tutorial .
    4. Now right-click the package and select New | Java Class .
    5. In the New Java Class dialog specify a name of your Java class and click OK . In our case it is FizzBuzzProcessor .
    6. Add the following code to the main FizzBuzzProcessor class:
    Читайте также:  Some pdf to html converter

    Our application is ready. Now, let’s create the necessary tests for it.

    Create a test class

    1. Open the main class FizzBuzzProcessor in the editor, place the caret at the class name and press Control+Shift+T .
    2. In the dialog that opens, let’s make sure that our testing library is JUnit4 (if you want use JUnit5 then make sure you have the JUnit5 ) and the destination package is com.gradle.tutorial . We add the name FizzBuzzTest and leave the rest of the default options as is and click OK .
    3. Now open the created test class and add the following code:

    Step 3. Run the application with Gradle

    Let’s quickly run the application to see if it works.

    Run main class from the editor

    1. Open the main class FizzBuzzProcessor in the editor.
    2. In the gutter, click and select Run ‘FizzBuzzProcessor.main()’ . Run application in Gradle
    3. Check the result in the Run tool window. Run tool window

    Step 4. Run tests

    Now, let’s run the test we’ve created.

    Run tests in a Gradle project

    We can run our test from the editor or from the Gradle tool window using the test task. We will use the editor.

    Gradle Run test from the gutter

    • Click in the gutter of the editor.

    The result of the test will be displayed in the Run tool window.

    Run tool window /test passed

    If we change the default number in one of the tests, it will fail.

    Run tool window / test failed

    As you can see, the Run tool window displays information obout the failed test including the specific line of the code where the error occurred.

    Step 5. Create an executable JAR file

    Now let’s build our application to create an executable JAR file.

    1. In the Project tool window, double click the build.gradle file to open it in the editor.
    2. Add the following code:
  • Click in the editor to load the changes to your project.
  • In the Gradle tool window, open the project’s node, then the Tasks node and double-click the build task to run it. Gradle tool window: build taskIntelliJ IDEA creates the build directory that contains our JAR file. Project tool window: build directoryYou can run the created JAR file in the command line with java -jar command.
  • Check the Run tool window for the results. Run tool window: build taskNote that the build task includes the test task that Gradle executes. So, if we make a mistake in one of our tests, the test task will fail and the build task will fail as well. Run tool window: build with failed test
  • Step 6. Run the JAR file with Gradle

    Now let’s tweak the build.gradle file a little bit more, so we can execute our JAR file in the Run anything window.

    Run the JAR file

    1. In the Project tool window, double click the build.gradle file to open it in the editor.
    2. Let’s add id ‘application’ to the plugins section and the following code:
  • Click in the editor to load the changes to your project.
  • In the Gradle tool window, open the project’s node, then the Tasks node. We can see that Gradle added the distribution node. Open the node and double-click the assembleDist task to run it. If we check the build directory now, we’ll see that IntelliJ IDEA created additional directories. Project tool window: build directory
  • In the Gradle tool window, click on the toolbar.
  • In the window that opens, enter the gradlew run command. Run anything: gradlew runWe should have the same result as when we ran the application in the IntelliJ IDEA editor. Run tool window: run task outputAlternatively, you can execute the run task under the application node. Gradle tool window: run task
  • Читайте также:  Use response redirect in html

    Источник

    Getting Started

    Everyone has to start somewhere and if you’re new to Gradle, this is where to begin.

    Before you start

    In order to use Gradle effectively, you need to know what it is and understand some of its fundamental concepts. So before you start using Gradle in earnest, we highly recommend you read What is Gradle?.

    Installation

    If all you want to do is run an existing Gradle build, then you don’t need to install Gradle if the build has a Gradle Wrapper, identifiable via the gradlew and/or gradlew.bat files in the root of the build. You just need to make sure your system satisfies Gradle’s prerequisites.

    Android Studio comes with a working installation of Gradle, so you don’t need to install Gradle separately in that case.

    In order to create a new build or add a Wrapper to an existing build, you will need to install Gradle according to these instructions. Note that there may be other ways to install Gradle in addition to those described on that page, since it’s nearly impossible to keep track of all the package managers out there.

    Try Gradle

    Actively using Gradle is a great way to learn about it, so once you’ve installed Gradle, try one of the introductory hands-on tutorials:

    • Building Android apps
    • Building Java applications
    • Building Java libraries
    • Building Groovy applications
    • Building Groovy libraries
    • Building Scala applications
    • Building Scala libraries
    • Building Kotlin JVM applications
    • Building Kotlin JVM libraries
    • Building C++ applications
    • Building C++ libraries
    • Building Swift applications
    • Building Swift libraries
    • Creating build scans

    There are more samples available on the samples pages.

    Command line vs IDEs

    Some folks are hard-core command-line users, while others prefer to never leave the comfort of their IDE. Many people happily use both and Gradle endeavors not to discriminate. Gradle is supported by several major IDEs and everything that can be done from the command line is available to IDEs via the Tooling API.

    Android Studio and IntelliJ IDEA users should consider using Kotlin DSL build scripts for the superior IDE support when editing them.

    Executing Gradle builds

    If you follow any of the tutorials linked above, you will execute a Gradle build. But what do you do if you’re given a Gradle build without any instructions?

    Here are some useful steps to follow:

    1. Determine whether the project has a Gradle wrapper and use it if it’s there — the main IDEs default to using the wrapper when it’s available.
    2. Discover the project structure.

    Either import the build with an IDE or run gradle projects from the command line. If only the root project is listed, it’s a single-project build. Otherwise it’s a multi-project build.

    Читайте также:  Сервер для проверки php

    If you have imported the build into an IDE, you should have access to a view that displays all the available tasks. From the command line, run gradle tasks .

    The help task can display extra information about a task, including which projects contain that task and what options the task supports.

    Many convention-based builds integrate with Gradle’s lifecycle tasks, so use those when you don’t have something more specific you want to do with the build. For example, most builds have clean , check , assemble and build tasks.

    From the command line, just run gradle to execute a particular task. You can learn more about command-line execution in the corresponding user manual chapter. If you’re using an IDE, check its documentation to find out how to run a task.

    Gradle builds often follow standard conventions on project structure and tasks, so if you’re familiar with other builds of the same type — such as Java, Android or native builds — then the file and directory structure of the build should be familiar, as well as many of the tasks and project properties.

    For more specialized builds or those with significant customizations, you should ideally have access to documentation on how to run the build and what build properties you can configure.

    Authoring Gradle builds

    Learning to create and maintain Gradle builds is a process, and one that takes a little time. We recommend that you start with the appropriate core plugins and their conventions for your project, and then gradually incorporate customizations as you learn more about the tool.

    Here are some useful first steps on your journey to mastering Gradle:

    1. Try one or two basic tutorials to see what a Gradle build looks like, particularly the ones that match the type of project you work with (Java, native, Android, etc.).
    2. Make sure you’ve read What is Gradle?
    3. Learn about the fundamental elements of a Gradle build: projects, tasks, and the file API.
    4. If you are building software for the JVM, be sure to read about the specifics of those types of projects in Building Java & JVM projects and Testing in Java & JVM projects.
    5. Familiarize yourself with the core plugins that come packaged with Gradle, as they provide a lot of useful functionality out of the box.
    6. Learn how to author maintainable build scripts and best organize your Gradle projects.

    The user manual contains a lot of other useful information and you can find samples demonstrating various Gradle features on the samples pages.

    Integrating 3rd-party tools with Gradle

    Gradle’s flexibility means that it readily works with other tools, such as those listed on our Gradle & Third-party Tools page.

    There are two main modes of integration:

    • A tool drives Gradle — uses it to extract information about a build and run it — via the Tooling API
    • Gradle invokes or generates information for a tool via the 3rd-party tool’s APIs — this is usually done via plugins and custom task types

    Tools that have existing Java-based APIs are generally straightforward to integrate. You can find many such integrations on Gradle’s plugin portal.

    Источник

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