0

please help, I have two spring boot projects with Kotlin + DSL + Gradle, but, I need to import one of them into the other, but, it doesn't work. This is what I have tried:

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

How do you add local .jar file dependency to build.gradle.kt file?

and googling there are many variations of the previous two posts that I have also tried and it just doesn't work. Also, I tried importing the generated ".jar" files, but, in a non-spring boot project (simple console app) and there it works, i can see the classes from this external projects.

Thanks in advance!

Additional data:

Kotlin: v. 1.5.30 IntelliJ IDEA: v. 2021.2.1 (Community Edition) Gradle: 7.1.1

1 Answers1

0

After 2 days wasted in this, I managed to fix the problem, I leave my solution hoping that it will help someone else:

  1. Generate the projects from scratch with Spring Initializer (https://start.spring.io/) with the following characteristics:
  • 1.1. Project: Gradle Project

  • 1.2. Language: Kotlin

  • 1.3. Spring Boot: select the version that suits you best

  • 1.4. Project metadata - It is very important that you do the following:

  • Group: this must be the same in both the parent project and the child projects. Example: com.company

  • Artifact: this should be the name that will differentiate each project (you can put any one as long as it is not assigned to an existing project). Example:

    api (parent)

    -> auth (child)

    -> security (child)

  • Package name: after filling in the above data, your "package name" for each project should look like this:

    com.company.api (parent)

    com.company.auth (child)

    com.company.security (child)

  1. Import the projects normally into IntelliJ IDEA and wait for Gradle to builds the projects.

  2. Very important !!! do not change the names of the classes that Spring Initializer generates, this was one of my biggest problems.

  3. In child projects, always builds the jar with the following command:

    gradle clean build

    Note: the .jar file is generated in / build / libs / and you will find two files, for example for project "auth":

  • auth-1.0.0.jar (large size)

  • auth-1.0.0-plain.jar (small size)

    The first contains all the dependencies so it is larger, the second only contains the classes written by the programmer.

  1. At the root of the parent project ("api"), create the folder "libs" or whatever you want to call it and put all the .jar files that you have previously generated here of child projects.

  2. Import the previous files (contained in the "libs" folder) into the parent project as follows and in the build.gradle.kts file (dependencies section):

dependencies { implementation (fileTree (mapOf ("dir" to "libs", "include" to listOf ("*. jar")))) }

That is all. Incredible that such a simple process causes such a headache.