Specify modules in root pom with archetype - maven

I'm generating a custom archetype that has 4 child modules. The problem is, when I define these 4 modules in the root pom.xml, using the following notation:
<modules>
<module>${rootArtifactId}-client</module>
<module>${rootArtifactId}-daemon</module>
<module>${rootArtifactId}-impl</module>
<module>${rootArtifactId}-war</module>
</modules>
When i run a mvn install on the archetype, I get an error saying that the project doesn't contain the modules.
Any ideas?
Thanks.

update your pom as follow:
<modules>
<module>${project.artifactId}-client</module>
<module>${project.artifactId}-daemon</module>
<module>${project.artifactId}-impl</module>
<module>${project.artifactId}-war</module>
</modules>

Related

Maven: Naming of a copy of a project

I have a project that consists of 3 subprojects.
I handle it with Maven in Eclipse.
MyPrj
Server
pom.xml with
<groupId>MyPrj</groupId>
<artifactId>Server</artifactId>
<name>Server</name>
src/main/java/MyPrj/Server/Server.java with
package MyPrj.Server
Client
pom.xml with
<groupId>MyPrj</groupId>
<artifactId>Client</artifactId>
<name>Client</name>
src/main/java/MyPrj/Client/Client.java with
package MyPrj.Client
pom.xml with
<groupId>MyPrj</groupId>
<artifactId>MyPrj</artifactId>
<name>MyPrj</name>
<modules>
<module>Server</module>
<module>Client</module>
</modules>
All is working fine.
Now I need to have a copy of MyPrj that should represent a newer version.
I did a copy of MyPrj.
MyPrj2
Server
pom.xml with
<groupId>MyPrj2</groupId>
<artifactId>Server</artifactId>
<name>Server</name>
src/main/java/MyPrj2/Server/Server.java with
package MyPrj2.Server
Client
pom.xml with
<groupId>MyPrj2</groupId>
<artifactId>Client</artifactId>
<name>Client</name>
src/main/java/MyPrj2/Client/Client.java with
package MyPrj2.Client
pom.xml with
<groupId>MyPrj2</groupId>
<artifactId>MyPrj2</artifactId>
<name>MyPrj2</name>
<modules>
<module>Server</module>
<module>Client</module>
</modules>
I can compile and package it in Maven without a problem.
But if I try to import MyPrj2 in Eclipse (exsiting Maven project) it gets a problem with the subprojects (Client, Server).
I can not tick the subprojects ("Project Client is already imported into workspace").
So the dirtories are imported but they are not recognized as a Java project.
So must be some problem with the naming. But the groupId is clearly different.
Any help would be appreciated, thanks!
If importing an existing Maven project you can select in Advanced a naming template.
I selected [groupid].[artifactid] there and then both projects were able to co-exist in eclipse.

Maven says it cannot find something in the "reactor"

I have a maven project and I'm using this --projects command from the root of the project to run several pom files.
mvn clean install --projects proj1, then-proj2
The proj1 and then proj2 are the artifact ids of the projects I want to run. That is, I go to the pom.xml for each project and use the "artifact id" there.
Both proj1 & then-proj2 are themeslves sub modules of projects in the main pom file.
Say proj1 is in A, and then-proj2 is in B, and both A & B are in the main pom file.
When I run this, maven tells me: "could not find the selected project in the reactor: proj1".
This is really frustrating - why not tell me what all the projects in the reactor are? Anyway, what am I doing wrong?
If you only use the artifactId's of the given project you have to define that correctly on command line:
help output of Maven (mvn --help)
Comma-delimited list of specified reactor projects to build of all
projects. A project can be specified by [groupId]:artifactId or by its
relative path
This means in your case you have to define:
mvn clean install --projects :proj1,:then-proj2
Try this:
mvn clean install -pl A/proj1, B/then-proj2
check this out: https://stackoverflow.com/a/23076358/1680793
Another thing is to make sure that you have listed your child projects inside the
<modules>
<module>
sections of the corresponding parent multimodule projects.
For example in the below project structure:
main
A
proj1
proj1A
B
then-proj2
Let's say you are trying to build proj1A. When you try
mvn package -pl A/proj1/proj1A
from the main's pom directory you will still have this same reactor error if you don't have:
"A" as a module in "main", or
"proj1" as a module in "A", or
"proj1A" as a module in "proj1"
If your modules are distinguished based on profiles then make sure to consider profiles too. Eg:
mvn -P profile1 -pl relative/path/to/project1 clean install
For this kind of setting, the pom would be:
<profiles>
<profile>
<id>profile1</id>
<modules>
<module>project1</module>
</modules>
</profile>
<profile>
<id>profile2</id>
<modules>
<module>project2</module>
</modules>
</profile>
</profiles>
Not mentioning profile would also give the Not Found in reactor.
A solution that worked for me, run the below command from the root directory.
mvn clean install --projects :projA, :projB -am
assuming that projB is dependent on projA
-am
If project list is specified, also
build projects required by the
list
The command used by you :
mvn clean install --projects 'submodule1','submodule2`
works on the sub-modules submodule1 and submodule2 specified in the pom.xml of the module where you're executing this command.
The guide to Working with Multiple modules shall help you understand the reactor and its sorting order. A general structure of the module for such use case would look like:
<groupId>stackoverflow</groupId>
<artifactId>mainmodule</artifactId>
<packaging>pom</packaging>
<version>1.2.3</version>
<modules>
<module>submodule1</module>
<module>submodule2</module>
... others
</modules>
... other tags

