Gradle guide to java

Posted by dammitjanet on Sun, 08 Mar 2020 09:23:53 +0100

In fact, I have always been used to build projects with maven, but yesterday I downloaded the source code of spring, and others have switched to gradle, and then I will simply learn the gradle, a typical application driven learning, and learn what new content I need

The short tutorial is as follows:

1. First, download a gradle and unzip it to the computer

2. Configure the Gradle environment variable to path

3. Open cmd for installation verification, and use the command gradle-v to display the following contents. The installation is successful

 

The installation of this gradle is complete

Because the use of gradle is in the groovy language, so let's simply learn about groovy here. Idea has a very simple tool for learning

There is a Groovy console, you can easily write

println("hello world")
println("hello groovy");
//This is an annotation.
/**
 * This is also a comment. You don't need brackets or semicolons at the end of the statement
 */
println "hello chengcheng, i miss you"
println("hello chengcheng, miss you very much")

//Defining variables
def i=18
def i2="chengcheng"
println i
println i2


//Definition set
def myList=[1,2,3,5,6]
println(myList)
//Add a value
myList.add(0) //Mylist < 0 is also a way to add
println(myList.get(5))

//Define map
def myMap=['name':'chengcheng',"age":"18","sex":1]
//Set value
myMap.phone=120
println(myMap.sex)
println(myMap.get("name"))
println(myMap.get("phone"))

println("~~~~~~~~~~~~~~~~~~~~~~~~")

//groovy closure, which can be regarded as a piece of code
def m1={
    println("hello chengcheng")
}
//Define a method in which parameters of closure type are required
def method1(Closure closure){
    closure()
}

//Calling method
method1(m1)


//Transfer parameters
def m2={
    v->println("hello ${v}")
}

//Setting parameters
def method2(Closure closure){
    closure("Variables defined,CC")
}

method2(m2)

It's very simple grammar

 

Insert a sentence

Set up gradle to use maven's warehouse to set the environment variables of window

The variable name is:

GRADLE_USER_HOME

The variable value is the folder address of maven

 

Here is the use of gradle

 

Using idea gradle to create a project, there are two areas that need to be modified,

1. User auto import

2.use local gradle distribution

It is not recommended to use the default gradle. Because every time you download new ones from the Internet,... The domestic network is not good, and the egg hurts

 

The second part is to fill in two IDS, which can be completed

 

There are two project configuration files for gradle: build.gradle and setting.gradle

setting.gradle is used to store the project name,

The main configuration is in build.gradle

//Plug in is java language
plugins {
    id 'java'
}

//Organization name and version number
group 'com.onyx'
version '1.0-SNAPSHOT'

//Source version
sourceCompatibility = 1.8

//Warehouse
repositories {
    //Remember to configure the grade ﹣ user ﹣ home environment variable first, use the local warehouse first, and then go to the warehouse in the intermediate warehouse after it is not found
    mavenLocal()
    //Central repository
    mavenCentral()
}

//All dependencies
dependencies {
    //The jar package scope depends on the jar
    testCompile group: 'junit', name: 'junit', version: '4.12'
    // https://mvnrepository.com/artifact/org.springframework/spring-context
    compile group: 'org.springframework', name: 'spring-context', version: '5.2.3.RELEASE'

}

 

The directory of the project is the same as maven, which is src-main-java hierarchy

Make the current project a jar package

The location of the jar you just typed is:

 

 

Let's talk about the usage skills. Configure the current project to use Alibaba cloud's maven warehouse

Add the following before dependencies

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

Finally, I wish you all good health, do not get sick, safe through the epidemic

March 8, 2020 15:55:10

Published 119 original articles, won praise 117, visited 260000+
Private letter follow

Topics: Gradle Maven Spring Java