Maven Include Dependencies in JAR Artifact: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Maven_jar_Plugin#Include_Dependencies_-_or_Some_of_the_Dependencies_-_in_JAR|Maven jar Plugin]]
* [[Maven_jar_Plugin#Include_Dependencies_-_or_Some_of_the_Dependencies_-_in_JAR|Maven jar Plugin]]
* [[Maven_dependency_Plugin#Include_Dependencies_-_or_Some_of_the_Dependencies_-_in_JAR|Maven dependency Plugin]]


=Overview=
=Overview=


There are situations when we want to extract some of the dependencies and bundle them with the JAR artifact. This is done using the jar plugin and the dependency plugin together, as described below.
There are situations when we want to extract some of the dependencies and bundle them with the JAR artifact, merged with the artifact classes. This is done using the jar plugin and the dependency plugin together, as described below.
 
Configure the Maven dependency plugin to unpack dependencies, or just some of them, in the directory that will be archived by the jar plugin:
 
<pre>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>unpack-some-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                <includes>**/SimpleStateless.class</includes>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
</pre>
 
Essential bits of configuration are the following:
 
* the "unpack-dependencies" goal, which tells the plugin the extract classes from the transitive dependency tree.
* outputDirectory "${project.build.directory}/classes" is the directory picked up by the jar plugin.
* <includes>**/SimpleStateless.class</includes> the filter that selects the classes we want in the JAR artifact.
 
=Project's Own Resources Overwriting Problem=
 
The dependency plugin usually executes at "[[Maven_Concepts_-_Lifecycle#prepare-package|prepare-package]]" phase, and thus it may overwrite project's own resources with the same name, which are copied in the same output directory by the resources plugin, at the earlier phases "[[Maven_Concepts_-_Lifecycle#process-resources|process-resources]]" and "[[Maven_Concepts_-_Lifecycle#process-test-resources|process-test-resources]]". This issue can be fixed by explicitly declaring the [[Maven_Resources_Plugin|resources plugin]] ''after'' the dependency plugin in the POM file, and configure it to execute at the "[[Maven_Concepts_-_Lifecycle#prepare-package|prepare-package]]" phase for the conflicting project resources we want in the final artifact. The configuration of the resource plugin to achieve this is describe here:
 
{{Internal|Maven_Resources_Plugin#Copy_Resources_at_an_Arbitrary_Phase|Maven Resources Plugin - Copy Resources at an Arbitrary Phase}}

Latest revision as of 19:20, 2 March 2018

Internal

Overview

There are situations when we want to extract some of the dependencies and bundle them with the JAR artifact, merged with the artifact classes. This is done using the jar plugin and the dependency plugin together, as described below.

Configure the Maven dependency plugin to unpack dependencies, or just some of them, in the directory that will be archived by the jar plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>unpack-some-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/classes</outputDirectory>
                <includes>**/SimpleStateless.class</includes>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
             </configuration>
         </execution>
    </executions>
</plugin>

Essential bits of configuration are the following:

  • the "unpack-dependencies" goal, which tells the plugin the extract classes from the transitive dependency tree.
  • outputDirectory "${project.build.directory}/classes" is the directory picked up by the jar plugin.
  • <includes>**/SimpleStateless.class</includes> the filter that selects the classes we want in the JAR artifact.

Project's Own Resources Overwriting Problem

The dependency plugin usually executes at "prepare-package" phase, and thus it may overwrite project's own resources with the same name, which are copied in the same output directory by the resources plugin, at the earlier phases "process-resources" and "process-test-resources". This issue can be fixed by explicitly declaring the resources plugin after the dependency plugin in the POM file, and configure it to execute at the "prepare-package" phase for the conflicting project resources we want in the final artifact. The configuration of the resource plugin to achieve this is describe here:

Maven Resources Plugin - Copy Resources at an Arbitrary Phase