Plugin dependency on an internal module - java

I have many modules in my project and let's say two of them are module A and module B. I have specified a dependency on the JAR of module A under the plugin tag of the pom for module B.
POM for module B:
<plugin>
............
............
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>module-A</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
The issue I am facing is that before the build begins i.e. before the JAR for module A is created, pom for module B is getting scanned and I am getting error that says:
"The POM for module A is missing, no dependency information available"
I do not have access to an online repository where I can host JAR for module A. How can I go about doing this locally? How do I ensure that JAR for module A is present locally before this scan takes place?

Usually, Maven determines the correct build order automatically.
I do not know whether plugin dependencies are accounted for as well.
In case of doubt I would move module-A before module-B in the list of modules in the main POM.

Related

Maven child module in parent pom as dependency

I'm trying to create a maven spring-boot project with multiple modules. I have created a parent module with packaging type pom and many children submodules with packaging type jar.
So my parent's pom.xml looks like:
<groupId>Creator</groupId>
<artifactId>DPAI</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>starter</module>
<module>DatabaseApi</module>
...
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
One of submodules: starter contains only starting class annotated with #SpringBootApplicatoion and in its pom.xml there is a section with other child artifacts like:
<parent>
<artifactId>DPAI</artifactId>
<groupId>Creator</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>starter</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>Creator</groupId>
<artifactId>DatabaseApi</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
...
</dependencies>
So I'm trying to do some refactoring and move Main.class and all dependencies to my parent's pom, but it doesn't compile with error with message that my dependencies referencing itself.
In my opinion, the problem is that my parent pom contains section with it's own submodules. Parent of that submoduls is the same pom, where I try to add described dependencies
The parent.pom can't contain any java code, only Maven specifics e.g. See: https://howtodoinjava.com/maven/maven-parent-child-pom-example/#parent-content
Maybe tell us, what you want to achieve.
In a Maven multi module project you usually have a parent Pom (with packaging Pom) and several modules at the same level as you already set your project up.
Build the modules without dependecies on your code first, the the dependent modules: In your parent Pom change the order of the modules to
<modules>
<module>DatabaseApi</module>
<module>starter</module>
...
</modules>
So I'm trying to do some refactoring and move Main.class and all
dependencies to my parent's pom
I dont think this is possible. Your parent pom is actually of type pom, meaning you're not actually supposed to have any java code in it. Its meant to hold the versions of jars used in your child modules. You can relate this to the spring-boot-parent module. When we declare the spring-boot-parent module in a spring boot project, your adding your project as a child of the spring-boot-parent. And the parent will manage the versions of all of your dependencies.
I think the best way forward would be to maintain all your service related code in your spring-boot module. Filters, controllers,etc. The other stuff like your jdbc, integration layers can be maintained in other child modules and then referred to the spring module as jar references similar to your example.
So I'm trying to do some refactoring and move Main.class and all
dependencies to my parent's pom,
I'm not 100% sure if Maven would support something like the following in the parent POM itself:
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>DatabaseApi</artifactId>
<version>${project.version}</version>
</dependency>
...
</dependencies>
But for sure it won't support Java classes in a Module with pom-packaging (such as parent modules or multi-module modules). The compiler:compile goal etc. are not bound to any phase for pom-packaging by default. In other words: Maven does not compile Java classes for pom-modules by default.
My recommendation:
Keep the SpringBootApplication in a Java-based module. For Spring MVC/ WebFlux application I usually create a "-web" module with:
SpringBootApplication
web service controllers
http/ web filters
global configs such as: security, swagger, async
application.yml
...
It's also the module where I configure the Spring Boot Plugin to create an executable JAR.

How to add certain specific libraries in all the projects of the current workspace in Eclipse?

I have created 5 or 6 Java projects in Eclipse by now and will be creating some 20 more projects. I have to add the TestNG library and another library (including some specific jar files) in the project.
Is there any way such that the Eclipse will automatically include both of these libraries at the time of creating every new project?
I don't want to add these libraries on my own by navigating to ADD BUILD PATH -> ADD LIBRARIES.
First, you shouldn't use Eclipse dependency management (add libraries manually to build path using Eclipse) if you're already using Maven.
IMO, you should only rely on Maven dependency mangement.
I suggest you to create a Maven parent project that will handle your dependecies used across multiple projects as follow.
Project structure
- maven-parent-project
|-- project-a
|-- project-b
|-- ...
Define Maven parent project
I suggest you used <dependencyManagement> (documentation) to manage the versions of your dependencies.
pom.xml
<project>
<artifactId>maven-parent-project</artifactId>
<modules>
<module>project-a</module>
<module>project-b</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Here you can define the versions of your dependencies used accross multiple project -->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Here you can define the dependencies used accross multiple project -->
</dependencies>
</project>
Define Maven sub projects
Then, you can define in all your project the maven-parent-project
pom.xml
<project>
<parent>
<artifactId>maven-parent-project</artifactId>
<version>0-SNAPSHOT</version>
<groupId>group</groupId>
</parent>
<artifactId>project-a</artifactId>
<dependencies>
<!-- Here you can define the dependencies specific to the project a -->
</dependencies>
</project>
I hope this will help you.
Study about dependency management in Maven. That will solve your requirements. Read this thread to get the jist.

