Reputation: 15744
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
Project B
Project C
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>
Upvotes: 3
Views: 256
Reputation: 7994
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:
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>
Upvotes: 1
Reputation: 2504
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 :
Upvotes: 2