[Series 1] Gradle Introduction Series 2: The First Java Project

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

This Java project has only one requirement: our build script must create an executable Jar file, in other words, we must be able to use the command java -jar jarfile.jar  to run our program. Let's take a look at how to meet this need.

Create a Java project

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

apply plugin: 'java'

就是这样,现在我们已经创建了一个Java项目。Java插件会在我们的构建中添加一些新的约定(如默认的项目结构),新的任务,和新的属性。

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 the project's resources (such as properties files).
  • The src/test/java directory contains test classes.
  • The src/test/resources directory contains test resources. All files generated by our build will be created in the build directory, which contains the following subdirectories, which we will mention in this tutorial, and some subdirectories that we will explain later.
  • The classes directory contains compiled .class files.
  • The libs directory contains the jar or war files generated by the build .

 

Add a main class to the build

Let's create a simple main class that will print a "Hello world" and then System.out . The source code of this HelloWorld class is as follows:

package net.petrikainulainen.gradle;
 
public class HelloWorld {
 
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

HelloWorld类存放在src/main/java/net/petrikainulainen/gradle目录

That's fine, however, we still need to compile and package our project, don't we? Let's first look at the tasks in this Java project.

Tasks in a Java Project

The Java plugin adds a lot of tasks to our build, the tasks we cover in this tutorial are as follows:

  • The assemble task will compile the source code in the program and package it into a Jar file. This task does not perform unit testing.
  • 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 runnable tasks and their descriptions

gradle tasks

这是一个很好的方式,不需要阅读构建脚本,就能对你的项目进行大致的浏览,如果我们在项目根目录下运行这个命令,我们可以看到以下输出:

> 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 all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - 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 message
projects - Displays the sub-projects of root project 'first-java-project'.
properties - Displays the properties of 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>: 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 move on, let's talk about how to package our project.

Project packaging

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

> gradle assemble
:compileJava
:processResources
:classes
:jar
:assemble
 
BUILD SUCCESSFUL
 
Total time: 3.163 secs

如果我们在命令提示符中执行命令gradle build,我们可以看到以下输出:

> gradle build
:compileJava
:processResources
:classes
:jar
:assemble
:compileTestJava
:processTestResources
:testClasses
:test
:check
:build
 
BUILD SUCCESSFUL
 
Total time: 3.01 secs

这些命令的输出表明了它们的区别:

  • The assemble task only executes the set of tasks necessary to package the project.
  • The build task executes the set of tasks necessary to package the project and execute 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 the same as the directory name where it is located. So if your project directory name is first-java-project , then the created Jar file name is first-java-project.jar.

Now, we try to run our program with the following command:

java -jar first-java-project.jar

我们可以看到以下输出:

> java -jar first-java.project.jar
No main manifest attribute, in first-java-project.jar

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

 

Configure 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 containing key-value pairs to specify the set of properties 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 {
        attributes 'Main-Class': 'net.petrikainulainen.gradle.HelloWorld'
    }
}

JavaSE教程提供了关于manifest文件的更多信息。)

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

java -jar first-java-project.jar

当我们运行程序时,System.out会打印出以下信息:

> java -jar first-java-project.jar
Hello World!

这就是我们今天所有的内容,我们看一下我们学到了什么。

Summarize

We've created a simple Java project with Gradle, and this tutorial has taught us four things:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326423791&siteId=291194637