We have 3 modules say A,B and C.These 3 modules are cross dependent.To compile A I want B and C.To compile B I want A and C.To compile C we want B and A.So how to build these 3 modules together and we want to get A.jar,B.Jar and C.war.
Use <dependencies> section in pom.xml to add a dependency for one another module.
Create a parent pom for all of your modules. When building parent, maven builds all children (except if you use -pl :mymodule selector on command line). See parent section of pom
You can also add a 4th child module D (without artefact, just a pom module) with A, B, and C dependencies or a subset. When you build D, maven compiles A, B, C with the option -am (also-make) which forces maven to build the selected module and dependencies too.
Note that you must avoid cyclic dependencies into your code or maven will fail to build your project. If you have a cyclic dependency, extract the code need by two modules into one another and adding a dependency in both
What you are explaining is the cyclic dependency. Maven does not support cyclic dependencies and your build will fail if your try to do that.
You can create a multi-module maven project. Move all the common functionality to Module A. Add the dependency of Module A in Module B and Module C's pom.xml <dependencies>...</dependencies> section.
Maven will build the modules in the order you specify in the parent project pom.xml <modules>...</modules> section. In this way you create A.jar, B.jar and C.war by defining the <packaging>...</packaging>as jar or war in their respective module's pom.xml
These links will be helpful - link1 link2
Related
I was given a project structure implementing a board game with GUI that contains two Projects/modules in one IntelliJ Project. Both use Maven to manage dependencies.
Module A (Game-Logic)
Module B (Game-gui)
Module A uses Classes in Module B, and A's pom.xml contains the following dependency:
<dependency>
<groupId>io.bibucket.plt</groupId>
<artifactId>Game-gui</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
But Game-gui is just Module B that is contained locally in the Project itself and the infos (groupId,artifactId etc.) are specified in Module B's pom.xml, so that's what it's pointing to!
Why would we specify a maven dependency to it? I thought the point of maven is to manage dependencies to remote artifacts. Is maven also used to specify local dependencies?
During Maven build I see this error:
The POM for io.bibucket.plt:Game-gui:jar:0.0.1-SNAPSHOT is missing, no dependency information available
What does this error mean? Why is this project using Maven to point to a local Module as an artifact?
You need to use install, package and deploy commands of maven for module B to make it accessible for module A. These commands make a .jar file for module B which can be imported by module A. Models and classes in different artifacts can not be visible for each other unless you define their dependencies in pom file of the other module.
Say I have a multi module maven project that builds components A and B.
Now I want to make a B a separate maven project but B has a dependency on A.
A also has a dependency on C. Because A has to be run in JAVA 7 and B has to be run on JAVA 8
C is a jar, but in C's pom the packaging is bundle.
<groupId>C</groupId>
<artifactId>C</artifactId>
<version>1.0.0</version>
<packaging>bundle</bundle>
After building A, I'm building B.
I'm declaring A as a dependency in the pom with a correct version, and in the local repository, C.jar exist.
But when I try to build B,
mvn clean install
[ERRROR] Failed to execute on project B: Could not resolve dependencies for B.jar:1.0.0: Could not find artifact B:bundle:1.0.0 in <artifactory_url>
This worked perfectly when I had a parent pom and had them as modules
How can I just make the dependency in B, A, to pick up the jar file from the artifactory without using the pom?
I have 2 project A and B.
Each has an interface "I" with its implementation.
In Project A, I want to use the implementation "I" of the project B, and it is not enough to add the dependency of project B to get there.
I ask the necessary configuration in pom.xml of the project A.
Research about writing a 'reactor build' file that would reference both projects and thus build them both; this build would first see which project depends on the other and thus build the dependency first.
Let's say I have a maven project which has some maven modules inside.
My main module depends on the other modules, so when I compile the main module they should be compiled together.
The question is, how to add these modules as dependencies to the main module?
I know if I have a custom lib that I want to use with maven, let's say a utilities project, I have to compile the jar of the project, do a mvn install:install-file on it to install it on the local repository and then add it to the pom.xml.
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module? Because if it should be done like this, there will be a lot of work to do when changing code on the other modules.
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
The question is, how to add these modules as dependencies to the main module?
The same way you add any other dependency to your maven project. By adding group id, artifact id and version to <dependency> element
Do I have to do this with all my modules and add the dependency to the pom.xml on my main module?
If your main module depends on some module A then only the pom of the main module should contain dependency declaration towards module A. You do that for all the dependencies of your module.
I don't know what you mean by "a lot of work when changing the code on other modules". Maven has nothing to do with code changes, it just builds the projects whatever they look like at the given moment...
What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?
Any project that you invoke mvn install on gets built and it's jar copied to local repository. That's all you need to do to get the jar into the repo. This will also put all the dependent jars, if available, into the local repo.
As for best practices for multi module projects:
If your parent project (the one that has modules inside) has <modules> section that lists the modules of your application, and modules are in subdirectories of your parent project, then you simply mvn install (or whatever you want to do) the parent project and that will cause all the modules to be built in order defined by declared dependencies between them. That means that if your main module has dependency on module A, then module A will be built before the main module. This way you can build and install all your modules with one command. On the other hand this approach makes more tight coupling between modules which is not desired in some cases, so it depends on your use case whether it is a good approach or not.
I've recently tried to work with NetBeans and I don't understand how it handles Maven multi-module projects.
My project has fairly complicated modules structure and when we're working on it (we mainly use IntelliJ community edition) we don't want to open all its modules (~50 modules) because it will take hours to load the project, instead we've created a 'workspace' module: a folder with pom.xml that has a packaging type pom and defines modules that I would like to load.
Lets say it defines modules A, B, C.
We have our main method (we don't use any type of container) in module A which is a low-level infrastructure module.
As a runtime dependency we need A, B, C. But A doesn't really depend on B, C, but rather B and C depend on A (in terms of Maven dependencies).
So we've created another module, lets call it runner, where we define all the dependencies. Our workspace pom.xml has module declaration of runner, so in order to run the project from IntelliJ, we use a 'classpath of module runner' while running method main in module A.
Now, How can I achieve the same affect with the latest Netbeans (7.1.2)?
I understand that question is kind of newbie's style, but I struggle with it a lot of time with no luck.
Just struggled with the same issue - from what's listed in the Netbeans wiki it seems that every module represents it's own Netbeans project and everything else would be expressed with project dependencies. See http://wiki.netbeans.org/MavenBestPractices
A project with modules is known as a multimodule, or aggregator project. Modules are projects that this POM lists, and are executed as a group. The Maven projects in NetBeans lists these modules as "Required Projects". So you will only get the required projects list populated for Maven projects with "pom" packaging that have modules defined.