How to make IntelliJ reference a local project for a dependency? - maven

Working in a multi-module Maven project, call it "app." I need to work on the source of one of the dependencies, call it "lib", and be able to easily test/debug "app" against my changes in "lib."
In Eclipse this is an option for its Maven and Gradle plug-ins, and is obvious since Eclipse doesn't bind the concepts of "workspace" and "project" as tightly as IntelliJ does. When I cloned the repo for "lib", IntelliJ offered to create a new project for it, but how do I force "app" to use the local working copy of "lib" for compilation and runtime?
To put it another way, can IntelliJ basically encapsulate doing build install on "lib" behind the scenes so that "app" uses the updated (snapshot) of it?

The obvious, cleanest choice would be to combine the two projects into a common Maven multi-pom project. If that is something you can't do (perhaps the projects belong to different teams etc.), then I could imagine you could fake it by using symlinks.
Create a wrapper project with just a pom file and two modules. Instead of folders for the modules, use symbolic links to the actual file locations. Obviously the reactor root pom would not be the parent pom.
Now open the wrapper pom as IntelliJ project.
I don't know if this works, but it's worth a try.

Related

building a jar library from a war project using maven?

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.

How to make Spring Tool Suite like a multi-maven-module project?

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.

Do I need the the Maven wrapper for submodules?

If I have a parent Maven project that contains the Maven wrapper files, is there any reason to also including the wrappers in the submodules of this project? Does it really matter? Any possible side effects of doing this I need to look out for? I only plan on running Maven commands from the parent project, but some day I may want to break the submodules up into their own separate projects.
Edit: To be clear, I'm talking about mvnw, mvnw.cmd, and the .mvn directory
The Maven Wrapper is only needed on the root level the project. The intention is to define a the version which will be used for building the project by downloading it automatically.
If you later decide to split up the project this would result in creating separated repositories (Git) and this means you have for each of such projects an other root which mean there it would make sense to have the maven wrapper.

Gradle search local maven or gradle repository

I am using gradle and its local repository is at \.gradle\caches\modules-2\files-2.1 which has all the downloaded jar but not my modules.Is there any specific place I should be searching it for ?
I need it as is in settings.gradle I am having a dependency path specified like :
include ':model'
project (':model').projectDir = new File(settingsDir, './model')
in a new project. Also I don't want to give path in that way because if I have a dependency from multiple projects on this project then mentioning path will be difficult and weird.
How can I make gradle search it from local maven or gradle repositories.
I'm still not sure what is being asked here, and I suspect there is some confusion over how multi-project builds work. So I'm going to attempt to provide a general-purpose answer.
The first question you need to answer is whether you're interested in dependencies between projects that are part of the same build — as in part of a multi-project build — or in separate builds.
Project dependencies (multi-project builds)
Project dependencies are covered in the user manual and only apply to multi-project builds. They use a logical path, using colons as 'path' separators, to specify the location of the target module, like so:
dependencies {
implementation project(":model")
}
At this point, Gradle needs to know where ":model" exists on the file system. There's no getting around that. You have a few options:
Follow the convention of directory structure matching the logical path structure, i.e. have a MyBigProject/model directory containing the ":model" child project
Specify the file path of ":model" in settings.gradle, e.g. with project(":model").projectDir = new File(rootDir, "unusual/path/to/model")
Automate the discovery of projects
The most common approach is the first one. The second is not unusual, particularly if you want to put child projects into a separate directory, like subprojects — something the build of Gradle itself does. I haven't seen the last option done, and I don't know whether it runs into problems.
For the sake of completeness, and at your own risk if you use something like it, here's an example of automatic discovery of projects in the settings.gradle file:
rootDir.eachDir { File dir ->
if ("build.gradle" in dir.listFiles()*.name) {
include dir.name
}
}
This fragment basically looks for directories within the root project folder that have a build.gradle file in them and adds them as child projects. The child projects' directory names become the projects' names.
It's not particularly clever, and you should really use different names for the build files, but it may give you some ideas to work with.
Non-project dependencies
As with project dependencies, Gradle needs to know where to get the corresponding JAR or other form of artifact for a specified module. You normally specify Maven Central or something similar for this, but there are other useful, but less common, options:
Copy a project's artifacts into the local Maven repository — both the Maven Plugin and Maven Publish Plugin support this
Publish to a Maven-compatible repository using a file:// URL rather than an HTTP/HTTPS one, which protects your projects from corruption of Maven Local
Worth noting is that Gradle supports composite builds that allow you to substitute a normal dependency with (effectively) a project dependency from another build. So if model were part of a separate build but you had the source code and build locally, you could make changes and immediately test them in another build's project without going through the whole "install" intermediate step that's common in the Maven world (and Gradle pre-composite-builds).
Hope all this makes sense.

Maven build dependency through pom.xml

For my project, I'm using code from another project found on github. I've included the project as a separate folder in my project. My project uses code from that project, so I want to build that project and include it in my project without really making any changes to that project. So how do I specify in my pom.xml to run the sub-projects pom.xml?
If it helps, here is the repository of the other project that I am using: Soda Java
If you're not planning on changing it, simply download it & build it once using Maven. This will install it into your local repository, and you can simply reference it in your pom without any issue.
If you can find it in an external maven repo somewhere, you wouldn't even have to download & build it.
Only if you're planning on changing it do you need the aggregate project approach.
You create an aggregate project with packaging=pom and a modules element that has one module for the dependency and one module for your project, and you build that.

Resources