I have a project structure like below.In aggregator POM project1 and project 2 are defined as modules.Now if i want to add a maven property that can be accessed in all the projects how can i do it?.
What is the best project structure in this case.?
If i define property like <temp.dir>data</temp.dir> in the aggregator POM then it is not available in project1 pom.xml and project2 POM.xml.I have to duplicate the same property in both the POM's.
project
- project1
- - pom.xml ---- has parent POM as project A
- project2
- - pom.xml --- has parent POM as project B
- pom.xml (Aggregator POM)
UPDATE:To clarify more project1 pom and project 2 has different parent POM's.So aggregator POM cannot be set as parent.
You need to set the aggregator pom as the parent of project1 and project2.
eg. in project1 and project2 poms add:
<parent>
<groupId>com.agr</groupId>
<artifactId>project-aggregator</artifactId>
<version>1.0.0</version>
</parent>
EDIT:
I understand that project1 and project2 have different parent poms.
The only solution i know about to achieve what You want is to store the properties in parent pom.
I can think only of one solution, to make an inheritance chain:
Project2Parent:
<parent>
<groupId>com.test</groupId>
<artifactId>Project1Parent</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>Project2Parent</artifactId>
<version>0.0.1</version>
Aggregator:
<parent>
<groupId>com.test</groupId>
<artifactId>Project2Parent</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>Aggregator</artifactId>
<version>0.0.1</version>
Project1
<parent>
<groupId>com.test</groupId>
<artifactId>Aggregator</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>project1</artifactId>
<version>0.0.1</version>
Project2
<parent>
<groupId>com.test</groupId>
<artifactId>Aggregator</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>project2</artifactId>
<version>0.0.1</version>
Here is aggregator's pom:
<groupId>y</groupId>
<artifactId>y</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>y1</module>
<module>y2</module>
</modules>
<properties>
<x>1</x>
</properties>
This is y1 effective pom (generated by Eclipse):
<parent>
<groupId>y</groupId>
<artifactId>y</artifactId>
<version>0.0.1</version>
</parent>
<groupId>y1</groupId>
<artifactId>y1</artifactId>
<version>0.0.1</version>
<properties>
<x>1</x>
</properties>
As you can see property x declared in parent is inherited by child.
Related
Project have next structure:
root
|
+--Module 1
| |
| +--Submodule_1
| |
| +--Submodule_2
| |
| +--Submodule_N
|
+--Module 2
Module 2 depend on Module 1 and expected access to classes from submodules 1..N
Root pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>root</artifactId>
<version>master</version>
<packaging>pom</packaging>
<modules>
<module>Module_1</module>
<module>Module_2</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
Module 1 pom.xml:
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>root</artifactId>
<version>master</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Module_1</artifactId>
<packaging>pom</packaging>
<modules>
<module>Submodule_1</module>
<module>Submodule_2</module>
...
<module>Submodule_N</module>
</modules>
</project>
Submodule_N pom.xml:
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>Module_1</artifactId>
<version>master</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Submodule_N</artifactId>
</project>
Module 2 pom.xml:
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>root</artifactId>
<version>master</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Module_2</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>Module_1</artifactId>
<version>master</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
So this configuration fail with error package com.example.submodule_n.x.y does not exist.
If add dependency from module 1 to submodule_n than package will be found but maven fail with error ...is referencing itself.
One working case if add dependency from module 2 to submodule_n. But expected way when module 2 will have only one dependency to module 1 for include all submodules from module 1.
It is can do gradle, but I don't have any ideas how it implement using maven.
One working case if add dependency from module 2 to submodule_n. But expected way when module 2 will have only one dependency to module 1 for include all submodules from module 1
That is your expectations that are actually not valid.
The purpose of configuration like:
<modules>
<module>Submodule_1</module>
<module>Submodule_2</module>
...
<module>Submodule_N</module>
</modules>
is to somehow organise project structure, it have nothing in common with dependencies between java libraries (though, it affects build lifecycle), moreover, the configuration like:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>Module_1</artifactId>
<version>master</version>
<scope>compile</scope>
</dependency>
</dependencies>
in general does not make sense because library may not depend on pom - actually that just tells maven to build Module_2 after Module_1.
Summarising statements above: if Module_2 depends on Submodule_N you must explicitly define that dependency in pom.xml
I have created a parent project so I can have all the common dependencies, DTOs, services, helpers, etc. that I will use in all my common projects.
This parent project is imported as POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hawk-parent</name>
<packaging>pom</packaging>
<description>Parent configuration for microservices</description>
In the other projects I have:
<parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>customers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customers</name>
<description>Customers microservice</description>
The dependencies have been imported correctly, but I am not able to access for example my Role enum even if it has public access modifier.
public enum Role {
CUSTOMER,
ADMIN,
SUPERADMIN
}
Or my #UserRole annotation:
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Documented
#Inherited
public #interface UserRole {
Role[] value();
}
Any idea about what I am doing wrong?
Cheers!
Update:
Following suggestions, I added a new module: hawk-core
Its POM looks like:
<?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>
<parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<artifactId>hawk-core</artifactId>
<name>hawk-core</name>
<description>Core libraries for microservices</description>
<packaging>jar</packaging>
</project>
And included this in the parent:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.whitehawk</groupId>
<artifactId>hawk-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hawk-parent</name>
<packaging>pom</packaging>
<description>Parent configuration for microservices</description>
<modules>
<module>hawk-core</module>
</modules>
I think there is misunderstanding here for 2 things:
1- packaging: this field is used as POM if you are not having classes to be package in jar/war file, this used for example for parent project which is not used as dependancies or packaged to be deployed.
2- dependancies: this decencies is used for adding lib packages you required for your project from sample you have here you are not including this, so the classes will not be seen in your project
suggest to have a parent project that not have any classes and have 2 modules one is your core classes to be jar as lib, and second one as spring boot you may refer to https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-maven-without-a-parent and have the first module as dependency in your second module to be like:
parent
|
|-core
|-pomt.xml
|-app
|-pomt.xml
|-pom.xml
The parent pom:
<modelVersion>4.0.0</modelVersion>
<groupId>xx.xxxx.xxxx.xx</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<properties_deinfed_in_parent_pom>1.0.2-SNAPSHOT</properties_deinfed_in_parent_pom>
</properties>
The child pom:
<parent>
<groupId>xx.xxxx.xxxx.xx</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>child-pom</artifactId>
<packaging>war</packaging>
<version>${properties_deinfed_in_parent_pom}</version>
which includes the parent pom, if I change the property properties_deinfed_in_parent_pom in parent pom, the version in child pom won't be updated. The only way I can update is using -Dxxx=value in maven command. I also checked the effective pom, it's also not updated. So will this idea work as my expectation, or what I thought was wrong.
I use Maven 3.3.x, IntelliJ 2018.1
The parent pom need to have the list of modules specified. Add the following to the parent pom
<modules>
<module>child-pom</module>
</modules>
I have existing maven Project(named as projectB) that is parent of other Projects(projectC and projectD), and I want to make projectB as child of newly created maven Project(named as projectA). This is Directory structure:
projectA
-> pom.xml
projectB
-> pom.xml
projectC
-> pom.xml
projectD
-> pom.xml
This is how my pom.xml of projectA looks:
<modules>
<module>projectB</module>
</modules>
And this is my pom.xml of projectB Looks:
<parent>
<groupId>(groupId of projectA)</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modules>
<module>../projectC</module>
<module>../projectD</module>
</modules>
But when I perform make clean on projectA it gives me error:
Child module C:\Users\projectB of C:\Users\projectA\pom.xml does not exist
I have searched existing solutions, but none has helped me in solving the problem.
Since your all projects are all in the same level, you should have:
In Project A:
<modules>
<module>../projectB</module>
</modules>
And in Project B:
<parent>
...
<relativePath>../projectA/pom.xml</relativePath>
</parent>
Below Configuration in pom.xml of Project B is incorrect
<parent>
<groupId>(groupId of projectB)</groupId>
<artifactId>projectB</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
In parent section, you have to specify Project A not B. Something like below should help
<parent>
<groupId>(groupId of projectA)</groupId>
<artifactId>projectA</artifactId>
<version>(Project A version)</version>
</parent>
I have a fairly simple maven-ized Java project, but am having trouble getting my head around it.
My parent module defines a lot of Java classes (and dependencies) that I expect to be useful for several child modules. One of the child modules is dedicated to deploying a web app, so it needs a few extra classes (the servlets) plus everything from the parent module.
The file structure looks like this
- parent
- src
- pom.xml
- child
- src
- pom.xml
My parent pom looks like this:
<project>
<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
...
<modules>
<module>child</module>
</modules>
</project>
And the child looks like this:
<project>
<artifactId>child</artifactId>
<packaging>war</packaging>
<parent>
<artifactId>parent</artifactId>
<groupId>my.group</groupId>
<version>0.0.1</version>
</parent>
...
</project>
Is this all I need to have the child know about the classes and dependencies defined in parent? It doesn't seem to be: eclipse gives compile errors, and running mvn clean package from parent folder or child folder results "cannot find symbol" messages any time a class from parent is mentioned.
What am I doing wrong?
I'd change the structure of your project like this:
parent (pom)
core (jar, with all the classes that used to be in parent)
child (war, depends on core)
Parent:
<project>
<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>child</module>
<!-- possibly more modules... -->
</modules>
</project>
Core:
<project>
<parent>
<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>core</artifactId>
<packaging>jar</packaging>
</project>
Child:
<project>
<parent>
<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>my.group</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Since your parent module's packaging is "pom", no jar file will be provided and no .class file from that will be accessible to other modules. Try changing it to "jar" and give it another try.
Although it's not a good practice to have a parent as "jar". I'd stick to the "pom" parent and probably create a new "core" or "common" child and make my "war" depend on it.
Try to add an internal dependency in child module pom, so it is familiar with its parent
<dependency>
<groupId>my.group</groupId>
<artifactId>parent</artifactId>
<version>0.0.1</version>
</dependency>
The first thing you should do is to define your dependencies within a dependencyManagmeent block in your parent like this:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
</dependencyManagement>
and furthermore in your child you should say which dependency your child would like to use as dependency:
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-a</artifactId>
<!-- No version given. See dependencyManagement -->
</dependency>
...
</dependencies>