How to break mvn project into multimodules? - java

Is there any way to break a standard Spring Boot Maven project into multi modules?
Let's say i have a package for models, one for controllers, one for dao and another one for ui. Can i split them to separate modules somehow?
I am using IntelliJ Ultimate if matters.

yes it is possible.
create a directory per each module in your project, and add for every one of the separate POM.
At the project root level at the main POM(reactor) you will reference each one of them.
Example for reactor :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>pom</packaging>
<name>...</name>
<modules>
<module>sub-module-a</module>
<module>sub-module-b</module>
<module>sub-module-c</module>
</modules>
</project>
And when you will build your project, you will do it from the reactor.
for more details you can check maven documentation:
https://books.sonatype.com/mvnex-book/reference/multimodule.html

Related

Properties in parent definition are prohibited in the intellij maven on my mac osx

I have a question about my maven configuration on my MAC Intellij. I get an error that ${a-web.version} "properties in parent definition are prohibited" within parent tag in child POM.xml and that properties is defined in parent POM.xml.
But this same configuration works well on my Windows Intellij.
Below is my parent POM.xml.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>***</groupId>
<artifactId>a-web</artifactId>
<packaging>pom</packaging>
<version>${a-web.version}</version>
<name>a-web project</name>
<url>http://***</url>
<properties>
<a-web.version>1.0.0</a-web.version>
</properties>
<modules>
<module>a-core</module>
<module>a-web</module>
</modules>
</project>
And this is my child POM.xml that configure a parent block.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>a-web</artifactId>
<groupId>***</groupId>
<version>${a-web.version}</version>
<relativePath>../</relativePath>
</parent>
<artifactId>aweb-web</artifactId>
<version>${a-web.version}</version>
<packaging>war</packaging>
<name>aweb-web</name>
</project>
I've tried many methods from stackoverflow, but no one can fix it. I want to know why this Maven config have different behaviour in Mac and windows and how to fix it.
I have run into a very similar situation when the project started using parent version properties in the commit I have just pulled. The project would not build and felt "ill-imported". An attempt to show effective pom failed with no details presented by IDEA.
As #khmarbaise pointed out in the comments[1], there are some restrictions for properties usage in maven <parent> declarations - the most notable is maven 3.5+ is required. The IDEA might not be using maven new enough (the bundled one in the latest IDEA I have installed was too old) so I had to switch to local install using Settings > Build, Execution, Deployment > Build Tools > Maven > Maven Home Directory. Once pointed to newer version, it did not start to work immediately - some of the restart/rebuild/reimport routines caused the new setting to be effective and the project to successfully import and build again.
Despite the maven version requirement was enforced in pom, it have not caused IDEA to complain the maven version it uses is old. The lack of any details that IDEA fails to inspect the project with the maven it uses did not helped either. Now when everything else works, it still says "Properties in parent definition are prohibited" in pom files.
[1] https://maven.apache.org/maven-ci-friendly.html
Setting property revision in parent pom.xml and then inherit it into children, fixed the issue for me.
<groupId>com.java</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<name>Test</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>backend</module>
<module>frontend</module>
</modules>
<properties>
<revision>1.0.0-SNAPSHOT</revision>
</properties>
In the child's pom.xml:
<parent>
<artifactId>com.java</artifactId>
<groupId>test</groupId>
<version>${revision}</version>
</parent>
<artifactId>backend</artifactId>
I use Maven 3.6.1
Work for me in Idea 18.2:
1) Don't use embeded Maven. Install Maven from official site! It's help me (^_^)
2) You can try to add groupId in child pom, put <groupId>com.iqiyi.lego.web</groupId> before line <artifactId>legoweb-web</artifactId>.
Try this code:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>lego-web</artifactId>
<groupId>com.iqiyi.lego.web</groupId>
<version>${lego-web.version}</version>
<relativePath>../</relativePath>
</parent>
**<groupId>com.iqiyi.lego.web</groupId>**
<artifactId>legoweb-web</artifactId>
<version>${lego-web.version}</version>
<packaging>war</packaging>
<name>legoweb-web</name>
</project>
because your maven version is lower than 3.5.2, try to use version that >=3.5.2,when the ide intellij idea bundled maven version is lower than 3.5.2,try to download zip set to use your own maven instead of the bundled one.
by the way,although maven lower than 3.5.2 show error in intellij idea,but it works ok,you can "mvn package" success.
I think it's because when you push the compiled jar to central maven or private maven such as nexus, you can find the version in META-INF/maven/xxxxx/pom.xml of sub project is
<version>${lego-web.version}</version>
not real value <version>1.0.0</version>.so when you want to release a single jar, you cannot get the true version
It is the IntelliJ IDEA bug.
At least in the IntelliJ IDEA 2019.2 (Ultimate Edition)
Build #IU-192.5728.98, built on July 23, 2019

