I have a project structure like:
However, whenever I run mvn clean test -DskipTests from the Project 1 directory, it ends up NOT including the Project 2 module in the Maven reactor even though I've listed it as a dependency in Project 1 as follows:
<dependency>
<groupId>com.main.sub</groupId>
<artifactId>project2-artId</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
What could be happening and why isnt the dependency being recognized?
From https://maven.apache.org/guides/mini/guide-multiple-modules.html; the reactor "Collects all the available modules to build". (modules of the project that is currently being built)
You need to: create a parent project, with packaging pom; add two modules to this project (one for Project1, one for Project2), and submodules for project1.
Then, when you build the parent project, the build order for all modules are decided by the reactor-
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.
I've googled and stack-overflowed everything, but maybe not enough as I'm still not clear how one adds a module of a Maven project as a dependency in another.
So for example let's say we've got:
MajorPager
|___ POM.xml
|___ chuck-commons-core
| |____POM.xml
|____rajni-commons-core
|____POM.xml
Now I want to add chuck-commons-core but not rajni-commons-core. If I do it directly, it can't find the module. So I ran across the following discussions on stack-overflow and my old friend Guggal:
In summary, the below discussions talk of how to create multi-module projects but not really how to include the sub-modules in a top-level POM into another project.
Useful discussions for context
SO Adding a reference to a parent POM project
SO How to add a parent POM as dependency to a different maven project
SO Adding a reference to Parent POM
SO Maven Parent POM vs Modules POM
SO Depend on multi-module aggregator project
SO How to add dependency in Eclipse?
SO How do I configure Maven multi-module-dependency on sub project with different package?
SO How to add a module in parent projects POM as dependency?
SO How to add a dependency in Maven?
Maven dependency mechanism
Maven POM aggregation
Baeldung Multi-module project
Codetab Maven multi-module
Howtodoinjava Maven parent child POM example
Mastertheboss Maven multi-module tutorial
Codetab Multi-module hierarchical project
Sonatype How to share resources across projects
Spring Multi-module project
Concretely, I'd just like a summary from an expert how they add a sub-module of a Parent POM as a dependency to another project.
As of now, I think this is the best solution - to add the following ccodee to the POM.xml of the MajorPager.
Code block 1 incoming
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.teachme.how</groupId>
<artifactId>MajorPager</artifactId>
<version>0.6-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Code block 2 incoming
<dependencies>
...
<dependency>
<groupId>com.teachme.how</groupId>
<artifactId>norris-commons-core</artifactId>
<version>0.6</version>
</dependency>
...
</dependencies>
When you do so enables Maven to find out where the dependency are - and so Maven picks them up with mvn clean -U install or mvn package -U -DskipTests. And I should like to be educated if this isn't the most effective pattern. Or if any of the fields above are optional that Maven doesn't want me to specify because it can derive it on it's own (say for example, version tag?) - please let me know if that's case as well.
I am using maven multi module build like below
<modules>
<module>module1</module>
<module>module2</module>// dependant on module 1
<module>module3</module>// dependant on module 2
<module>warApplication</module>// its a war file dependant on above modules
</modules>
Present build behaviour :-
If i make a change only in module 1, maven will build even module2, module3 source files and then put module1,module2,module3 jar files under warApplication/target/lib.
Is there a configuration/way when i trigger the build, maven verifies if any file was modified under the module, if no skip that module. For war file it will put specific
lib file that got modified. This will save a lot of time.
To check if any file got modified under module or not, maven can store last build time . Before starting build it will check if any of the file got modified after last build timestamp, if yes build it otherwise skip it.
Is there any maven plugin available for this ?
Update :-
My project structure is
TopFolder
.mvn directory -> extensions.xml
build directory->build-Projects-> pom.xml
build directory-> parent -> pom.xml
modules - > module1 -> pom.xml
modules - > module2 -> pom.xml
modules - > module3 -> pom.xml
Here is i am including modules in parent pom
<modules>
<module>../../modules/module1</module>
<module>../../modules/module2</module>
</modules>
. I have included scm tag <developerConnection>scm:svn:https://comp.com/svn/trunk</developerConnection> in both below projects
build directory->build-Projects-> pom.xml
build directory-> parent -> pom.xml
Running build from build directory->build-Projects-> pom.xml.
But the issue is when I change any file any module and run build from build directory-> pom.xml, its not detecting module changes ?
Porbably the best option is using incremental module builder. The first time after making changes in your extension file you need to mvn install and so on you can use mvn -b incremental package which just build only the modules which are changed from the previous build. The github address is https://github.com/khmarbaise/incremental-module-builder
You can use profiles to build what you know has change, as timestamp isn't supported within the pom
See here for more details : Maven Modules optional in pom.xml
I want to use maven project 2 classes in my maven project 1 so that I can use it as a parent class. Can you please tell me how to do this using eclipse and JAVA.
Please write in step by step navigation as I am new in maven.
Thanks in advance
I know its too late But this could help others who are searching for same problem.
suppose you have 2 projects p1 and p2 and you want to use p2 project's classes in p1 project then,
A) Right Click on your p2 maven project(If not maven project then convert it to maven) then
-->Choose Run As-->Click on Maven Install.
(It will make p2 to avaliable for other projects in your local maven repository)
B) Now you can simply add p2 project as dependency in p1 project's pom.xml file like this
<dependency>
<groupId>com.project.p2</groupId>
<artifactId>p2</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
For more details see this
maven install is a hack.
the correct thing to do is create an aggregator pom that includes both project directories something like this:
<packaging>pom</packaging>
<modules>
<module>pathTo/P1</module>
<module>pathTo/P2</module>
</modules>
and then from this new pom directory run
mvn -am compile
the "-am" here is what is important. It will ensure both projects are added to the reactor and are discoverable.
You can add your local project 1 what is in yor eclipse workspace to your local maven repository.
With this command (command line) yor are able to build this project to a pom.
mvn install:install-file -Dfile=c:\whereYoureFileIs-{version}.jar -DgroupId=com.companyName.projectName
-DartifactId=projectName -Dversion={Your version} -Dpackaging=jar
and after you build your jar of Project 1 to your local maven repository you are able to use this jar in your second project as a dependency. After including this jar you can extend the class you need.
If you need more explanation, please let me know.
add a maven dependency like this :
<dependency>
<groupId>groupId of project2</groupId>
<artifactId>artifactId of project2</artifactId>
<version>version of project2</version>
</dependency>
First Create parent java project
Simple java project
Click on project (right-click) -> Configure -> Convert to maven project
Convert to Maven
Convert Java to Maven
Right click on parent project -> new -> Porject...
select maven module
Create another Maven Module child project depend on base project
ex : called (webapp)
Open webapp/pom.xml -> dependencies tap
Click add -> search about your base group id and select -> save
Main Module
-pom.xml
Module1
jar dependency
Module2
jar dependency
I am new to Maven and am wondering what the best practice is for configuring the above setup.
We are upgrading our version of Alfresco (currently all of the above are compile via ANT) and for testing purposes and to make headway on the project, I initially just compiled Module1 and Module2 using their existing ant build scripts and then did a manual install into the Maven repository. I manually added the dependency in my Main Model pom.
Module1 and Module2 need to be compiled and rebuild every time we update Main Module. They are shared with other projects and can change frequently.
What I am working on at this point is the following:
In Main Module pom.xml, I am using maven-antrun-plugin to run the associated ant build scripts for Module1 and Module2. This is working fine and produces the two jar.
Now I would like to have my main module pom.xml file install the two jars into the maven repository.
My dependencies in my pom.xml have an issue when I update the project version since the pom.xml has not been run yet to install the two jars.
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>EformExtractor</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>ca.gc.hc.nhpd</groupId>
<artifactId>SubmissionConsumer</artifactId>
<version>${project.version}</version>
</dependency>
Am I going about this incorrectly? It will work if I run the ant script and install manually and then add the correct version in the pom.xml dependency. We wish this to happen automatically though. We don’t want to have to do this manually for every build we do.
Perhaps you should structure this project with parent and child pom.xml files:
MainModule
|-- pom.xml
|-- ModuleA
| |--pom.xml
|-- ModuleB
|--pom.xml
The parent pom.xml would look something like the one in this example.
The pom.xml for ModuleA and ModuleB could run their Ant scripts, if necessary. Then you should be able to run mvn install at the parent level to install ModuleA and ModuleB to your local Maven repository.