zoukankan      html  css  js  c++  java
  • Gradle with Kotlin (二) 项目、Java项目、父子、同级共享代码

    在 settings.gradle 中使用 include 包含项目

    $ tree
    .
    ├── a
    │   └── build.gradle.kts
    ├── build.gradle.kts
    └── settings.gradle
    
    1 directory, 3 files
    
    $ cat build.gradle.kts
    tasks.register("hi") {
            doLast {
                    println("Father")
            }
    }
    
    $ cat settings.gradle.kts
    rootProject.name = "mygradle"
    include("a")
    
    $ cat a/build.gradle.kts
    tasks.register("hi") {
            doLast {
                    println("Son")
            }
    }
    
    $ gradle hi # 在父项目执行hi任务
    Starting a Gradle Daemon (subsequent builds will be faster)
    
    > Task :hi
    Father
    
    > Task :a:hi
    Son
    
    BUILD SUCCESSFUL in 5s
    2 actionable tasks: 2 executed
    
    

    在父项目执行hi任务时,子项目也相继执行了hi任务。即同名任务全部执行。
    如果在父项目中执行只有子项目中存在任务,也是可以的,不会出现找不到任务的错误。

    回到Java

    Gradle天生为Java而生,我们来看看如何使用gradle命令创建一个Gradle项目

    现在得到这样一个目录结构:

    接下来新建一个目录:liba,使用gradle init创建一个library:

    # 查看build.gradle.kts
    githmb@MyServer:~/mygradle/liba$ cat build.gradle.kts
    plugins {
        // Apply the java-library plugin to add support for Java Library
        `java-library`
    }
    
    repositories {
        // Use jcenter for resolving dependencies.
        // You can declare any Maven/Ivy/file repository here.
        jcenter()
    }
    
    dependencies {
        // This dependency is exported to consumers, that is to say found on their compile classpath.
        api("org.apache.commons:commons-math3:3.6.1")
    
        // This dependency is used internally, and not exposed to consumers on their own compile classpath.
        implementation("com.google.guava:guava:29.0-jre")
    
        // Use JUnit test framework
        testImplementation("junit:junit:4.13")
    }
    
    # 源码:src/main/java/com/liba/Library.java
    githmb@MyServer:~/mygradle/liba$ cat src/main/java/com/liba/Library.java
    /*
     * This Java source file was generated by the Gradle 'init' task.
     */
    package com.liba;
    
    public class Library {
        public boolean someLibraryMethod() {
            return true;
        }
    }
    
    # 在父项目中settings.gradle.kts中包含liba项目
    githmb@MyServer:~/mygradle$ cat settings.gradle.kts
    /*
     * This file was generated by the Gradle 'init' task.
     *
     * The settings file is used to specify which projects to include in your build.
     *
     * Detailed information about configuring a multi-project build in Gradle can be found
     * in the user manual at https://docs.gradle.org/6.6/userguide/multi_project_builds.html
     */
    
    rootProject.name = "mygradle"
    include("liba")
    
    # 在父项目中build.gradle.kts中添加liba项目依赖
    dependencies {
        // This dependency is used by the application.
        implementation("com.google.guava:guava:29.0-jre")
        implementation(project("liba"))
    
        // Use JUnit test framework
        testImplementation("junit:junit:4.13")
    }
    
    # 使用liba类
    githmb@MyServer:~/mygradle$ cat src/main/java/com/mygradle/App.java
    /*
     * This Java source file was generated by the Gradle 'init' task.
     */
    package com.mygradle;
    
    public class App {
        public String getGreeting() {
            return "Hello world.";
        }
    
        public static void main(String[] args) {
            System.out.println(new App().getGreeting());
            com.liba.Library liba = new com.liba.Library();
            System.out.println(liba.someLibraryMethod());
        }
    }
    
    # 运行
    githmb@MyServer:~/mygradle$ gradle run
    
    > Task :run
    Hello world.
    true
    
    BUILD SUCCESSFUL in 795ms
    4 actionable tasks: 1 executed, 3 up-to-date
    

    同一级项目的代码共享

    一般来说父级单独向子级获取代码就可以了,但是这会遇到重复依赖的问题,试问如何解决?

    END

  • 相关阅读:
    Study Plan The Twelfth Day
    Study Plan The Fifteenth Day
    Study Plan The Seventeenth Day
    Study Plan The Tenth Day
    Study Plan The Eighth Day
    Study Plan The Eleventh Day
    Study Plan The Sixteenth Day
    Study Plan The Thirteenth Day
    Study Plan The Fourteenth Day
    Study Plan The Ninth Day
  • 原文地址:https://www.cnblogs.com/develon/p/13503019.html
Copyright © 2011-2022 走看看