NotFound
千里之行始于足下
Gradle 入门

Gradle 入门

  • Gradle 6.8

初始化项目

gradle init 可初始化项目:

gradle init --type=basic --dsl=groovy --project-name=example

生成的目录结构如下:

.
├── build.gradle
├── .gitattributes
├── .gitignore
├── .gradle
├── gradle/wrapper/
├── gradlew
├── gradlew.bat
└── settings.gradle

包含 Main 方法的 jar

  1. 新建文件 src/main/java/example/Main.java

    package example;
    
    public class Main {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    
  2. 修改 build.gradle ,把所有依赖一起打包:

    plugins {
        id 'java'
    }
    
    jar {
        manifest {
            attributes "Main-Class": "example.Main"
        }
        from {
            configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
    }
    
  3. 编译、执行

    gradle build
    java -jar build/libs/example.jar
    

War Plugin

  1. 新建文件 src/main/java/example/HelloServlet.java

    package example;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    @WebServlet("/hello")
    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            PrintWriter out = resp.getWriter();
            out.write("Hello World!");
            out.close();
        }
    }
    
  2. 修改 build.gradle

    plugins {
        id 'war'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    }
    
  3. 编译、部署

    gradle build
    cp build/libs/example.war $CATALINA_HOME/webapps
    

    访问 http://localhost:8080/example/hello 即可

Application Plugin

Application Plugin 能创建一个可分发的 ZIP 文件,其中包含了依赖以及启动脚本。

  1. 初始化

    gradle init \
           --dsl=groovy \
           --package=app \
           --project-name=app  \
           --test-framework=junit-jupiter \
           --type=java-application
    
  2. 打包成 zip

    gradle distZip
    
  3. 解压,运行

    unzip app/build/distributions/app.zip -d /tmp/app
    /tmp/app/bin/app
    

添加测试

  1. 新建 src/main/java/example/Hello.java

    package example;
    
    public class Hello {
        public String getGreeting() {
            return "Hello World!";
        }
    }
    
  2. 新建 src/test/java/example/HelloTest.java

    package example;
    
    import org.junit.jupiter.api.Test;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    class HelloTest {
    
        @Test
        void getGreeting() {
            assertEquals(new Hello().getGreeting(), "Hello World?");
        }
    }
    
  3. 修改 build.gradle

    plugins {
        id 'java'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.1'
        testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.7.1'
    }
    
    test {
        useJUnitPlatform()
    }
    
    
  4. 测试

    gradle test
    gradle test --info
    

参考


Last modified on 2021-03-13