Maven build Automation for a web Application

I have three modules of a project, getting developed as two seperate jars and one WAR. WAR is referring to two other jar. Please see the details below-
1.processDAO-this is a java Project producing a jar(processDAO.jar)
2.ProcessModel-this is a java project producing a jar(processModel.jar). And in pom.xml it is referring (processDAO.jar)
3.ProcessWebApp-this is Main web app project as War producing (processWebApp.WAR). Now this is using (processModel.jar)
Now currently after each development when I need to build the WAR file I first build -(processDAO.jar)
then 2nd- processModel.jar
and in last I build ProcessWebApp.WAR
So each time I have to follow this sequential build process. Now, I want whenever I go to build my ProcessWebApp.WAR other dependency gets build automatically in same sequential manner(1->2->3).
Any advice on this will be very helpful.
Environment I am using Eclipse which have mavenplugin. I just right click on every project and do RunAs 'Maven install'.
1.processDAO.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>processDAO</groupId>
<artifactId>processDAO</artifactId>
<version>2.0</version>
<packaging>jar</packaging>
2.ProcessModel.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ProcessModel</groupId>
<artifactId>ProcessModel</artifactId>
<version>2.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>processDAO</groupId>
<artifactId>processDAO</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
3.ProcessWebApp.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ProcessWebApp</groupId>
<artifactId>ProcessWebApp</artifactId>
<version>2.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>ProcessModel</groupId>
<artifactId>ProcessModel</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Listen there several issues with the way you have created your pom or what you want to achieve.
A minor comment on the existing samples is to fix the <groupid> element usually it is something of com.xxx.my.company and is not identical with the artifact id.
Now from what I understand it seems that you are still changing the code on the dependent modules, so that means you are releasing SNAPSHOT versions (otherwise you wont need to check the compatibility of your libs every time right?). See this question on using xxx-SNAPSHOT here
Also check the use of the -U flag mvn clean install -U . See mvn -help.
-U,--update-snapshots Forces a check for missing
releases and updated snapshots on
remote repositories
This is usually achieved by use of a parent project POM, the only purpose of which is to build the modules that it depends on. When creating the POM, specify the packaging type as 'POM' and then specify your WAR and JARs as module elements. There's a pretty good example and description of this on Sonatype's site here.

Build multi-module project into a single jar with Maven2

I have read this:
maven: multi-module project assembly into single jar
For the accepted answer of that question, my maven version is 2.2.1 and shade need maven 3.
My parent pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>myGroup</groupId>
<artifactId>x-all</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>x-framework</module>
<module>x-adaptor</module>
<module>x-plugin</module>
<module>x-utils</module>
<module>x-offline</module>
<module>x-protocols</module>
</modules>
...
I just want to build a x-all-1.0.0-jar-with-dependencies.jar that includes
every sub-module class
every sub-module dependency class (need resolve dependency conflict automatically)
some resource file(in x-all/src/main/resources)
How do I build that?
Add another module, for example, call it x-package. let x-package depends every module in that project. And we just run mvn package with a simple assembly plugin configuration(jar-with-dependence), we will get the wanted jar in x-package's target directory.

MavenError: Failed to execute goal on project: Could not resolve dependencies In Maven Multimodule project

