Spring boot include transitive dependencies - java

Here is the relations between my projects:
Spring boot project A -> Project B -> Project C
When I run a Maven:install I want to have all the dependencies from Project B AND Project C in my lib folder.
The problem is that for now I just Have the dependencies from Project A...
I just have the spring-boot-maven-plugin as build configuration without any argument...

you should use maven 2.0 or later to have your transitive dependencies imported .
The dependencies in B and C should not have scope "provided" otherwise maven will not import them thinking that the server will provide them

I finally find my mistake...
In the pom of the Project C, I had a dependency in scope system and the system path was like ${basedir}/my/path/librairy.jar. The problem is that Maven wants an absolute path for this librairy when I package Project B. I saw a warning in maven's logs that was saying that. The solution was easy, I change the path for an absolute path and everything went fine! Except for the system librairy that is not packaged. I will put it on our Nexus and it will be fine.

Related

Does Maven resolve dependencies of dependencies?

The current project I'm participating in has such structure:
Project 1
depends on Project2 in pom
checks if object is instanceof SomeClass (mentioned below)
Project 2
depends on spring-boot-starter-web-services in pom
is imported into Project1 as a .jar file through IntelliJ IDEA project settings
has a class, SomeClass that extends org.springframework.ws.client.core.support.WebServiceGatewaySupport
When running mvn clean package, the error below shows up:
cannot access org.springframework.ws.client.core.support.WebServiceGatewaySupport
and the error's line refers to where instanceof SomeClass is written.
Checking the External Libraries in IntelliJ IDEA, seems like libraries related to spring-boot-starter-web-services simply just didn't show up. Adding spring-boot-starter-web-services in project1's pom fixes this but it seems confusing because project2, which project1 depends on, already has that in its pom.
Is this intended behaviour of Maven? Does Maven install dependencies of dependencies? Or is there something I still need to configure to make this work?
It depends: usually maven cares for transitive dependencies.
But if your Project2 is
neither available in a remote Repo (because you didn't mvn deploy it)
nor in the local Repo (because you didn't mvn install)
nor you have it as sibling module in a modularized project
or you didn't update (in the cases of 1 and 2) it for a long time there, maven could try to work with an old version of the poms that might not yet contain that dependencies.
Another source of problems could be if Project 2 declares its dependencies in the maven scope of provided.
And you wrote that Project 1 depends on Project 2 "through IntelliJ IDEA project settings". This is not sufficient for maven to resolve dependencies, you have to declare the dependency in the maven pom.xml!

Install third party jar in local maven before maven resolves its dependency?

I am working on a multi-module maven project and have third party jar which isn't available in central or public repository, I also even don't want to place on public repo. I am providing following plugin directive in my parent pom to install jar in my local maven repository before its dependency is resolved in child project.
Now I provide dependency in child project as;
But I build the project, it successfully adds dependency in local maven repository (places third party jar in .m2 folder) but the at the same time it gives following error. Looks like, it searches this jar file in child projects libs folder as well, while I have already placed it on root (in myproject.parent/libs).
Failed to execute goal org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file (install-
external-non-maven1-jar) on project myProject.core: The specified file 'C:\Users\myuser\git\MyProjectRepository\myproject.parent\myproject.core\libs\be-ixf-java-sdk-jar-with-dependencies.jar' not exists.
I already know scope and systemPath option but I don't want to use it for some reason.
Kindly help in identifying what I am missing ?
The best approach that you could have if your project have a centralized maven repo like nexus setup is to have your third party library also added to the repo. Now , you are having the bin file added to your project and it's not preferable.
If you already have the required jar under your project code in like : libs\*, then why can't you refer the dependency directly in your pom.xml instead of having to install it in your local maven repo and then use it .
<dependency>
<groupId>anything</groupId>
<artifactId>anything</artifactId>
<version>anything</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jar_name.jar</systemPath>
</dependency>
providing the location of the dependency in your project directory should resolve it during build itself.Look at Maven System dependencies.
Since you do not want to change your current setup . Please bear in mind the following about maven pom structure :
Project Aggregation is similar to Project Inheritance. But instead of
specifying the parent POM from the module, it specifies the modules
from the parent POM. By doing so, the parent project now knows its
modules, and if a Maven command is invoked against the parent project,
that Maven command will then be executed to the parent's modules as
well
So the maven-install-plugin execution that you added in main pom.xml might be getting triggered for all your modules because of this reason. You might want to move that to the module that you actually need this lib to be included in.Read maven doc.
Related : force Maven to copy dependencies into target/lib

IntelliJ: How do I override a dependency in SBT with a module imported from local source?

I have 2 projects, Project A and Project B. Project A is a Play2 App that depends on Project B (which is a client library.) Currently Project B is pulled from our artifactory with SBT. I would like to set up IntelliJ such that Project B is pulled from the project source on my computer, rather than from the artifactory that is specified in.
I have added Project B as a module of Project A and added Module B to the dependencies of Module A. I then ordered Module B to be at the very top of the dependency list. The static analysis of the code seems to be working fine, there's not compilation errors showing when I updated Project A's code to use a new method signature that I've updated in Project B. However, when I run the Play App I get a compilation error stating that the method signature is incorrect.
Is there a way to override the module used at runtime for SBT and the Play App?
You can do this via sbt. In your build.sbt, for example:
val localDep = ProjectRef(file("/Users/me/projects/b"), "b")
dependsOn(localDep)
IntelliJ will import this dependency as a module. You should remove the library dependency however, to avoid conflicting classpaths.
Naturally, this makes the project hard to share unless other developers have the project in the same location. In that case, I would create a multi-project build instead, which is usually the best choice for tightly coupled projects with individual resulting artifacts.
Another option is a git project dependency:
val projectDep = ProjectRef(uri("git://github.com/me/b"),"b")

IntelliJ - Replace dependency to a module with dependency to the Maven artefact

When you delete a module from a Maven project in IntelliJ, all references to that module turn into compilation errors. Is there a way to exchange references to the module with references to the artefact in the Maven repo, so that everything still compiles?
Now I can get it to work after a restart of IDEA. Don't know why it didn't work at first. I delete a module, and "make project" gives me compile errors, but Maven > Reimport adds dependencies to Maven repo to replace dependencies to the deleted module. Works fine, and I can live with it being a two-step procedure :)
(My dependencies are already in pom.xml, Harmelodic. Modules are created from Maven pom.xml files.)

IVY - multiproject local build - mimcking "resolve dependencies in workspace"

I have three projects: A, B, C
and build.xml in ant that should compile them.
C depends on B that depends on A
So far I had all dependencies were in eclipse and I had a build.xml in ant with:
<eclipse.incrementalBuild project="A" kind="incremental"/>
<eclipse.incrementalBuild project="B" kind="incremental"/>
<eclipse.incrementalBuild project="C" kind="incremental"/>
Now, I have ivy.xml files - for example:
<ivy>
<ivy module="B"/>
<dependencies>
<dependency name="A">
</dependencies>
</ivy>
Now, all works great when I publish A, B, C in this order to a repository (I use sonatype nexus repository), since the repository is also used for resolving, so the process is:
1. resolve dependencies for A - no such
2. upload currently in workspace A as jar to repository
3. resolve dependencies for B - A is resolved as dependency
4. upload currently in workspace B as jar to repository
5. resolve dependenceies for C - B and A are resolved as dependencies
6. upload currently in workspace C as jar to repository
The way I see it publishing to nexus is for delivering the product
What I need is a simple build.xml to just compile my project - and so I don't want to publish all the time new version.
Instead I am looking for I way to use ivy to
1. compile A
2. and then compile B using already compiled A
2. and then compile C using already compiled B,A
I thought about using a local repository in the file system, in which stages 1-6 are made - producing local jars to be used - however, this is a bit cumbersome since:
1. my resolve will have to contain two resolvers - the file system preceding the nexus repo - which means that on a real publish, I will have to remember to delete the local jars
2. Sounds a bit too much work for something I believe may have a simpler solution.
In essence, I want to mimic eclipse's IVY plugin that can resolve dependencies in workspace and compile projects using other projects in the workspace - but I am looking for the best, most recommended way of doing this.
Thank you
You stated:
I have three projects: A, B, C and build.xml in ant that should
compile them.
That suggests that your project is in fact one large monolithic build.
Instead you need to emulate how Maven does its work. Each module needs to have its own build file and an ivy file that lists that module's dependencies. Each module publishes its own jar artifact into a repository (local or remote) making it accessible to other modules using ivy resolve operations.
The following examples give links to the ivy documentation on multi-module builds using ivy.
ivy simple shared repository
Ivy: Using dynamic revisions
It's not easy to break up a big project, I hope this helps.

Categories