Maven multimodules w. assembly options per module? - maven

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.

Related

Installing maven generated artifacts without using the artifactId

I have a maven parent project with 2 modules. When building the parent project, I want to generate and install the final artifact with a different name than the artifactId used in the pom.xml. I can see that the artifact generated does have the name that I want use in the build dir. However when the archive is installed in the local Maven repository, the maven-install-plugin copies the artifacts using the project's artifactId. How can I configure the plugin to install the artifacts without using the artifactId?
My child project's pom.xml:
<project>
<parent>
<groupId>com.mycomp</groupId>
<artifactId>com-mycomp</artifactId>
<version>12.0.0.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycomp</groupId>
<artifactId>child1</artifactId>
<version>12.0.0.0.0</version>
<build>
<defaultGoal>install</defaultGoal>
<finalName>${parent.artifactId}-${project.version}</finalName>
<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>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependencies/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly:package</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/dependencies.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<classpathPrefix>libs/</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>com.my.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
The parent pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycomp</groupId>
<artifactId>com-mycomp</artifactId>
<version>12.0.0.0.0</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
:
:
</project>
The desired output of mvn clean install:
1) com-mycomp-12.0.0.0.0.jar in the local maven repository
2) com-mycomp-12.0.0.0.0.zip comprising of the above jar and its dependencies.
The observed behavior:
3) com-mycomp-12.0.0.0.0.jar is created in the build dir. But it is installed in the maven repository as child1-12.0.0.0.0.jar
4) com-mycomp-12.0.0.0.0.zip is created in the build dir. But it is installed in the maven repository as child1-12.0.0.0.0.zip
How can I achieve the desired behavior (#1 and #2) ?
Artifacts in the local repository are always installed with their artifactId.
You cannot change that. And even if you could, I would strongly recommend against that because you are breaking expectations.
If you want to create an additional zip that contains the jar with dependencies, you don't need a second module. You can create it through the assembly plugin and attach it with a classifier like dependencies. You then have two artifacts like
com-mycomp-12.0.0.0.0.jar
com-mycomp-12.0.0.0.0-dependencies.zip

How to maintain daily SNAPSHOT build using TeamCity in C# projects

I am taking over a project which consists of around 15 projects, whose continuous integration practice was done (the practice was ceased and I need to re-start it in a new environment) by Maven + Nexus OSS + TeamCity and are developed using C#.
What I got, except for those C# solutions themselves, is a POM for each of these project, and another parent POM (which doesn't contain any code) which every other project has parent of. These development POMs only have inter-dependency on SNAPSHOT versions hence the build order is crucial. But these POMs that I have do not need any VS plugin, which means (I guess) the compile procedure is not done by Maven but by TeamCity (VS runner). The Maven scripts I have are probably only in charge of e.g., downloading dependencies, validating and installing/deploying/releasing. Unfortunately I can't find any TeamCity configurations so I have no clue how this was done before.
EDIT:
I'll try to put some POM and script file that I have and see if someone can see some clue on the build procedure.
The files I got from SVN are mainly in three kinds categories:
1) The C# source code and project/solution files. Each solution has a 'Dependency' folder which contains all the dependencies, both on other projects on third-party dlls, so that this solution can be built in VS by the developer right after he checkout this solution.
2) The (development) POMs.
Firstly I have the POM of the parent project. This project doesn't contain any code but only the POM and some scripts (other projects have similar files too). The POM looks like this:
<modelVersion>4.0.0</modelVersion>
<groupId>MY.GROUP</groupId>
<artifactId>configuration</artifactId>
<version>1.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Configuration</name>
...
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>purge-local-dependencies</id>
<phase>clean</phase>
<goals>
<goal>purge-local-repository</goal>
</goals>
<configuration>
<!-- Whether to purge only snapshot artifacts. -->
<snapshotsOnly>true</snapshotsOnly>
<actTransitively>false</actTransitively>
<reResolve>false</reResolve>
</configuration>
</execution>
<execution>
<id>unpack-dependencies</id>
<phase>validate</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${dependencies.directory}</outputDirectory>
<markersDirectory>${dependencies.markers.directory}</markersDirectory>
<useBaseVersion>true</useBaseVersion>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>false</excludeTransitive>
<useSubDirectoryPerArtifact>true</useSubDirectoryPerArtifact>
<stripVersion>true</stripVersion>
<stripClassifier>true</stripClassifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>assembly-single-zip</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.build.finalName}</finalName>
<descriptors>
<descriptor>${assembly.file}</descriptor>
</descriptors>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>deploy-file-snapshot</id>
<phase>verify</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${deploy.file}</file>
<repositoryId>${nexus.repository.id}</repositoryId>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<url>${nexus.repository.url}</url>
<pomFile>${nexus.deploy.pom}</pomFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<preparationGoals>clean</preparationGoals>
<tagBase>${release.tagBase}</tagBase>
<tagNameFormat>#{project.version}</tagNameFormat>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
...
Here we see that the plugins used in the build are dependency (to download dependencies), 'assembly' (to package), deploy (to deploy files to Nexus) and release--frankly I can't figure out how it is used. The scripts that I have (I'll describe later) don't use it explicitly and it doesn't seem to be executed in other standard build phases.
And in each of the solution the POM has parent of the configuration. And they look like this:
ProjA
<parent>
<groupId>MY.GROUP</groupId>
<artifactId>configuration</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<groupId>MY.GROUP</groupId>
<artifactId>ProjA</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ProjA</name>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
</plugins>
</build>
...
In ProjB which depends on ProjA, the POM is like this:
<parent>
<groupId>MY.GROUP</groupId>
<artifactId>configuration</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<groupId>MY.GROUP</groupId>
<artifactId>ProjB</artifactId>
<version>1.2.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ProjB</name>
...
<dependencies>
<dependency>
<groupId>MY.GROUP</groupId>
<artifactId>ProjA</artifactId>
<version>1.1.2-SNAPSHOT</version>
<type>zip</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
</plugin>
</plugins>
</build>
...
3) Then some .bat scripts as well as deploy.pom and release.pom.
The deploy pom and release pom just simply replace the version numbers and declare the dependencies:
deploy.pom for ProjA:
<?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>MY.GROUP</groupId>
<artifactId>ProjA</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>pom</packaging>
..<dependencies>...</dependencies>
By deploy I assume it means the the deployment of SNAPSHOT version as the version number indicates.
And the release.pom are basically the same but change the version to release version (in ProjA it is 1.1.1).
In each solution I also have some scripts which I believe are called by TeamCity. a) a file called download.bat, which basically calls mvn -U clean and then mvn -U validate. And b) a file called upload.bat which basically calls mvn prepare-package and then mvn verify. In both scripts we pass some mvn options like -DDeployPomFile, -DNexusUrl, -DRepositoryId. From the parent POM we can see some plugins are executed in those scripts too. And I guess the download.bat is called before TC is executing the VS build and upload.bat is called after the build (assume the target of the build is to publish the latest version).
Above is all I got. I suspect I still miss some TeamCity configuration because they are not stored in the SVN. But anyway, can somebody help figure out how to manage the daily build? Thank you very much!

