Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Gradle in java Project Construction

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to build Gradle for java projects. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Relevant introduction

Gradle is a useful build tool, and the reasons for using it are:

Configuration depends on a small amount of code and will not have too much xml as maven does.

Packaging, compilation, testing and release are available, and they are easy to use

You can use custom tasks to accomplish the functions you want.

II. Installation

Download address http://services.gradle.org/distributions/, download the corresponding version you need, what I download here is gradle-4.7-bin.zip. After downloading, extract it to the directory you want, and then set the environment variable:

When viewed in cmd mode, the following message appears to prove that the installation was successful:

Then we can configure gradle's default warehouse address in the environment variable (unlike maven):

3. Use in IED 1. IDEA

Create a Gradle project for web using idea

Then package the project and run it:

Double-click war

After the package is completed, the war file will be found in:

Then put war into the corresponding tomcat directory, which will not be explained here.

2 、 Eclipse

Install the plug-in yourself in eclipse. The plug-in path is:

Https://download.eclipse.org/buildship/updates/e46/releases/2.x/

IV. Problem description

1. Explain build.gradle and settings.gradle

First, a project contains group, name, and version. Settings.gradle is used to manage multiple projects, which contains the name of the project

In build.gradle, apply is a plug-in for applications, such as:

Here we use plug-ins for java and war, and dependencies is used to declare which jar the project depends on.

What is shown here is that we rely on junit's jar during the test compilation phase. These include complile (compile time) runtime (run time) testCompile (test compile time) testRuntime (test run time). Repositories is a warehouse. Gradle will look for jar in the warehouse according to the order from top to bottom.

Here we default to a central repository of maven. From the gradle source code, we can see that the address is like this.

It can be configured here, where mavenLocal () indicates the use of the local maven repository; mavenCentral () uses the maven central repository. Using a fixed address, Aliyun can be used here.

(maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'})

The download speed of the image will be faster, and then you can also use the private server address within the company.

Add a gradle configuration file for spring boot, which can be compared with the build of maven

/ / the script in the buildscript code block first executes buildscript {/ / ext to define the version of the dynamic attribute ext {springBootVersion = '1.5.2.RELEASE'} / / Custom Thymeleaf and Thymeleaf Layout Dialect [' thymeleaf.version'] = '3.0.3.RELEASE' ext [' thymeleaf-layout-dialect.version'] ='2 .2.0' / / Custom Hibernate version ext ['hibernate.version'] =' 5.2.8.Final' / / uses Maven's central warehouse (you can also specify other repositories) repositories {/ / mavenCentral () maven {url 'http://maven.aliyun Com / nexus/content/groups/public/'}} / / dependency dependencies {/ / classpath statement states that when the rest of the script is executed ClassLoader can use these dependencies classpath ("org.springframework.boot:spring-boot-gradle-plugin:$ {springBootVersion}")}} / / use the plug-in apply plugin: 'java'apply plugin:' eclipse'apply plugin: 'org.springframework.boot'// packaged with the type jar And specify the generated packaged file name and version jar {baseName = 'springboot-test' version =' 1.0.0'} / / specify the JDK version of the compiled .java file sourceCompatibility = 1.8max / the central repository of Maven is used by default. Here we use the custom mirror library repositories {/ / mavenCentral () maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}} / / dependency dependencies {/ / this dependency is necessary for compilation and distribution, compile (' org.springframework.boot:spring-boot-starter-web') / / Add Thymeleaf dependency compile ('org.springframework.boot:spring-boot-starter-thymeleaf') / / add Spring Security dependency compile (' org.springframework.boot:spring-boot-starter-security') / / add Spring Boot development tool dependency / / compile ("org.springframework.boot:spring-boot-devtools") / / add Spring Data JPA dependency Compile ('org.springframework.boot:spring-boot-starter-data-jpa') / / add MySQL connection driven dependency compile (' mysql:mysql-connector-java:6.0.5') / / add Thymeleaf Spring Security dependency Consistent with Thymeleaf version 3.x compile ('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE') / / add Apache Commons Lang dependency compile (' org.apache.commons:commons-lang3:3.5') / / this dependency is necessary for compilation testing The default includes compiling product dependencies and compile time according to testCompile ('org.springframework.boot:spring-boot-starter-test')}. Thank you for reading! This is the end of the article on "how to use Gradle in java Project Construction". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report