Gradle references the level project

 Gradle is a very powerful tool. Many multi-project management teachings on Gradle on the Internet are not written very complicated, or they are wrong.

Suppose we have a main project, lab-springboot, and another parallel sub-project called library1. The main project will call the sub-project's library. The directory structure is as follows and can be placed in any directory:

lab-springboot
library1

This tutorial demonstrates how to use eclipse to create a Gradle project with such a structure.

First, add a SpringBoot project. Of course, eclipse must have Spring related toolchain installed. Select Spring Boot→Spring Starter Project, and select Gradle as Type:

Next, add a sub-project library1, select Gradle→Gradle Project:

After the construction is completed, eclipse will display two projects, which are also equal in the physical save path.

Change Library.java in library1 and change someLibraryMethod to static.

public class Library {
    public static boolean someLibraryMethod() {
        return true;
    }
}

Next is the highlight, modify the settings.gradle in lab-springboot, add the includeFlat command:

rootProject.name = 'lab-springboot'
includeFlat 'library1'

Modify the build.gradle of lab-springboot and add a line of compile:

dependencies {
        compile project('library1')

	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Then use eclipse's refresh gradle project function, expand "Project and External Dependencies" and you will see that library1 has been correctly referenced.

Open LabSpringbootApplication.java to modify the code and test it:

public static void main(String[] args) {
	boolean value = Library.someLibraryMethod();

	SpringApplication.run(LabSpringbootApplication.class, args);
}

Lab-springboot can compile correctly and use library1 code.

Comments

Popular posts from this blog

Getting started with Gradle-Grovvy language foundation

Linux Command Pipeline basic teaching

Gradle configuration Spring multi-project test