Maven does not pick up war dependency in pom.xml

I have a multi module Maven project. Parent project and then two other modules, one of which builds the war through its pom.xml.
Parent Project
---- Java Project
---- War Project
I build the war from parent project. There is another external war dependency in War project's pom.xml. Jars in that project are needed to build a specific profile, which is specified in War project's pom.xml. Maven cannot get those jars from war dependency. I ran the following command
mvn install:install <information about war>
to put it in local.m2 repository, but Maven doesn't pick anything from that dependency. I understand that Maven processes war dependencies differently than it does a jar dependency but I am unable to understand as to how will it pick it up? Any help will be appreciated. Thanks.
Edit
This is how I try to include the war dependency
<dependency>
<groupId>com.example</groupId>
<artifactId>my-id-artifact</artifactId>
<version>1.0.0</version>
<type>war</type>
</dependency>
Also, I would like to mention that I use that maven war dependency in <overlay> tag.

Is possible override the version of a dependency in child POM?

I'm working with Maven to management the dependencies of my projects. I have my parent POM and I'm using Dependency Management to avoid write the common dependencies in each project.
Now, I need change the version of one dependency in my child POM.
I have this of my parent POM
<dependencyManagement>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
</dependencyManagement>
My child POM has a reference to the parent POM
<parent>
<groupId>com.myproject</groupId>
<artifactId>root-parent-pom</artifactId>
<version>1.1.0.22</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
And in this child POM I want to override the version of the same dependency that I wrote in the parent POM, something like this.
<dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependency>
I did that, but when I generate my project, in this case is a WAR, the final version of the dependency that is included in my project is the version that is described on the parent POM.
Is possible override the version in child POM?
Maybe. If you specify a distinct version, then this version will override the one in the dependencyManagement element.
But only for this single POM. It doesn't magically distribute to the next POM in the reactor build unless the next POM has this POM as parent. So if you have this setup:
parent
- mod1
- mod2
- war
and you put this into mod1, then the war won't notice since war uses parent as parent POM. The dependency mod1 says "please use 2.6" but that's the same as any other pom which uses 2.1 - there is no reason to prefer that over the other. That's the power of dependencyManagement: You get a single place where you can control the versions of all transitive dependencies.
Try mvn help:effective-pom to see what Maven will use in each part of your build.
To make the WAR pick up the overwritten version, you need to specify it in the parent POM of the WAR or in the WAR's POM itself.
The usual solution is to have a parent POM for all projects which sets the default versions (2.1). Then you have a parent POM per project which inherits from the global parent POM. Here you can set the version to 2.6. Since all modules of the project inherit from it, the per-project parent POM takes precedence.
You need to define that dependency in a war project.

Maven aggregation module serving as a bulk dependency

Is it possible to use an aggregating module (pom that aggregates modules for building purposes) as a dependency that transitively includes its modules as dependencies? Considering it must declare those dependencies that correspond to its submodules, otherwise if you declare it as a dependency, it hasn't own dependencies, so that no transitive deps are included.
I already tried it but I got a cyclic dependency error.
Otherwise I would have to create an extra module (say my_module_deps) that just declares all those dependencies, so I could use it as a dependency that transitively includes its dependencies. I don't like having maven modules that do not have any specific purpose except for being a dependency bulk.
This is the desired state, so I can use it for both module aggregation and a dependency to be used for getting its transitive dependencies :
<project>
<artifactId>aggregationModule</artifactId>
<modules>
<module>a</module>
<module>b</module>
<module>c</module>
</modules>
<dependencies>
<dependency>
<artifactId>a</artifactId>
</dependency>
<dependency>
<artifactId>b</artifactId>
</dependency>
<dependency>
<artifactId>c</artifactId>
</dependency>
</dependencies>
</project>
Do any of the sub-modules have that aggregating project defined as its parent?
If so, this cannot work, since for being parent the project must be build first.
But if the parent itself defines the modules as dependencies at the same time, the modules must be build first, so you created your cyclic dependency error.
You cannot declare a dependency to a project with packaging type "pom". If you do so maven will show the same error as when you declare a dependency to a jar module which does not exists in your local repository and could not be downloaded from your repository list.
Maybe some of the alternatives proposed to this question could help you.

Categories