Maven : only deploy root project - maven

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.

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.

Build multi maven modules then copy JARs inside a docker image

I have a multi-modules project configured with maven.
/myProject
/module1
/target
pom.xml
/module2
/target
pom.xml
pom.xml
The pom parent is building the submodules.
Using the com.spotify.dockerfile-maven-plugin I want to build all the modules, build a docker image and copy all the JARs inside that image.
The pom parent
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>myTest</artifactId>
<name>test</name>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
</project>
If I put a Dockerfile at the root of /myProject, the plugin complains there is no Dockerfile for module1
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} /home/xyz/jars
maven plugin:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<configuration>
<repository>mytest</repository>
<buildArgs>
<JAR_FILE>target/${project.artifactId}-${project.version}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
This works fine with one module if I put the Dockerfile inside that module but how to do with multiple modules and only 1 Dockerfile?
Can I build the submodules and then the docker image with a single mvn clean install on the parent pom?
Maybe by creating a submodule only with a pom and the Dockerfile?
If the jars are dependencies for one executable then yes, you could just build that one executable with its dependencies declared in its pom.xml file. Maven should figure out which order to build the submodules in, so long as you don't create a cycle. As you suggest, you want to make it a submodule and not the root pom as the root pom is parent configuration that is inherited by other modules. Just put the plugin on that one submodule (in its pom.xml) and only give it the Dockerfile.
(If you think of this as a problem about having one executable depending on libraries in the same maven project then it's not really related to docker. You could ask the same question about a spring boot app using libraries in the same multi-module project. In a sense it just is that problem as the jar will get built together with its dependencies and copied into the docker image.)
Or if you mean that each jar for each submodule is an executable in itself (and I don't think you do) then you could create multiple docker images, each from its own Dockerfile, and then you can bundle and start them together if you want to with a docker-compose file.

How to access files from module using maven without searching it in local .m2 repository & central maven repository?

I have two projects. Project A & B. Project B is dependent on project A.
To run project B, I have to add project A's depedency in project B's pom.xml.
If I change something from Project A. I have to do mvn install again to update local repository with latest code of project A. Then I can run Project B.
Can I access Project A's changed code in project B without doing mvn install?
No. You can't access the updated code. That is because, when you build a maven application, it will get all the dependencies from your .m2 repository. If it does not find the dependency there, then it will search your remote repository.
In your case, when you update A's code and don't build it, then the updated artifact will not be be available in your .m2 repository. Now if you build Project B, then it will try to fetch project A artiufact from .m2 repository. Since you did not build A after modifying the code, B will fetch the artifact that is currently there in the .m2 folder i.e. the artifact with the old code.
Another approach that you can use is to have a parent pom. This is just an aggregator pom which will execute the pom.xml for both of your modules. For eg:
<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>org.sonatype.mavenbook.multispring</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>module-A</module>
<module>module-B</module>
</modules>
</project>
Please be aware that the packaging for this should be pom.

Specify modules in root pom with archetype

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>

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