简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM