当前位置:网站首页>Gradle 15 minute introductory tutorial
Gradle 15 minute introductory tutorial
2022-07-24 13:17:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
1-Gradle Introduction
Before reading or practicing the examples in this article , You must first ensure that Gradle Plug in installed to Eclipse in . without , You can view it by clicking on the link below Gradle Installation instructions : – http://www.yiibai.com/gradle/how-install-gradle-windows.html
Objectives of this tutorial :
This is the code structure diagram after the project is completed :
2- establish Gradle project
open Eclipse, Click and select menu File->New->Other And select Gradle Project As shown in the figure below –
Click next (Next) Show some descriptions of the integration , as follows –
Click next (Next), And fill in the name of the project to be created HelloGradle And click Finish (Finish), as follows –
On the first run ,Eclipse Will download Gradle Relevant software or requirements specify local installation Gradle The path of . Please refer to Eclipse+Gradle Integration tutorial .
By default ,
GradleThe software will passEclipseDownload toC:/Users/{username}/.gradle. However, the configuration can be changed to other locations , This configuration is described in the last appendix of this guide .
Gradle The file structure required by the project will be automatically created . Its structure is similar to Maven project .
Be careful , This is a gradle Project defined project structure , What about? , Is it a little familiar with ? – src/main/java – The folder contains all java Source file . – src/test/java – The folder contains all java The test case . – build.gradle – This file contains the scripts used to build the project . – settings.gradle – The file will contain the necessary settings , for example , Lazy relationship between tasks or projects, etc .
3- To configure Gradle
build.gradle File is the file that configures the library to be used in the project . It and Maven In the project pom.xml identical . open build.gradle File configuration library to be used , The code generated by default is as follows :
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'Administrator' at '16-10-30 Afternoon 4:20' with Gradle 3.1
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/3.1/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}Add the following code to the above source code file –
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'Add the following –
Be careful : If
build.gradleFile update ,Eclipse At present, the classpath will not be automatically updated . In the project or inbuild.gradleRight click in the file and select Gradle/Refresh Gradle To update the project .
If you add a new library to build.gradle In a statement ,Gradle They will be downloaded to the local computer .
Next , To create some Java Class to test using the downloaded class library , Here you create a simple CheckNumeric.java class , As shown below –
CheckNumeric.java The code for the class is as follows –
package com.yiibai.hellogradle;
import org.apache.commons.lang3.StringUtils;
public class CheckNumeric {
public static void main(String[] args) {
String text1 = "a12340";
String text2 = "1234";
boolean result1 = StringUtils.isNumeric(text1);
boolean result2 = StringUtils.isNumeric(text2);
System.out.println(text1 + " is a numeric? " + result1);
System.out.println(text2 + " is a numeric? " + result2);
}
} function CheckNumeric Class should get the following results :
You can clearly see the libraries used in the project , Its location on the hard disk is shown in the figure below –
4- explain Gradle How it works
In the above steps, we have created the project , And successfully run . Used by the project StringUtils class , It's a Apache class , Not in JDK Classes in the standard library . Traditionally , You must copy this class library to the project and declare the classpath . however , There is no need to copy and declare the classpath in the traditional way . These class libraries allow Gradle To manage . Now let's see Gradle How it works , This is shown in the following figure –
The figure above shows Gradle The whole process of work , Let's explain step by step . – stay build.gradle It is stated in that the project depends on common-lang3 Library version 3.0. – When using Gradle When the tool refreshes the project ,Gradle It will check whether the specified dependent library has a local repository on the computer . without ,Gradle Download from the Internet repository to the local . – Last ,Gradle Will automatically declare Classpath.
So all you need to do is build.gradle The file declares all the libraries you want to use , These libraries are provided by Gradle Manage yourself .
5- View the local repository
Will you have such a problem : Where is the local repository on my computer ? If you create a project according to all the above routines , Then look at the figure below –
And the above configuration used commons-lang3 The path of the library is C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\org.apache.commons, As shown in the figure below –
6- Gradle Location configuration
By default ,Gradle The software will pass Eclipse Download to C:/Users/{username}/.gradle Directory . But you can change the configuration to its location . For example, you want to change the download directory to D:\worksp\gradle\Downloads, Then you can configure it according to the following operations .
stay Eclipse The menu , open Window -> References Choose a directory D:\worksp\gradle\Downloads, As shown in the figure below –
Right click on the item , And then choose Gradle -> Refresh Gradle Project,Gradle Will be downloaded to the new folder just set . As shown in the figure below –
7- Check on the network Gradle The repository
problem : Where to find information groupId,artifactId And version ? You can go to the website : http://mvnrepository.com , For example, in our example above common-lang3 , Open... Can be found by searching in the website URL:http://mvnrepository.com/artifact/org.apache.commons/commons-lang3 This is shown in the following figure –
According to the version you want , find gradle Information about , As shown below –
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/124965.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

Vscode configuration user code snippet (including deletion method)

Search engine based on boost library

Custom scroll bar

Knowledge sharing | sharing some methods to improve the level of enterprise document management

EAS environment structure directory

Redis (13) -- on master-slave replication of redis

Make a fake! Science has exposed the academic misconduct of nature's heavy papers, which may mislead the world for 16 years

Leetcode's 302 weekly rematch

3. Realize snake and basic game interface

【论文阅读】Mean teachers are better role models
随机推荐
2022.07.15 summer training personal qualifying (10)
Custom scroll bar
Voice recognition based on MATLAB
Introduction of embedded network interface scheme and summary of driver debugging methods
Introduction to the use of thread (2) thread
Question 10: find numbers in an array with rows and columns in order
Windivert:可抓包,修改包
The use of two-dimensional array (including the definition of two-dimensional array, the declaration and initialization of two-dimensional array (dynamic initialization, static initialization), common
【C语言】详细的文件操作相关知识
31. Climb stairs
Two stacks implement one queue
How to draw Bezier curve and spline curve?
汉字风格迁移篇---无监督排版传输
【论文阅读】Mean teachers are better role models
[C language] dynamic memory management
Vscode configuration user code snippet (including deletion method)
The second batch of projects of Shenzhen Metro Line 12 passed the acceptance and is expected to be put into trial operation on July 28
[datasheet] PHY ksz9031 gigabit network chip interpretation
Solution to embedded SD card /u disk read-only problem (fat read-only repair method)
3. Realize snake and basic game interface