Series Tutorial 1 Gradle Getting Started Series II: First Java Project

Source: Internet
Author: User

The main content of this tutorial is to explain how to compile and package a simple Java project with Gradle.

There is only one requirement for this Java project: Our build script must create an executable jar file, in other words, we must be able to run our program using the command Java-jar Jarfile.jar . Let's look at how we can meet this demand.

Create a Java Project

We can use the Java plugin to create a Java project, in order to do this, we need to add the following statement to the build.gradle file:

Apply plugin: ' java '

That's it, now that we've created a Java project. The Java plugin adds some new conventions to our build (such as the default project structure), new tasks, and new properties.

Let's take a quick look at the default project structure.

Java Project structure

The default project structure is as follows:

    • The Src/main/java directory contains the source code for the project.
    • The src/main/resources directory contains resources for the project, such as a property file.
    • The Src/test/java directory contains the test classes.
    • The src/test/resources directory contains test resources. All of the files we build will be created in the build directory, which covers the following subdirectories, which we'll mention in this tutorial, as well as some subdirectories we'll be explaining later.
    • The Classes directory contains the compiled . class file.
    • The Libs directory contains build-generated jar or war files.

Add a main class to the build (main classes)

Let's create a simple main class that prints a "Hello world" in this class and System.out it out. The source code for this HelloWorld class is as follows:

Package publicclass  HelloWorld {     publicstatic   void  Main (string[] args) {        System.out.println ("Hello world!" );    }}

HelloWorld class stored in src/main/java/net/petrikainulainen/gradle directory

That's good, however, we also need to compile and package our project, won't we? Let's take a look at the tasks in this Java project.

Tasks in the Java project

The Java plugin has included many tasks in our build , and the tasks we have covered in this tutorial are as follows:

    • The assemble task compiles the source code in the program and packages the build Jar file, which does not perform unit tests.
    • The build task performs a complete project build.
    • The Clean task deletes the build directory.
    • The Compilejava task compiles the source code in the program.

We can also execute the following command to get a complete list of running tasks and their descriptions

Gradle Tasks

This is a good way to get a general view of your project without reading the build script, and if we run this command at the root of the project, we can see the following output:

> Gradle tasks:tasks------------------------------------------------------------All Tasks runnable from root Project------------------------------------------------------------Build Tasks-----------Assemble-assembles the Outputs of this project.build-assembles and tests this project.builddependents-assembles and tests this project and AL L projects that depend in It.buildneeded-assembles and tests this project and all projects it depends On.classes-assem Bles classes ' main '. Clean-deletes the build Directory.jar-assembles a jar archive containing the main Classes.testclas Ses-assembles classes ' test '. Build Setup Tasks-----------------init-initializes a new Gradle build. [Incubating]wrapper-generates Gradle wrapper files. [incubating] Documentation Tasks-------------------javadoc-generates Javadoc API documentation for the main source code. Help Tasks----------dependencies-displays all dependencies declared in root project ' First-java-project '. Dependencyinsight -Displays the insight into a specific dependency in root project ' First-java-project '. Help-displays a Help Messageproj  Ects-displays the sub-projects of the root project ' First-java-project '. Properties-displays the properties of the root project ' First-java-project '. Tasks-displays the tasks runnable from root project ' First-java-project '. Verification Tasks------------------Check-runs all checks.test-runs the unit tests. Rules-----pattern:build<configurationname>: Assembles the artifacts of a configuration. Pattern:upload<configurationname&gt: Assembles and uploads the artifacts belonging to a configuration. Pattern:clean<taskname>: Cleans the output files of a task. To see all tasks and more detail, run with--all. BUILD Successful Total time:2.792 secs


Let's go ahead and talk about how to package our project.

Project Packaging

We can package a project by using two different tasks.
If we execute the command at the command prompt gradle assemble, we can see the following output:

>3.163 secs

If we execute the command gradle buildat the command prompt, we can see the following output:

>3.01 secs

The output of these commands indicates their differences:

    • The assemble task simply executes the set of tasks necessary to package the project.
    • The build task performs the task set necessary to package the project and performs automated tests. Both commands will create a file-java-project.jar file in the build/libs directory. The name of the jar file created by default is determined by this template:[Projectname].jar, in addition, the default name of the project is consistent with the name of the directory in which it is located. So if your project directory name is first-java-project, then the name of the jar file you created is First-java-project.jar.

Now, let's try to run our program using the following command:

Java-jar First-java-project.jar

We can see the following output:

> Java-jar first-java.project.jarNo main manifest attribute, infirst-java-project.jar

The problem is that we did not configure the main class of the jar file in the manifest file, so let's continue to see how to solve the problem.

Configuring the main class of the jar file

The Java plugin adds a jar task to our project, and each Jar object has a manifest attribute, which is an instance of manifest .

We can configure the main class of the generated jar file, using the attributes () method of the Manifest interface . In other words, we can use a map structure that contains key-value pairs to specify the set of properties that are added to the manifest file.

We can specify the entry point of our program by setting the value of the Main-class property. After we make the necessary changes to the build.gradle file, the code is as follows:

Apply plugin: ' java ' jar {    manifest {        ' main-class ': ' Net.petrikainulainen.gradle.HelloWorld '     }}

(Thejavase tutorial provides more information about the manifest file.) )

After we execute the gradle assemble or gradle build command to generate a new jar file, we can run the jar file by executing the following command:

Java-jar First-java-project.jar

When we run the program,System.out will print out the following information:

> Java-jar first-java-project.jarhelloWorld!

That's all we have today, so let's see what we've learned.

Summarize

We've created a simple Java project through Gradle, which teaches us four points:

    • We learned that a Java project can be created using Gradle's Java plugin.
    • We know that the default structure of the Java project is the same as the default structure of the MAVEN project.
    • We know that all the files generated by the build can be found in the build directory.
    • We know we can customize the attributes that are added to the manifest file.

Series Tutorial 1 Gradle Getting Started Series II: First Java Project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.