天天看點

Spring Boot 項目建構 之 使用 Gradle 建構 Java 項目(Building Java Projects with Gradle)Building Java Projects with Gradle

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_what_you_ll_build">What you’ll build</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_what_you_ll_need">What you’ll need</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_how_to_complete_this_guide">How to complete this guide</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#scratch">Set up the project</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#initial">Install Gradle</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_find_out_what_gradle_can_do">Find out what Gradle can do</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_build_java_code">Build Java code</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_declare_dependencies">Declare dependencies</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_build_your_project_with_gradle_wrapper">Build your project with Gradle Wrapper</a>

<a target="_blank" href="http://spring.io/guides/gs/gradle/#_summary">Summary</a>

<a target="_blank" href="http://spring.io/guides?filter=gradle">gradle</a>

<a target="_blank" href="http://spring.io/understanding/Git">Git</a>

GETTING STARTED

This guide walks you through using Gradle to build a simple Java project.

You’ll create a simple app and then build it using Gradle.

About 15 minutes

A favorite text editor or IDE

To skip the basics, do the following:

cd into <code>gs-gradle/initial</code>

When you’re finished, you can check your results against the code in <code>gs-gradle/complete</code>.

First you set up a Java project for Gradle to build. To keep the focus on Gradle, make the project as simple as possible for now.

In a project directory of your choosing, create the following subdirectory structure; for example, with <code>mkdir -p src/main/java/hello</code> on *nix systems:

Within the <code>src/main/java/hello</code> directory, you can create any Java classes you want. For simplicity’s sake and for consistency with the rest of this guide, Spring recommends that you create two classes: <code>HelloWorld.java</code> and <code>Greeter.java</code>.

<code>src/main/java/hello/HelloWorld.java</code>

<code>src/main/java/hello/Greeter.java</code>

Now that you have a project that you can build with Gradle, you can install Gradle.

Unzip the file to your computer, and add the bin folder to your path.

To test the Gradle installation, run Gradle from the command-line:

If all goes well, you see a welcome message:

You now have Gradle installed.

Now that Gradle is installed, see what it can do. Before you even create a build.gradle file for the project, you can ask it what tasks are available:

You should see a list of available tasks. Assuming you run Gradle in a folder that doesn’t already have a build.gradle file, you’ll see some very elementary tasks such as this:

Even though these tasks are available, they don’t offer much value without a project build configuration. As you flesh out the <code>build.gradle</code> file, some tasks will be more useful. The list of tasks will grow as you add plugins to <code>build.gradle</code>, so you’ll occasionally want to run tasksagain to see what tasks are available.

Speaking of adding plugins, next you add a plugin that enables basic Java build functionality.

Starting simple, create a very basic <code>build.gradle</code> file that has only one line in it:

This single line in the build configuration brings a significant amount of power. Run gradle tasksagain, and you see new tasks added to the list, including tasks for building the project, creating JavaDoc, and running tests.

You’ll use the gradle build task frequently. This task compiles, tests, and assembles the code into a JAR file. You can run it like this:

After a few seconds, "BUILD SUCCESSFUL" indicates that the build has completed.

To see the results of the build effort, take a look in the build folder. Therein you’ll find several directories, including these three notable folders:

classes. The project’s compiled .class files.

reports. Reports produced by the build (such as test reports).

libs. Assembled project libraries (usually JAR and/or WAR files).

The classes folder has .class files that are generated from compiling the Java code. Specifically, you should find HelloWorld.class and Greeter.class.

At this point, the project doesn’t have any library dependencies, so there’s nothing in thedependency_cache folder.

The reports folder should contain a report of running unit tests on the project. Because the project doesn’t yet have any unit tests, that report will be uninteresting.

The libs folder should contain a JAR file that is named after the project’s folder. Further down, you’ll see how you can specify the name of the JAR and its version.

The simple Hello World sample is completely self-contained and does not depend on any additional libraries. Most applications, however, depend on external libraries to handle common and/or complex functionality.

For example, suppose that in addition to saying "Hello World!", you want the application to print the current date and time. You could use the date and time facilities in the native Java libraries, but you can make things more interesting by using the Joda Time libraries.

First, change HelloWorld.java to look like this:

Here <code>HelloWorld</code> uses Joda Time’s <code>LocalTime</code> class to get and print the current time.

If you ran <code>gradle build</code> to build the project now, the build would fail because you have not declared Joda Time as a compile dependency in the build.

For starters, you need to add a source for 3rd party libraries.

The <code>repositories</code> block indicates that the build should resolve its dependencies from the Maven Central repository. Gradle leans heavily on many conventions and facilities established by the Maven build tool, including the option of using Maven Central as a source of library dependencies.

Now that we’re ready for 3rd party libraries, let’s declare some.

With the <code>dependencies</code> block, you declare a single dependency for Joda Time. Specifically, you’re asking for (reading right to left) version 2.2 of the joda-time library, in the joda-time group.

Another thing to note about this dependency is that it is a <code>compile</code> dependency, indicating that it should be available during compile-time (and if you were building a WAR file, included in the /WEB-INF/libs folder of the WAR). Other notable types of dependencies include:

<code>providedCompile</code>. Required dependencies for compiling the project code, but that will be provided at runtime by a container running the code (for example, the Java Servlet API).

<code>testCompile</code>. Dependencies used for compiling and running tests, but not required for building or running the project’s runtime code.

Finally, let’s specify the name for our JAR artifact.

The <code>jar</code> block specifies how the JAR file will be named. In this case, it will render<code>gs-gradle-0.1.0.jar</code>.

Now if you run <code>gradle build</code>, Gradle should resolve the Joda Time dependency from the Maven Central repository and the build will succeed.

The Gradle Wrapper is the preferred way of starting a Gradle build. It consists of a batch script for Windows and a shell script for OS X and Linux. These scripts allow you to run a Gradle build without requiring that Gradle be installed on your system. To make this possible, add the following block to the bottom of your <code>build.gradle</code>.

Run the following command to download and initialize the wrapper scripts:

After this task completes, you will notice a few new files. The two scripts are in the root of the folder, while the wrapper jar and properties files have been added to a new <code>gradle/wrapper</code>folder.

The Gradle Wrapper is now available for building your project. Add it to your version control system, and everyone that clones your project can build it just the same. It can be used in the exact same way as an installed version of Gradle. Run the wrapper script to perform the build task, just like you did previously:

The first time you run the wrapper for a specified version of Gradle, it downloads and caches the Gradle binaries for that version. The Gradle Wrapper files are designed to be committed to source control so that anyone can build the project without having to first install and configure a specific version of Gradle.

At this stage, you will have built your code. You can see the results here:

Included are the two expected class files for <code>Greeter</code> and <code>HelloWorld</code>, as well as a JAR file. Take a quick peek:

The class files are bundled up. It’s important to note, that even though you declared joda-time as a dependency, the library isn’t included here. And the JAR file isn’t runnable either.

To make this code runnable, we can use gradle’s <code>application</code> plugin. Add this to your<code>build.gradle</code> file.

Then you can run the app!

To wrap things up for this guide, here is the completed <code>build.gradle</code> file:

<code>build.gradle</code>

 There are many start/end comments embedded here. This makes it possible to extract bits of the build file into this guide for the detailed explanations above. You don’t need them in your production build file.

Congratulations! You have now created a simple yet effective Gradle build file for building Java projects.