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
After 2 days wasted in this, I managed to fix the problem, I leave my solution hoping that it will help someone else:
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)
Import the projects normally into IntelliJ IDEA and wait for Gradle to builds the projects.
Very important !!! do not change the names of the classes that Spring Initializer generates, this was one of my biggest problems.
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.
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.
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.
Related
We have a EAR project which has a WAR project. Maven is used, so pom.xml. As typical of any project, this project also contains a big feature (say Job Scheduling "JBS") among many other features. As it is planned to retire this whole project in the near future, it is discouraged heavily to spend much on working on this project (whether bugs or enhancements).
Therefore, for the sake of running the (JBS) feature as a separate application, the whole EAR project was duplicated (also to save time/cost). As a result, all the Java packages and classes (necessary for JBS project) were duplicated. In this situation, if we update one or more classes in the main project, this (JBS) feature project/application gets outdated (and needs update).
The fact is that this JBS feature project ONLY requires many packages of Java classes (from the main EAR-WAR project), and do not require 99% of the web modules and others. I am removing all the unnecessary things from JBS project. Then I would like to create a JAR library with all the java classes, so JBS project can have a dependency on this JAR.
I do not know if it is a good idea to separate these classes out of the main project (to create another Java project). I would like to continue to have these classes as part of the main project. Then, it will be good, as and when one or more of these classes are changed, a new version of the JAR will be generated (right away). And the JBS project would then make use of this updated JAR.
How can we accomplish this? I understand, through maven, we can do a build/package jar/war/ear on a project of that nature. I am not an expert with maven (and did not learn it systematically).
But, is there a way to create one or more JARs additionally from inside WAR pom.xml? In other words: I mean pom.xml of WAR will create a WAR. In addition to creating a WAR, can maven help create additional JAR? Or can maven create two packages out of one pom.xml?
Or should I create a separate module in the main project with all these packages/classes, and have its own pom.xml to generate the necessary JAR? For this, most probably I need to modify the structure of the main project. I would like to avoid this unless there is no way out.
Can you advice?
It seems like the best thing for you would be to create a multi-module project that both contains the JAR and the other project. This way, you can easily change/build them together, but you create separate artifacts.
I'm trying to create a Spring Boot project with multiple Maven modules. I've used the tutorial at https://spring.io/guides/gs/multi-module/ .
This site recommends a directory tree like this:
parent
application
src, and other subdirectories
pom.xml
library
src, and other subdirectories
pom.xml
pom.xml
I developed this project using Visual Source Code.
Wanting to see the project in another light, I tried to import the project directories into Spring Tool Suite. I'm using a recent one, where you apply the STS plugin to an up-to-date Eclipse installation.
Well, STS doesn't really like this project.
The (File, Open projects from file system) sees the project, but the Finish button doesn't actually do anything.
The (File, Import, General, Existing Projects into Workspace) imports a project, but as a Maven project (no "J" icon). When I try the (Run, Run Configurations) it won't see my project.
How can such a project be made friendly to Spring Tool Suite?
Thanks,
Jerome.
To make multi maven projects what you can do is, simply download two separate maven projects from start.spring.io and then extract them and move both folders to one parent folder and try grabbing the parent folder to Intellij, so it automatically downloads the dependencies and other requirement for the project in which we have two maven projects in one single entity
Eclipse can be a bit confusing with several different Wizards to import projects. Ironically the wizards are supposed to make importing projects easy, and in a sense they do... but... unfortunately picking the right wizard itself can be a bit challenging / confusing. Which wizard you use depends on the type of project.
Since your projects are maven projects, the best wizard to use would be the one for maven projects. You can find it at "File >> Import >> Existing Maven Projects".
So give that a try, point it at the 'parent' folder of your project and you should be presented with a relatively intuitive UI to import all 3 projects and configure them for use in Eclipse.
Want to shift to maven for my old java web project. So created a basic pom.xml using IntelliJ Idea facility. The project is a J2EE web project and also uses axis 2 1.7.7 . All the jars are in 'lib' directory inside the project structure. Is there a easy way to find out the dependency entries which will be required in the newly created 'pom.xml' . What I mean is a a tool which can pickup the jars one by one and suggest the dependency entries
Any help will be highly appreciated here.Thanks
I don't think there is such a tool.
Note that you probably only need to add some of the dependencies because Maven resolves transitive dependencies automatically.
So I would look into the source code, figure out which classes you import and add the appropriate dependencies.
What is the key difference between includeBuild(...) and implementation(project(...)) in the Gradle build system? I cannot really see the usecase difference after reading the documentation:
https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies
https://docs.gradle.org/current/userguide/composite_builds.html#separate_composite
What I am trying to do: Share same codebase for two separate projects: data classes (kotlix.serialization), external database dao, interfaces. It is not a full library but just some code snippets.
How can I connect the two projects in Intellij so that type hinting works?
Composite Build (by using includeBuild) is a way to create a dependency between autonomous Gradle Projects.
Project import, is a way to create a dependency between two modules within a same Gradle Project.
Composite Build is far more powerful and is also meant to be the new way of factorizing gradle configuration between multiple projects that you traditionally do with the buildSrc technique.
I found this "Structuring Large Projects" article to be easier to read than the "Composite Builds" doc.
An excellent sample project that demonstrates the power of Composite Builds can be found in Gradle sample_structuring_software_projects.
Project dependency case
The tree would look like this:
settings.gradle.kts
module1/build.gradle.kts
module2/build.gradle.kts
And you are declaring a dependency in module1/build.gradle.kts like this:
dependencies {
implementation(project("com.domain:module2"))
}
The dependency will be resolved only if both projects are declared as sub-modules of a common root project.
It means you have a root settings.gradle.kts like this:
rootProject.name = "rootProject"
include(":module1")
include(":module2")
Composite build case
The projects do not need to have common "umbrella" root project.
Each project is a totally independent project.
One project can simply declare a dependency on the other project (without even the target project knowing about it).
Tree:
project1/build.gradle.kts
project1/settings.gradle.kts
project2/build.gradle.kts
project2/settings.gradle.kts
In project1/settings.gradle.kts:
rootProject.name = "project1"
includeBuild("../project2") //No more ':' as it is not a module
In project2/settings.gradle.kts:
rootProject.name = "project2"
In project1/build.gradle.kts like this:
dependencies {
implementation("com.domain:project2")
}
I have the same problem. Reading on the first link, the next para says:
Local forks of module dependencies
A module dependency can be substituted by a dependency to a local fork
of the sources of that module, if the module itself is built with
Gradle. This can be done by utilising composite builds. This allows
you, for example, to fix an issue in a library you use in an
application by using, and building, a locally patched version instead
of the published binary version. The details of this are described in
the section on composite builds.
So, it should be implementation project according to me.
P.S. The code completion is working on one of my sub project but not on the other. I am still trying to figure that out
Background
I wrote a spring boot project using gradle and it works fine.
I would like to split up the project into libraries (jars) so that I can reuse them.
These are the requirements
Each of the projects are in sibling folders, each containing its own .git repo.
The multi-build project should be in sibling folder, not in the folder that contains the child projects. It also contains its own .git repo.
Projects that are used as libraries only (jar) have tests annotated with #SpringBootTest and these should be runnable independently.
Projects that are used as class libraries should be provided to other projects as "thin jars" without any dependencies included. The using project needs to add whatever dependencies are accumulated by all the projects it uses.
It should be possible to build each child project independently. For projects that depend on other projects in the suite, this means that building the project will use the output of the other projects (jar) that it needs, and if that output is not ready, gradle will first build those projects.
It should be possible to build all projects from the mult-project parent (Dependencies will be worked out and only necessary builds will occur).
Building the entire suite with the multi-project will create a deployable folder containing the parent boot jar and all its dependencies.
I would like to be able to author the projects, edit and debug all of the above with Intellij.
Minimal, Reproducible Example
I have uploaded a minimal, reproducible example (reprex) here: https://github.com/david-sackstein/multiproject
It contains three folders:
productcommon is a jar with a public class ProductFactory.
productservice is a SpringBoot application with a single #RestController that invokes a public method of ProductFactory in one of its methods. productservice has a project dependcy on productcommon.
multiproject is a gradle project with no code that depends on productcommon and productservice
I setup the build.gradle and settings.gradle in all three projects accordingly.
When I build productservice it fails - evidently because the project import of productcommon fails.
What is missing or incorrect in this example?
Today I came across this excellent post on the subject:
https://reflectoring.io/spring-boot-gradle-multi-module by Tom Hombergs
Tom also provides a great example at:
https://github.com/thombergs/buckpal
I may have some follow on questions on the subject after working with his architecture but for the moment, I am happy with this solution.