如何将一个外部的Java代码文件夹以相对路径的方式添加到我的Gradle项目中?

huangapple go评论59阅读模式
英文:

How can I add an external folder of java code to my gradle project with a relative path?

问题

我有一个Gradle Java项目,我想引用另一个存储库中存在的Java代码。我不太确定该如何做。

我现有的build.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源文件的文件夹?

英文:

I have a Gradle Java project that I want to reference other java code that exists in another repo. I am not quite sure how to do this.

My existing build.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'
}

What can I add to this file, to have it reference a relative path that points to a folder with java source files in it?

答案1

得分: 1

根据评论中的讨论,以下是操作步骤。

project1 有它自己的 build.gradlesetting.gradle 文件。

project1 文件夹中克隆存储库后,打开 setting.gradle,然后添加以下内容:

include ':project2'

现在,在 project1build.gradle 文件中,您可以将 project2 添加到 dependencies 部分,使其看起来像这样:

dependencies {
    // 使用JUnit测试框架。
    testImplementation 'junit:junit:4.13.2'

    // 此依赖项由应用程序使用。
    implementation 'com.google.guava:guava:30.1.1-jre'
    implementation project(':project2')
}

最终结果:

每次构建 project1 时,project2 也将被构建。

对于 project2 的任何更新将立即影响到 project1,在任何构建操作之后。

阅读官方文档有助于了解多项目构建。

如果您不想在 project1 中克隆 project2,那么可以使用Gradle命令行构建 project2,如下所示:

这将导致在 project2/build/libs/ 目录下创建一个Jar文件,文件名类似于 project2-0.0.1-SNAPSHOT.jar

然后,您可以使用相对路径将其添加到 project1,如下所示:

implementation files('../project2/build/libs/project2-0.0.1-SNAPSHOT.jar')
英文:

Well, According to the discussion in the comments, here is how to do it.

project1 has its own build.gradle and setting.gradle

go setting.gradle and add the following after cloning the repo inside project1 folder:

include ':project2'

Now, In your project1 in build.gradle file You can add project2 to dependencies section so it will look like this :

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')
}

the final result :

Any time you build project1, project2 will be built as well.

Any update for project2 will be effected immediately to project1 after any build operation.

Might be helpful to read official docs about multi project builds.

If you don't want to clone project2 inside project1, Then build project2 with gradle command line gradle build

This will result in a Jar file created at project2/build/libs/project2-0.0.1-SNAPSHOT.jar

Then you can add it to project1 with a relative path to this Jar like this:

implementation files('../project2/build/libs/project2-0.0.1-SNAPSHOT.jar')

huangapple
  • 本文由 发表于 2023年1月9日 07:11:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定