Referring dependencies that don't yet exist in Maven

I need to install a whole bunch of 3rd party jar files in my local repository, using maven-install plugin. That part is done with the following 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sap</groupId>
<artifactId>sdk</artifactId>
<version>${sap.version}</version>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>cecore</item>
<item>cesession</item>
<item>celib</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
</plugin>
<goal>install-file</goal>
<configuration>
<file>${bo.lib.dir}/#item#.jar</file>
<groupId>${sap.group}</groupId>
<artifactId>#item#</artifactId>
<version>${sap.version}</version>
<packaging>jar</packaging>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<bo.lib.dir>C:\Program Files\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\java\lib</bo.lib.dir>
<sap.group>com.sap</sap.group>
<sap.artifact>sdk</sap.artifact>
<sap.version>4.1</sap.version>
</properties>
<packaging>pom</packaging>
In a nutshell, I'm looping through these three items (cecore,celib, and cesession) to perform the actual installation.
I would like to then add these three items as dependencies, but since these won't exist before the package phase, Maven complains about that.
Ideally, I would like to instruct Maven to resolve the dependencies after the packaging, or to instruct Maven to trust that the dependencies will in fact become available.
Any ideas/suggestions?
Thanks!
Eric

How to expose a dependency of a dependency in a maven project

I have a maven web application that is using a jar I created and is referenced as a dependency in my pom file.
The jar file I created has another jar file as a dependency.
When I deploy the web application, it says that a class in the dependency of the jar file I created cannot be found.
The only way to work around this was to add the dependency of the jar file as a dependency of the war file. This seems unnecessary.
Is there a way I can configure the war file to be able to see the classes defined in a dependency of a dependent jar file?
my jar pom file looks like: The dependency that has the class that can't be found is the the QRS one.
<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>group</groupId>
<artifactId>artifact</artifactId>
<packaging>jar</packaging>
<version>1.0.7</version>
<name>name</name>
<dependencies>
<dependency>
<groupId>group2</groupId>
<artifactId>QRS</artifactId>
<version>8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
my war pom looks like:
<dependency>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.0.7</version>
</dependency>
and the war plug in is configured:
<plugin>
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<warName>${project.name}-${project.version}-${env}</warName>
<webResources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<include>jboss-web.xml</include>
<include>web.xml</include>
<targetPath>/WEB-INF</targetPath>
</resource>
</webResources>
<warSourceExcludes>**/toAggregateAndRemove/**</warSourceExcludes>
<goal>war:manifest</goal>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
Maven already resolves transitive dependencies.
That it isn't indicates you're likely not doing it right: the other jar you've created should be a Maven project with its own pom, installed at least locally via mvn install, and listed as a dependency in the second project (the one that uses it).
The project using the jar only needs to specify that artifact as its dependency, the other project's dependencies will be transitively determined and included as a project dependency.
#Dave Newton has answered, this should happen by default.
A few observations from the snippets in the question - if that helps...
Packaging dependant jars as a folder of artifact project's jar.
Defining the maven war plugin jar itself as a dependency for maven war plugin - this is not required at all!
war:manifest goal in the maven war plugin configuration.
It is possible that the war:war which actually creates the war is not run.

maven embed dependency artifact code in jar

I have a multi-module maven project. I have a main "base-code" module which creates a jar of all the compiled source code in my project.
I have another module, "executable", which creates an executable jar from the same source code. To avoid duplication I want to pull the classes in from the "base-code" module.
I thought that all I had to do was make the "base-code" module a dependency of the "executable" module to do this. But I just get an empty jar. What am I doing wrong?
(my "executable" pom is below)
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.myproject/groupId>
<artifactId>myproject</artifactId>
<version>1</version>
</parent>
<artifactId>executable</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.myproject/groupId>
<artifactId>code-base</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>runnable</finalName>
<archive>
<manifest>
<mainClass>com.myproject.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
What you are looking for is probably uber-jar: a single jar file with all embedded jar dependencies, the newly version of maven-assembly-plugin support this as one of the 4 pre-defined descriptor, check out here.
Try using maven-assembly-plugin replace your maven-jar-plugin like this:
<!-- Create single executable jar with all dependencies unpacked and embedded -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.myproject.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals><goal>single</goal></goals>
</execution>
</executions>
</plugin>
Alternatively, you can also use maven-shade-plugin to do this.
Hope that helps.

Resources