Maven : only deploy root project

I have a root pom.xml which has :
<modules>
<module>A</module>
<module>B</module>
</modules>
Now I want to install all modules, but deploy only root project (push artifact to Nexus, but not artifacts of child's), without editing poms of children porjects. How should I do?
Thanks.

List all the modules of an aggregate POM dependent on a specific dependency

Here's my aggregate POM:
<project>
...
<groupId>myAggregate</groupId>
<artifactId>myAggregate</artifactId>
...
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
<module>module5</module>
</modules>
...
</project>
Assume that the modules: module1, module3 and module5 depend on same/different versions of an artifact, say, dependency1. I wanted the maven-dependency-plugin to list these modules when I run a goal on the aggregate POM above, but I couldn't find a goal that would do it for me. Like, for example:
mvn dependency:dependents-of -Dartifact=...
Anyone know of any plugin that tells me this or if I should write my own goal for the plugin?
Since the modules were right under the aggregator, I merely used grep on the pom.xmls to list them. Sometimes, simplicity helps ;)

How to setup circular reference in maven modules?

I have this problem with building a maven project...
A mvn project parent is set like this:
<groupId>com.company.system.ping</groupId>
<artifactId>system-ping</artifactId>
<name>system-ping</name>
<parent>
<artifactId>parent_lvl_1</artifactId>
<groupId>com.company.system</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
Then I look for said parent's POM and find this:
<groupId>com.company.system</groupId>
<artifactId>parent_lvl_1</artifactId>
<packaging>pom</packaging>
<name>_proj_test</name>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>parent_lvl_0</artifactId>
<groupId>com.company</groupId>
<version>1</version>
</parent>
I finally look at the first parent and see this:
<groupId>com.company</groupId>
<artifactId>parent_lvl_0</artifactId>
<packaging>pom</packaging>
<name>_main</name>
<version>1</version>
<description>The whole Projects</description>
Now I check the modules:
<modules>
<module>../_proj_test</module>
...
</modules>
The first parent module is referencing a child! So when I try
mvn install
on '_main' I get this:
[ERROR] The project com.company.system:_proj_test:1.0-SNAPSHOT (C:\...\pom.xml) has 1 error
[ERROR] Non-resolvable parent POM: The repository system is offline but the artifact com.company:_main:pom:1 is not available in the local repository. and 'parent.relativePath' points at wrong local POM # line 4, column 10 -> [Help 2]
[ERROR]
I translate this to: you parent requieres a module which requires you back. If I comment out everything in modules I can install everything correctly but this is NOT OK for me! How can I build the parent without building the module dependencies?
Thank you!
The config looks very weird to me, mainly because your '_proj_test' which is a child for '_main' is located on the same level.
How can I build the parent without building the module dependencies?
Try mvn -N install.
As for the project structure, I'd do it in such a way:
1) $basedir with '_main' pom.xml which should contain this
<modules>
<module>_proj_test</module>
</modules>
2) $basedir/_proj_test with '_proj_test' pom.xml which should contain this
<modules>
<module>system-ping</module>
</modules>
3) $basedir/_proj_test/system-ping with 'system-ping' pom.xml (with correct groupId of parent - com.company.system instead of com.company)

Resources