I am trying to create a maven multi-module project. the project is created successfully but when I am trying to use one module as a dependency of another module, it throws an exception. When I create a module using eclipse, I was selecting packaging as a jar, but when the module is created, the packaging tag was not mention in child pom.xml and I manually insert the packaging tag as a jar.
following is my parent pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
-------------------------
<modules>
<module>empirecl-web</module>
<module>empirecl-dao</module>
<module>empirecl-service</module>
<module>empirecl-api</module>
</modules>
Dao Child Module:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>empirecl-dao</artifactId>
<packaging>jar</packaging>
<name>empirecl-dao</name>
------------------------
Service Child Module:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.netsol</groupId>
<artifactId>empirecl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>empirecl-service</artifactId>
<packaging>jar</packaging>
<name>empirecl-service</name>
<dependencies>
<dependency>
<groupId>com.netsol</groupId>
<artifactId>empirecl-dao</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
------------------------------------------
The Dao module maven clean and install successfully, but when i trying to use service module, it will generate an following exception:
[ERROR] Failed to execute goal on project empirecl-service: Could not resolve dependencies for project com.netsol:empirecl-service:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT: Could not find artifact com.netsol:empirecl:pom:0.0.1-SNAPSHOT -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project empirecl-service: Could not resolve dependencies for project com.netsol:empirecl-service:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.netsol:empirecl-dao:jar:0.0.1-SNAPSHOT
I am trying the find to solution from web, but still the solution is not found. In eclipse when i open the Dependency Hierarchy of service module, it shown the DAO module as a folder not jar. below is the screen shot of Dependency Hierarchy of service module.
In case anybody comes back to this, I think the problem here was failing to install the parent pom first, which all these submodules depend on, so the Maven Reactor can't collect the necessary dependencies to build the submodule.
So from the root directory (here D:\luna_workspace\empire_club\empirecl) it probably just needs a:
mvn clean install
(Aside: <relativePath>../pom.xml</relativePath> is not really necessary as it's the default value).
In my case I forgot it was packaging conflict jar vs pom. I forgot to write
<packaging>pom</packaging>
In every child pom.xml file
My solution was to insert <packaging>pom</packaging> between artifactId and version
<groupId>com.onlinechat</groupId>
<artifactId>chat-online</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>server</module>
<module>client</module>
<module>network</module>
</modules>
This what worked for me -
Go to Java build path --> Order and Export(check JRE system libraries and maven dependencies) in my case these 2 were unchecked.
My solution:
remove all projects in current workspace
import all again
maven update project (Alt + F5) -> Select All and check Force Update of Snapshots/Releases
maven build (Ctrl + B) until there is nothing to build
It worked for me after the second try.
For me, adding the following block of code under <dependency management><dependencies> solved the problem.
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b06</version>
</dependency>
Check with your VPN connection, if you are working in home.
Delete or close all other projects, and restart netbeans IDE, and try again.

In Maven How to build the dependent projects that are added as the dependencies in the main project when building the main project?

I have 3 independent projects in my main project. When I want to add the dependencies of these projects to main project, then first I am building the dependent projects and then finally building the main project.
Is there is a way to build the dependent projects at the time of building the main project?
Sure, use a multi-module project
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>someName</artifactId>
<name>someName</name>
<packaging>pom</packaging>
<modules>
<module>dependentProject1</module>
<module>dependentProject2</module>
<module>mainProject</module>
</modules>
</project>
In this comment you specified your question a bit clearer: "My problem is when i am building the project C then the projects A and B are also to be build and their dependencies should be added at the time of C build process only". You have dependencies, want to build the dependencies on building the project, but you do not want to use a multi-module project.
Want you want to do is not possible. I had a similar question, and found this clear answer this answer. Most other answers and comments suggest to have a multi project structure or to orchestrate the different (independent) builds with an ant script.
Can you explain better your situation (include the pom.xml)...maybe what you need is this http://docs.codehaus.org/display/MAVENUSER/Multi-modules+projects
In child project you can add
<groupId>com.childproj.service</groupId>
<artifactId>A</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
And in parent project you can do like
<dependencies>
<dependency>
<groupId>com.childproj.service</groupId>
<artifactId>A</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

Categories