Gradle study notes-build javaSE project

Ready to work

If you need to use the local gradle, do these local work. If you use the idea plugin directly, you can skip this step

  1. The download address selects the corresponding version to download the compressed package
  2. After decompression, 6.x has only three folders, if you use it, you only need to pay attention to gradle.bat under bin
    Insert picture description here
  3. Configure environment variables and add gradle's bin directory to PATH
  4. gradle -vThe following results are installed normally Insert picture description here

Idea uses Gradle to create Java projects

The specific sequence of steps will be slightly changed by the Idea version, the main steps are as follows

  1. New project-select Gradle-select your own jdk-check Java
    Insert picture description here
    2. Select path-enter module information
    Insert picture description here
    3. Select local Gradle or Idea default Gradle

Introduction to Gradle

table of Contents

Similar to the Maven directory, it is divided into main and test under src, and then into java and resources.
Insert picture description here
I use Idea's default Gradle here, so there is more Gradle.bat file.

Groovy console

1. Tools-GroovyConsole to open the console
Insert picture description here
2. Enter the code-click the run button (the red box in the figure)

The semicolon at the end of the statement can be omitted

Insert picture description here
3. Introduction to grammar

  • Some syntax of variable declaration

Single quotation marks are standard java.lang.String;
if there is no interpolated expression in the string enclosed by double quotation marks, then it is java.lang.String, if there are interpolation expressions, then it is groovy.lang .GString
triple single quotation marks support multiple lines, which are also examples of java.lang.String. Add a backslash\ at the beginning of the first ``' to start text on a new line:

println("hello gradle")

def i=0 //声明变量
println i  //println的括号也可以省略

def list = ['a', 'b'] // 定义列表
list << 'c' //追加元素
println(list.get(2)) //取出列表的元素

def map = ['key1':'value2'] //定义一个map
map.'key3' = 'value3' //直接添加一个键值对
println(map.get('key3')) //获取某个键对应的值
  • Use of closures
//定义并使用无参闭包
def b1 = {
    println("hello")
}
def method1(Closure closure){
    closure()
}
method1(b1)
//定义并使用有参闭包
def b2 = {
    v ->
        println "hello $v"
}
def method2(Closure closure){
    closure("world")
}
method2(b2)

Gradle configuration

It looks more concise than maven. I only load from the central warehouse here. If you want to load from the local warehouse first, you can check this blog

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'
/*指定所使用的仓库,mavenCentral()表示使用中央仓库;本项目所有的jar包都会从中央仓库下载到本地指定项目*/
repositories {
    mavenCentral()
}
/*gradle工程的所有jar包都在dependencies属性内放置
* 每个jar包由三个维度确定:group、name、version
* testCompile表示该jar包在测试的时候起用(每个jar都需要指定作用域)
* */
dependencies {
    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.6.RELEASE'
}

If the directory is not modified, the jar package will be downloaded to the home directory by default (similar to maven's m2 folder)
Insert picture description here

Gradle uses Spring

Compile and run

  1. Dao and impl under main/java
package org.example.dao;
import java.util.List;

public interface AccountDao {
    public List findAll();
}
package org.example.dao.impl;
import org.example.dao.AccountDao;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
    @Override
    public List findAll() {
        System.out.println("列表查询成功");
        return null;
    }
}
  1. Write bean.xml under main/resources
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="accountDao" class="org.example.dao.impl.AccountDaoImpl" />
</beans>

  1. Write test cases in test/java
package org.example.test;
import org.example.dao.AccountDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AccountTest {
    @Test
    public void accountTest(){
        ApplicationContext  ac = new ClassPathXmlApplicationContext("bean.xml");
        AccountDao accountDao = ac.getBean(AccountDao.class);
        accountDao.findAll();
    }
}

The results are as follows
Insert picture description here

If garbled, make sure Idea of coding and Gradle encoding the same

Bale

  1. Gradle-Expand your own project-Tasks-build-jar
    Insert picture description here

  2. The jar package is generated under build/libs
    Insert picture description here

Gradle create web project

Just create a Java project as above,

Guess you like

Origin blog.csdn.net/weixin_44112790/article/details/109799405