How do collect all dependencies for multi modules - maven

Collect all the dependencies for multi modules
I have modules shown as below:
parent
|_module1
|_module2
|_distribution
parent pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<profiles>
<profile>
<id>distribution</id>
<modules>
<module>distribution</module>
</modules>
</profile>
</profiles>
</project>
Distribution pom.xml
<project>
<artifactId>distribution</artifactId>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<inherited>false</inherited>
<configuration>
<includeScope>compile</includeScope>
<outputDirectory>${project.build.directory}/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I would like to collect all dependencies base on the profile.
profile1: all dependencies includes module1 and module2 jar itself.
profile2: all dependencies excludes module1 and module2 jar itself.
Profile2 mainly for development environment. We just need the dependency jars in the server, and we use Eclipse to deploy the module1 and module2 on the fly to the server.
I have tried to use maven-assembly to get all dependencies includes module1 and module2, but I don't how to have conditional in maven assembly to include / exclude module jar.
Thanks

Related

Why does profiles defined with modules are executed first -maven?

I have case where i want to download the jar and unpack the jar.
Within that jar i need to invoke the pom.xml from particular location ,
For eg: I downloaded jar and unpacked at
/home/sohanb/admin/target/com/tooling/admin/dockerfiles/cfg
Now at,
/home/sohanb/admin/target/com/tooling/admin/dockerfiles/cfg there is pom.xml which i need to call to build my docker image.
To do this i thought of doing something like this as below,
I have profiles where i want to invoke it sequentially.
unpack-jar
build-tools
Some how my modules profile is always getting called. I am not sure why this is happening or it is default nature of maven to invoke modules profile first.
Here is sample 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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>smp</artifactId>
<packaging>pom</packaging>
<name>dockerize</name>
<description>docker image creation</description>
<dependencies>
</dependencies>
<properties />
<profiles>
<profile>
<id>unpack_jar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.sigma.samp</groupId>
<artifactId>tooling.sigmaadmin</artifactId>
<version>6.0.0-20160405.213109-41</version>
<type>jar</type>
<excludes>**/*.class</excludes>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>build_tools</id>
<modules>
<module>target/com/tooling/admin/dockerfiles/cfg</module>
<module>target/com/tooling/admin/dockerfiles/logs</module>
</modules>
</profile>
</profiles>
mvn install -Punpack_jar -Pbuild_tools
when i execute above command, it always calls the modules profiles giving error,
[ERROR] Child module .... does not exist #
Please suggest.

Sonar with multi-module maven project and profiles

I have a main project pom.xml which has one module not bound to any profile (module A) and one module bound to profile rest (module module.REST). In jenkins profile, there is a build configuration with a cobertura and sonar plugin.
When Jenkins executes the sonar build, the maven runner only analyses modules which are not bound to profiles and the project module itself. Is there any other way to tell sonar to analyze all modules without specifying those modules again in the profile where the plugin itself is located (in my case this is jenkins profile)? I think cobertura plugin does perform code coverage on ALL modules.
Example:
<?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>group</groupId>
<artifactId>project</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>A</module>
</modules>
<profiles>
<profile>
<id>rest</id>
<modules>
<module>module.REST</module>
</modules>
</profile>
<profile>
<id>jenkins</id>
<activation>
<property>
<name>BUILD_NUMBER</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<aggregate>true</aggregate>
<formats>
<format>xml</format>
</formats>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
</plugins>
</build>
<modules>
<module>module.REST</module>
</modules>
</profile>
</profiles>
</project>

Maven plugin inheritance from pluginManagement

there
I have a parent pom.xml which defines the the default configuration for maven-ear-plugin
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact1</artifactId>
</jarModule>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact2</artifactId>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</pluginManagement>
In the child pom, I defined the maven-ear-plugin in the <plugins> section
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<executions>
<execution>
<id>default-ear</id>
<phase>package</phase>
<goals>
<goal>ear</goal>
</goals>
<configuration>
<earSourceDirectory>EarContent</earSourceDirectory>
<defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact3</artifactId>
</jarModule>
</modules>
</configuration>
</execution>
</executions>
</plugin>
My intention is to include all 3 jar files artifact1, artifact2 and artifact3 in the ear. However, after the build, only artifact3 defined in child pom.xml is included. So it looks like by default the <modules> definition in the child pom.xml overwrites what's defined in the parent pom, instead of merge them together. In fact, if I remove the whole <modules> section in the child pom.xml, the artifact1 and artifact2 will be included after the build.
My question is whether there is a way to include all jar modules defined in parent pom and child pom. I have several ear projects and all of them need to include the same set of jar modules plus a few jar modules of their own. I am trying to move the common set of jar modules to the parent so that they are not repeated in each child pom.xml.
Update to clarify my projects relations:
parent pom.xml (define maven-ear-plugin in the pluginManagement section)
-- project1 pom.xml (multi-module project)
-- myJar-proj1
-- myWeb-proj1
-- myEar-proj1 (define maven-ear-plugin in the <build> section)
-- project2 pom.xml (multi-module project)
-- myJar-proj2
-- myWeb-proj2
-- myEar-proj2 (define maven-ear-plugin in the <build> section)
In order to achieve the merge behavior you should put the maven-ear-plugin under the plugins tag in the parent pom (rather than under pluginManagement).
Parent pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<modules>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact1</artifactId>
</jarModule>
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact2</artifactId>
</jarModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
EDIT
After OP's clarified projects structure: the attribute combine.children="append" on the modules element, in the child projects:
<modules combine.children="append">
<jarModule>
<groupId>com.test</groupId>
<artifactId>artifact3</artifactId>
</jarModule>
</modules>
The parent should still define the plugin only in pluginManagement.

Maven multimodules w. assembly options per module?

I've a multimodule maven setup, where I'd like to pack one of the jars with their dependencies and all others could stay as they are. My configuration looks like this:
Root:
<project...>
<modelVersion>4.0.0</modelVersion>
<name>Foo</name>
<artifactId>Foo</artifactId>
<groupId>org.example</groupId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>Bar1</module>
<module>Bar2</module>
<module>Bar3</module>
</modules>
</project>
Module (Bar1):
<project...>
<modelVersion>4.0.0</modelVersion>
<name>Foo - Bar1</name>
<artifactId>Bar1</artifactId>
<groupId>${project.parent.groupId}</groupId>
<parent>
<artifactId>Foo</artifactId>
<groupId>org.exmaple</groupId>
<version>1.0</version>
</parent>
<build>
<finalName>Bar1</finalName>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.exmaple.bar1.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
...
</plugins>
</build>
</project>
With that running mvn package would give me all jar/war files for the modules. But to generate the jar with dependencies I have to switch into the module and trigger the assembly in addition cd Bar1; mvn assembly:single.
Is there any chance to change the setup so that after mvn package one of the jars is build with dependencies included?
Cheers.
Include the assembly plugin to the execution of the package phase:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/assembly/bin.xml</descriptor>
<finalName>apache-maven-cookbook-${pom.version}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
As described on maven assembly page
You need to define an execution for the assembly plugin. Otherwise it won't be executed. The site of the plugin may be a bit misleading as there is a section on configuration which looks like yours. But if you want the execution to actually happen, you need to define it.

Use jarsign plugin with assembly plugin in Maven 3

I'm using assembly plugin to package a list of applets into zip in one of modules with my maven project. here is the pom.xml:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>applets-deploy</artifactId>
<name>deploy</name>
<dependencies>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>applets-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>ceplot-applet</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
......
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/resources.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
what I need is to also sign jars before they are packaged, can I use jarsign plugin here, and how? I can't find where the jar files are temporarily stored during the build process.
Thanks,
This would be a pretty normal use of the jarsigner plugin. By default, jars are built by the jar plugin during the package phase and output to the ${project.build.directory}, which defaults to target.
You'd just need to sign the jars some time after they're built during package and before you assemble the zip. You could do that by binding the assembly plugin to a later phase or by adding the jarsigner plugin above the assembly plugin and binding it to the package phase, too.

Resources