Inheriting and Building with Maven - java

I started to learn maven and now I'm trying to make some realworld projects with beginning simple ones. Maybe it looks simple to most of all in here but I really confused about what should I do if I want to package both parent and children projects at the same time.
(PS: I'm using intellij idea btw)
here is my configuration
Project A
assume that it depends on guava and gson
Project B
it will depend on Project A
Project C
it will depend on Project B
when I set packaging attribute of Project A to jar, it gives an error basically saying that "you must set packaging element as pom if it's in a parent pom". But I know that if I set packaging element as pom it wont create the package.
So I decided to create another project as multimodule to manage the packaging issues but dont figure it out how! Shortly how can I generate jar files for each of them at the same time?
EDIT - HERE IS WHAT I DID
MODULE POM
<?xml version="1.0" encoding="UTF-8"?>
<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.deneme.module</groupId>
<artifactId>ModuleGroup</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<modules>
<module>A</module>
<module>B</module>
</modules>
</project>
PROJECT A
<?xml version="1.0" encoding="UTF-8"?>
<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.mg.A</groupId>
<artifactId>A</artifactId>
<version>1.0.2</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</project>
PROJECT B
<?xml version="1.0" encoding="UTF-8"?>
<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>
<packaging>jar</packaging>
<parent>
<artifactId>A</artifactId>
<groupId>com.mg.A</groupId>
<version>1.0.2</version>
<relativePath>../A/pom.xml</relativePath>
</parent>
<groupId>com.mg.B</groupId>
<artifactId>B</artifactId>
<version>1.0.1</version>
</project>

If your project A declares B and C as modules, it must have the packaging element set to pom. It also means that it shouldn't contains any code and will not generate an artifact by itself. It's merely a way of grouping projects together.
In maven, parent pom are used to define common configuration (properties, etc.) that will be inherited by all children projects.
In your case I would guess that you need a parent project (pom packaging) with 3 childrens : A, B and C :
parentProject
* module A
* module B
* module C
When running mvn install on parentProject, all sub-modules will be built and produce the corresponding jar (say A.jar, B.jar and C.jar ).
Note that you do not need to declare the parent in child pom to achieve this result, you only need to declare them as modules in the parent pom, which is called project aggregation. Declaring the parent in the child is called project inheritance. You can look at the following documentation for more details :
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance_vs_Project_Aggregation

I think you are confusing inheritance and dependency.
Inheritance: A child pom inherits all values, like properties, groupId, version, also dependencies. In general you should not use dependencies in parent poms
Dependency: A pom depends on some other jar/pom and uses it's contents and also uses/depends on transitive dependencies
As far as i understand your problem you have the following:
Project A - a library depending on external packages like gson and guava
Project B - a library depending on Project B, and transitively on gson and guava
Project C - an application depending on Project B, and transitively on Project A, gson and guava
I suggest you use this directory structure:
Workspace
|- ModuleProject
|- ProjectA
|- ProjectB
\- ProjectC
and the poms would look like this (header stripped):
Module pom:
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<packaging>pom</packaging>
<version>1.0.1</version>
<modules>
<module>../ProjectA</module>
<module>../ProjectB</module>
<module>../ProjectC</module>
</modules>
Project A:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>A</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
Project B:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>B</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.deneme.projectname</groupId>
<artifactId>A</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
Project C:
<parent>
<groupId>com.deneme.projectname</groupId>
<artifactId>ModuleGroup</artifactId>
<version>1.0.1</version>
<relativePath>../ModuleProject/</relativePath>
</parent>
<artifactId>C</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.deneme.projectname</groupId>
<artifactId>B</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>

Related

Maven: why are dependencies declared in parent pom not inherited by child pom?

When I declare a dependency in a parent pom like -
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.12</version>
</dependency>
</dependencies>
</project>
above i have declared spring-core as a dependency for parent pom.
Now in child pom, i am importing the parent 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>deps2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</project>
Now on looking at the dependencies inherited by child pom, there are none. Should NOT the spring-core jar be inherited by child project in all cases. As the parent pom directly depends on this jar and is it not passed on/inherited by child projects.
Note: This question is not about dependency management and versions
I understand dependencyManagement, which is to ensure that a set of projects have the same version and scope of a depencency.
There is a difference between placing a parent tag and Bill of materials (importing the pom in the dependency management section)
Using <parent> is a "real inheritance" in maven. You define this tag on the child pom and by that you will get all the dependencies defined in the parent pom automatically (also properties and plugins).
The Bill of Materials on the other hand (This is how its called in the official documentation) doesn't import any dependencies by itself, however it allows to avoid specifying the versions of the dependencies in the pom of your application, because you define them in this BOM.
So to answer your question, you should really rewrite the child pom as:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>deps2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- not relevant for this question -->
</dependencies>
</project>
Your child pom is a standalone pom because you didn't specified a parent. You define a parent by adding this tag :
<parent>
<groupId>yourpackage</groupId>
<artifactId>yourartifactid</artifactId>
<version>version</version>
</parent>
In your case, this block should do the trick :
<parent>
<groupId>com.demo</groupId>
<artifactId>deps</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
I know that your question was not about depedencies management; but for people that would not know the difference I'll write some words about that.
Note that by importing your pom in <dependenciesManagement> you don't have any impact given that it only defines intentions of use but not concrete import. The <dependencies> contains the concrete imports and it's only its content that you can use in your application.

Unable to use classes of one child module into another in multi-module maven project

I want to use the classes of one child module into another child module present in multi-module maven project. I have followed many existing solution. I am doing exactly same thing as in existing solution. But still it does not works.
Project structure:
Parent A: sampleproject
|-- child A: Gui
|-- child B: calculator
This is how pom.xml of parentA looks like:
<groupId>com.example</groupId>
<artifactId>sampleproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>Gui</module>
<module>calculator</module>
</modules>
pom.xml of child A looks like:
<parent>
<groupId>com.example</groupId>
<artifactId>sampleproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Gui</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>calculator</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
<dependencies>
pom.xml of child B looks like:
<parent>
<groupId>com.example</groupId>
<artifactId>sampleproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>calculator</artifactId>
<packaging>jar</packaging>
When I perform mvn install on parent project. jar files are created in calculator/target/name_of_jar.jar and Gui/target/name_of_jar.jar. But when I check in maven dependencies of Gui project, only directory of calculator is created rather than jar file.
Here is image of maven dependencies in Gui project.
Links I have followed: use-a-class-from-the-other-child-module-in-maven and share-classes-within-modules-in-maven-project

Maven module - How to use it as an external lib in an unrelated project

I have set-up a modular project in Maven with a parent project having more than one child projects.
The pom.xml of parent looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
<packaging>pom</packaging>
<dependencies>...</dependencies>
<properties>
<parent-application.version>1.0</parent-application.version>
</properties>
<modules>
<module>parent-model</module>
<module>parent-masters</module>
<module>parent-web</module>
</modules>
</project>
The pom.xml of child projects looks as follows -
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.company</groupId>
<artifactId>parent-app</artifactId>
<version>${parent-application.version}</version>
</parent>
<packaging>jar</packaging>
<artifactId>child-model</artifactId>
<dependencies>...</dependencies>
</project>
Now, I need to use one of the child projects as a lib in a separate unrelated project. The pom of the new project looks like below.
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>unrelated-app</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.company</groupId>
<artifactId>child-model</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I keep getting the below error
Illegal character in path at index 57: http://repo.maven.apache.org/maven2/com/company/parent-app/${parent-application.version}/parent-app-${parent-application.version}.pom
The reason seems that I am inheriting the version attribute in child-model from prent-app.
Is there a way to overcome this issue? OR do I need to provide the version for each child module in respective pom.xml and cannot inherit from common parent.
Thanks in advance.
There is a way to overcome this issue with relativePath but I would not recommend it (take a look at this answer)... You should always provide version for parent module. To update all versions of all modules (parent+children) you can use maven-version-plugin or maven-release-plugin.

Maven Project Requires Local Dependency to be Built

I have 2 Maven projects, A and B. Project A is entirely independent, while B requires A as a dependency. Currently, B is getting A from the maven repository:
<dependency>
<groupId>com.myproject</groupId>
<artifactId>projectA</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
What I would like to do is set up the dependency such that B can reference the local instance of A. I can do the following:
<dependency>
<groupId>com.myproject</groupId>
<artifactId>projectA</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${basedir}/../projectA/target/projectA-2.0.0-SNAPSHOT.jar</systemPath>
</dependency>
but this only works if A is already built (so projectA-2.0.0-SNAPSHOT.jar already exists).
Is there a way to force A to build before B in cases where I don't already have a build of A?
One way is you create a parent pom, and add these projects as child modules. So, the directory structure would look like this:
+ pom.xml
|
+-A
| \
| + pom.xml
+-B
\
+ pom.xml
The parent pom would have <modules> tag, adding A and B projects as child modules:
<groupId>com.myproject</groupId>
<artifactId>parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>A</module>
<module>B</module>
<modules>
And then, add A module as dependency to B module. Just keep the group-id same for parent, A and B. And then refer the module using artifact-id.
pom.xml for B would be like:
<parent>
<groupId>com.myproject</groupId>
<artifactId>parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>projectB</artifactId>
<dependencies>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>projectA</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
Now, build the parent pom. It will take care of build order.

how to make maven build dependent project

I have project with several dependencies on other project.
<?xml version="1.0" encoding="UTF-8"?>
<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>group1</groupId>
<artifactId>artifact1<artifactId>
<name>RealtyRegistry</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact2</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>group1</groupId>
<artifactId>artifact3</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
All of them developed by me simultaniously. I add edition to files of all of project and i need to build main project together with dependent ones. How to do that for projects without tree structure?
There can be 2 or more covering trees for projects hierachy, for example: A depends on B,C; D depends on C,E; A and D are independent.
You can build multiple projects together using "Modules". Normally, you would do this by creating a "mother" project with <packaging>pom</packaging> and adding your real project as modules using the <modules> tag. Then, when you build the "mother" project, all modules are automatically built in the right order.
Here is an example from the Maven by Example book:
<groupId>org.sonatype.mavenbook.multi</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
Note that this requires you to have your modules in subfolders that are named accordingly. For example, you would have the "mother" pom in some folder:
/.../my-project/
and the modules in:
/.../my-project/simple-weather/
/.../my-project/simple-webapp/
For more information, read Chapter 6. A Multi-module Project of the book, it's freely available on the Sonatype website.

Categories

Resources