Maven multi-module project - copying all dependencies into a single tar.gz - maven

I'm looking to pull all of the dependencies from each module of my maven project and stick them into a single tar.gz file using the maven-assembly-plugin.
I currently have a pom setup as a parent to all of the modules. My idea was to use the maven-dependency-plugin to copy all of the dependencies to a single folder in the parents directory however when i use
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
The dependencies are copied into the modules build directory. Is there any way to set this output directory to one inside the parent project?

You are going at it the wrong way.
Instead of creating this archive inside the parent POM, you should create another module which will be responsible for creating it. This new module will have dependencies on all the other modules.
To create the archive, you need to use the maven-assembly-plugin. I strongly suggest that you read Chapter 8. Maven Assemblies of the Maven book to get you started with using assemblies. Basically, an assembly is created with the help of an assembly descriptor. In your case, you will need to configure this descriptor to use the tar.gz format.

Related

Why doesn't the dependency contain a jar in the maven public repository https://mvnrepository.com?

I a maven rookie and am wondering how to get a binary jar file if it is not already in the repo. Specifically i'm in need of: jackson-dataformats-text-2.13.0.jar. Do I need to build it myself? I'm used to creating a project and marking a library as a dependency and seeing the jar downloaded into my .m2 cache but all i see in my cache is:
jchan#jchan-Z170N:~/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformats-text/2.13.0$ ls
jackson-dataformats-text-2.13.0.jar.lastUpdated jackson-dataformats-text-2.13.0.pom.sha1
jackson-dataformats-text-2.13.0.pom _remote.repositories
Can someone advise how I am to get a built version of the jar from maven central?
We are still maintaining our ant build and I need the jar file for this. (i know i know, ancient stuff but team is not ready to port just yet).
parent pom don't contain jar file
This is the reason why no bundle link is present on the official public maven repository https://mvnrepository.com
The maven dependency is not a jar, is a parent. So the extension is: .pom which is just a plain pom.xml
Parent dependencies don't contain compiled class like .jar.
In your specific case, there are another dependencies who contains jars:
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.13.0/
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformat-xml/2.13.0/
advice
Check what classes do you need on your ant project and search if exist a jar (with the classes you need) on https://mvnrepository.com
Another option is to get all the dependencies from pom :
https://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/jackson-dataformats-text/2.9.0/jackson-dataformats-text-2.9.0.pom and download them into your ant project. In theory is the same of add the parent pom in a maven project

Maven: Get artifact list of sub modules

I have a root parent maven module which has lot of sub-modules that build jar files as artifacts.
I cannot change all sub-module pom.xml files.
From the root parent pom.xml is there a way I can get a list of all jars (artifacts) built by sub-modules?
Preferably after the package phase is complete?
PS: As a part of root module build I want to generate a report using a tool which requires this list of jar files.
There's a target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst in a Maven project.
You can use the GMavenPlus Plugin and Walking the File Tree to gather these files and put their content where you want.
Another option is to develop an own Maven plugin that does the same.

How to include dependency files in maven assembly

I have a maven project which is configured (via the use of pom.xml and assembly.xml) to package a zip file containing both the compiled webapp (war file) and all files under src/main/folder alongside it whenever we run mvn clean package assembly:single.
This project imports/uses another maven project (which becomes a jar file) and that project in turn also imports/uses a third maven project (again a jar file).
The third project also contains some files inside src/main/folder as well which I'd like to be placed alongside the other files in the zip file whenever I build the main project (the one on top of the chain that becomes a war file).
Is it possible somehow to tell maven to include all files in a specific folder inside all it's dependencies (Or in one specific dependency) alongside in the zip file?
Start here: Maven: The Complete Reference - 8.5. Controlling the Contents of an Assembly
Note that if you deploy a Maven project with assembly:assembly (which you really should configure as a <build> plugin, not use on the command line), the assemblies get "attached" to the project and installed in the repository. You can then reach them using the Maven dependency notation, i.e. mygroup:myartifact:extrafiles:zip
I've found the Assembly descriptor <dependencySets> cumbersome to configure. They really are for the simple case (including the JAR files your application needs at runtime).
Here is the general approach I would take for your desired outcome:
Use Maven Dependency Plugin dependency:copy to download the specific files from the other projects, placing them under a sub-directory of target/
Configure your assembly descriptor to add those downloaded/extracted files to your final assembly artifact.
Alternatively, hack something together using Maven Ant Run Plugin.
You will need to produce an additional assembly from the third project to package up the extra files. Append the assembly id so it produces a package named something like third-1.0.0-extrafiles.zip
Then add this as a dependency of your first project using <type>extrafiles</type> in the dependency descriptor. In the assembly for the first project you'll have to tell it to "unpack" the dependencies of this type (so you don't get a zip in a zip)

Maven Assembly plugin. Any way to unzip/untar an archive straight from the descriptor file, rather than relying on antrun plugin?

I'm using maven assembly plugin version 2.2.1 to build a package in a custom, proprietary format.
As a part of this process, I need to extract a bunch of external archives (by external I mean they are not listed as maven dependencies, but they are arbitrary files on the filesystem).
Do you know if maven assembly is able to do that ? I've looked and apparently, it is able to extract a bunch of dependencies in an arbitrary folder, but it is unable to extract an external archive.
Using the <unpack>true</unpack> in your <dependencySet> is the way to go.
External archives are not supported by Maven. So you will need to attach them using, e.g. build-helper:attach-artifact then you can reference it via the <dependencySet>
Update
One of the comments wanted to know how you could avoid installing the attached artifact into the maven repository (local/remote)
The solution to that is to use a dummy module that is a non-transitive dependency.
We are relying on the assembly being built not being used as a transitive dependency.
So you start with a dummy module, which will look a little something like
<project>
...
<packaging>pom</packaging>
...
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<maven.install.skip>true</maven.install.skip>
<!-- or you could override the plugin configuration for a safer - but less quick - solution -->
</properties>
...
</project>
That dummy module can attach the files using build-helper:attach-artifacts
The your assembly module will just list the dependencies with <scope>provided</scope> and <optional>true</optional> as a guard against becoming transitive dependencies. (not necessary if the assembly is say the installer bundle and will not be consumed by other Maven builds.
And there you go, the compressed content will be streamed from one archive to the other and the raw file will not be copied to the remote repo... but you will always need to build the hack module in any reactor that builds the assembly.
"arbitrary files on the filesystem" does not mean much, from maven point of view.
You should be able to run your maven pom from any point "your machine", "the integration server (jenkins...)". Ant is a tool for "home made" build, maven promotes convention over configuration, and reusability.
Therefore, any resources your project depend on should be either a dependency, or somehow included in your project .
I expect all my projects to be buildable as id, with a simple svn check out. Anything that is not in the project should be downloaded from mvn repositories.
So When I have to write an assembly project, I make sure everything is available in the project, as resources. If not, you can always build a simple project/pom for shared resources, or use some special plugins that do that for you.

Maven build - include classes in projects /src/main/webapp/WEB-INF/classes

We have to build ontop of an application that we did not develop and we need to include some classes from the Jar's WEB-INF directory. How do we get maven to do this? The eclipse deployment directory includes this for local deploy but the built war looks different and does not include the files we place in the source.
You can use the dependency plugin unpack dependency option to unpack from a dependency and output it to a location you desired.
http://maven.apache.org/plugins/maven-dependency-plugin/unpack-dependencies-mojo.html
Or alternatively, you can use the maven assembly plugin's unpack describtor to achieve the same thing.

Resources