Can Maven package a WAR as .tar.gz? - maven

I'm building a WAR application in Maven and I need to deploy it to Docker. The issue is that docker doesn't support the zip format (but can use a .tar.gz or .tar.xz).
So, how can I package my mywebapp-1.0.0.war as mywebapp-1.0.0.tar.gz?
We have this requirement so it would become easier to perform the hardening steps for the docker image.
I tried using the Assembly plugin and it produces a target/mywebapp-1.0.0.tar.gz. However, it ends up placing the whole WAR file inside a .tar.gz file, and NOT its contents. Here's my assembly.xml file:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
<format>tar.gz</format>
</formats>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<includes>
<include>target/mywebapp-1.0.0.war</include>
</includes>
</fileSet>
</fileSets>
</assembly>

Although I find the requirement strange (the application server should unpack your war, not you), I guess it is possible by tarring the directory in target that is used to build the war.
Before the package step, all the files for the war are gathered in a directory just below target (I forgot the name, but it should be easy to reverse engineer) - just pack that directory by using the assembly plugin.
Edit by the OP:
I wanted to accept this answer but I also wanted it to be useful to someone else so I added the working solution (assembly.xml file). Here it is:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>docker</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/${project.build.finalName}</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
This solution generates the file target/mywebapp-1.0.0-docker.tar.gz in addition to the typical war file.
Notice it added the docker classifier.

Related

Maven, exclude all classes but one under a package

This may sound silly, but I really want to know if I can do that (by adding some 'magic' configuration in pom.xml).
Let's say, I have a project which has a bunch of packages, one of them, say 'com.foo.bar', has quite a few .java files, including one named 'Dummy.java'.
Now, when generating the jar file, I want to exclude all classes under com.foo.bar, except the 'Dummy'.
I tried regular expression in a section, but no luck.
Is there an easy way to go? Many thanks.
With a "sledge hammer" (assuming Dummy.class not .java):
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>com/foo/bar/Dummy.class</include>
</includes>
<!-- alternatively(!) excludes/exclude* -->
</configuration>
</plugin>
... or with a "Swiss army knife": maven-assembly-plugin ... ^^!"%/"))
...
To use the Assembly Plugin in Maven, you simply need to:
choose or write the assembly descriptor to use,
configure the Assembly Plugin in your project's pom.xml,
and
run "mvn assembly:single" on your project.
...
with an assembly descriptor like:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>dummy-only</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<outputDirectory>/</outputDirectory>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>com/foo/bar/Dummy.class</exclude>
</includes>
</fileSet>
</fileSets>
</assembly>
see also: I wish to exclude some class files from my jar. I am using maven-assembly-plugin. It still adds the files. I dont get any error

Maven: add non-JAR dependency

I'am using the maven-assembly-plugin to create a tar.gz archive. Now I need to include files in that archive. These are just a bunch of plain old text files and are a dependency at runtime, not at compile or (unit) test time.
I thought about adding a maven dependency on that but these files are not stored in a maven repository but in a simple remove folder. To make it even worse, this folder is password protected. I could, in a pre-build-step, download these password-protected files into a local folder. But then there is still the problem of using those files in the assembly. Also, this is extra work I want to avoid.
Is there any way to do that?
If you can download those files, workaround password. Maybe this will help.
Create mod assembly project where you put something like that:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>${artifact.version}</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<excludes>
<exclude>${project.groupId}:*:*</exclude>
</excludes>
<outputFileNameMapping>project${timestamp}_code-${artifact.name}.${artifact.extension}</outputFileNameMapping>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<includes>
<include>readme.txt</include> <!-- here's that plain file -->
</includes>
</fileSet>
</fileSets>
You can include those files in fileSets.
http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html for more info

Create a .gz file using maven

Maven assembly doesn't seem to support gz format. http://maven.apache.org/plugins/maven-assembly-plugin/
I can't change to use tar.gz or something else as .gz has been hard coded in some scripts that I don't have permissions to update.
Any idea would be much appreciated?
Are you sure it doesn't support tar.gz?
I think if you do this:
<assembly>
<id>bundle</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/output</directory>
<outputDirectory>output</outputDirectory>
</fileSet>
</fileSets>
</assembly>
Then if you do this: mvn clean assembly:assembly then Maven should create a tar.gz file.
What is the result you got?

instructing maven assembly plugin to unpack thirdparty jar which is not defined as dependency in pom.xml

Using maven-assembly-plugin I am able to create a *.jar with my project specific requirements. It unpacks all dependencies and transitive dependencies. But along with that I have a third party jar which is not there in maven repository. That is referenced locally in my project. Is there a way to instruct maven-assembly-plugin to unpack given jar file as well?
my jar creating assembly descriptor is:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>packing-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}/com</directory>
<outputDirectory>/com</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
As my requirement needs to unpack only one third party jar I tried a turnaround for that. Used maven install to install that jar locally. So, as that jar is now part of maven dependencies that assembly plugin will unpack it.

Create maven uberjar without including an internal pom file

I have a maven build setup but it has been requested that I make one change to the uberjar and that is to not include the pom file, because it messes up my clients subsequent build that uses my uberjar as a dependancy. I know this is not the way to do this but my client cannot access my central repo because of security issues. So the uberjar was settled on as a deployment method. Now the question is this: Is there a way to not include the pom file in the creation of a jar via maven?
Thanks,
Blair
I assume you are using the maven-assembly-plugin to build your uberjar and using an assembly descriptor. What you want to do is instruct the assembly plugin (via the assembly descriptor) to exclude any files named "pom.xml". For example:
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/pom.xml</exclude>
</excludes>
</unpackOptions>
<scope>runtime</scope>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
The key here is the "unpackOptions" section and the exclusion contained therein.
Hope that helps, if so please remember to mark my answer as correct so I get the reputation points.

Resources