如何使用相对路径将Java代码的外部文件夹添加到我的gradle项目?

jdg4fx2g  于 2023-01-09  发布在  Java
关注(0)|答案(1)|浏览(164)

我有一个Gradle Java项目,我想引用另一个存储库中存在的其他Java代码。我不太确定如何执行此操作。
我的现有版本.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13.2'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:30.1.1-jre'

    // implementation project(':project1')
    // implementation files('../../project1/lib/build/libs/lib.jar')
}

application {
    // Define the main class for the application.
    mainClass = 'project2.App'
}

我可以在这个文件中添加什么,使它引用一个相对路径,该路径指向一个包含java源文件的文件夹?

piztneat

piztneat1#

好吧,根据评论中的讨论,下面是如何做到这一点。
project1有自己的build.gradlesetting.gradle
转到setting.gradle并在project1文件夹中克隆repo后添加以下内容:

include ':project2'

现在,在您的project1 in build.gradle文件中您可以将project2添加到dependencies部分,因此它看起来如下所示:

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13.2'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:30.1.1-jre'
    implementation project(':project2')
}

最终结果是:
每当您构建project1时,也会构建project2
project2的任何更新将在任何生成操作后立即影响到project1
阅读关于多项目构建的官方docs可能会有所帮助。

相关问题