Maven assembly: placing project jar in bin folder - maven

We have a deploy.xml file which is referenced in by the maven-assembly-plugin section in the pom.xml.
When mvn package -DskipTests -Pdeploy is run from the command line, it creates a zip, within which all the dependency jars are placed in the lib folder. It also puts the project jar itself (with the code and configuration specific to our application) into the lib.
We would like the project jar to be placed in the bin folder. (Have been told that that is the standard, though not sure if that is true). How do we modify our configuration to do that?
This is the relevant configuration we have in deploy.xml right now:
<assembly>
<fileSets>
<fileSet>
<directory>bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
</fileSets>
...
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*:*</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
Thanks

You need two dependency sets, one including the project artifact, the other including your dependencies:
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
<dependencySet>
<outputDirectory>bin</outputDirectory>
<includes>
<include>${yourgroupid}:${yourartifactid}:jar</include>
</includes>
<useProjectArtifact>true</useProjectArtifact>
</dependencySet>
</dependencySets>

Related

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

maven-assembly-plugin include *.properties at root level of zip

I'm trying to get maven to include my *.properties files when it zips up my artifacts. They are located inside src/main/resources. I tried adding the fileSet element to my assembly file, but the resources are not being included in the zip. I saw this question which seems to indicate that adding fileSet should work.
plugins.xml:
<?xml version="1.0"?>
<assembly>
<id>release</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.properties</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<useTransitiveFiltering>true</useTransitiveFiltering>
</dependencySet>
</dependencySets>
</assembly>
The properties that you want to include inside your ZIP are located in the src/main/resources source directory of your project. So the <fileSet> element should point to this directory.
${project.build.directory} is Maven current build directory, which is by default target. You could also point to the temporary directory where Maven copies all resources during the build but it is preferable to stick with the permanent data where possible.
As such, you just need to change your <fileSet> element with:
<directory>src/main/resources</directory>

Exclude 3rd party dependency from target single jar in maven assembly

I am using maven-assembly-plugin to make a single jar from maven modules.
Here is the assembly.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>all-jar</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpackOptions>
<excludes>
<exclude>**/resources</exclude>
<exclude>xsom:xsom</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
I am using a 3rd part jar in one of my modules. Although i put in the exclude list, this third party jar is also unpacked and isnt excluded. How can i exclude it from unpacking? In fact i only want the resource folders of the modules to be unpacked , other java sources could be stay packed.
You need to put the exclude tag outside of unpack options:
<dependencySets>
<dependencySet>
<unpack>true</unpack>
<useTransitiveDependencies>false</useTransitiveDependencies>
<excludes>
<exclude>xsom:xsom</exclude>
</excludes>
<unpackOptions>
<excludes>
<exclude>**/resources</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
If you use exclude inside unpackOptions, you exclude contents from being unpacked.

Speeding up maven assembly of multiple modules

I'm having a project of the following form
- pom.xml
- projectA
- pom.xml
- src/main/
- java
- startupScript
- projectB
- pom.xml
- src/main/
- java
- startupScript
- projectAssembly
- pom.xml
I want projectAssembly to produce a tar.gz that would contain two folders one for projectA and one for projectB, in each folder, there would be project's dependencies and the startupScript library.
The "naive" way to do that is to add assembly.xml file to each project, a file which roughly looks like:
<assembly>
<formats>
<format>tar.gz</format>
</formats>
<baseDirectory>/${project.artifactId}</baseDirectory>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/startupScripts</directory>
<outputDirectory>/startupScripts</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
Then, in the projectAssembly, depend on <type>tar.gz</type> of both projectA and projectB, and add an assembly file which roughly looks like
<assembly>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
This works, however, I do not need the intermediate tar.gz of projects A and B, and producing them, especially if they have a lot of dependencies, takes a long time.
How can I tell maven to directly assembly only the tar.gz of projectAssembly without wasting times on packing and unpacking intermediate archives?
Your assembly descriptor in the projectAssembly needs to look something like this (See comments inside):
<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>bin</id>
<formats>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<!-- Enable access to all projects in the current multimodule build! -->
<useAllReactorProjects>true</useAllReactorProjects>
<!-- Now, select which projects to include in this module-set. -->
<includes>
<include>*:projectA</include>
<include>*:projectB</include>
</includes>
<!-- Select and map resources from each module -->
<sources>
<includeModuleDirectory>false</includeModuleDirectory>
<fileSets>
<fileSet>
<directory>src/main/startupScript</directory>
<outputDirectory>${module.artifactId}/startupScript</outputDirectory>
</fileSet>
</fileSets>
</sources>
<!-- Select and map dependencies from each module -->
<binaries>
<dependencySets>
<dependencySet>
<outputDirectory>${module.artifactId}/lib</outputDirectory>
</dependencySet>
</dependencySets>
</binaries>
</moduleSet>
</moduleSets>
</assembly>
I believe that would be what you need. If not let us know..

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