Maven Project hit jar package, how to add dependencies

Source: Internet
Author: User
Tags unpack

The previous introduction of using the Spring-boot-maven-plugin plugin to hit the jar package will import all the dependent files and then become an executable jar package. The downside of this is that I don't actually need to break the dependent jar into the generated jar package, just package the code for the current project. The generated jar package can be executed as long as it can refer to the jar package it depends on.

So in general, how do you add dependencies when packaging?
Using the maven-dependency-plugin plugin
In the Pom.xml file in the subproject, add the following maven-dependency-plugin dependencies, the role of this maven-dependency-plugin dependency is to automatically copy the jar package to the target directory

This is my Maven subproject package, which specifies that the jar packages in its dependent parent project be copied to the Lib directory under target

1 <plugin>2     <groupId>Org.apache.maven.plugins</groupId>3     <Artifactid>Maven-dependency-plugin</Artifactid>4     <executions>5         <Execution>6             <ID>Copy-dependencies</ID>7             <Phase>Prepare-package</Phase>8             <Goals>9                 <goal>Copy-dependencies</goal>Ten             </Goals> One             <Configuration> A                 <!--${project.build.directory} is a maven built-in variable, default is target - -                 <outputdirectory>${project.build.directory}/lib</outputdirectory> -                 <overwritereleases>True</overwritereleases> the                 <Overwritesnapshots>True</Overwritesnapshots> -                 <Overwriteifnewer>True</Overwriteifnewer> -             </Configuration> -         </Execution> +     </executions> - </plugin>

About Maven-dependency-plugin: Some of the most used operations: Copy, copy-dependencies and their corresponding unpack, unpack-dependencies
Description: The copy and unpack operations are performed by a package to be copied, the package needs to specify which package to copy, which is not related to the current project dependencies. The difference between the two--whether decompression
Copy-dependencies and Unpack-dependencies, but it is used to copy the current project's dependency package. The difference between the two--whether decompression

  The copy operation can be used to copy a (some) Maven artifact (s) to a directory. Add phase and goal as follows:

1     <Build>2         <Plugins>3             <plugin>4                 <groupId>Org.apache.maven.plugins</groupId>5                 <Artifactid>Maven-dependency-plugin</Artifactid>6                 <version>2.8</version>7                 <executions>8                     <Execution>9                         <Phase>Package</Phase>Ten                         <Goals> One                             <goal>Copy</goal> A                         </Goals> -                     </Execution> -                 </executions> the             </plugin> -         </Plugins> -     </Build>

Then is the configuration, copy can be configured to more than the number of items, please refer to: Copy configuration. Here are some common item descriptions:

Artifactitems: Specifies the jar package to be copied
Prependgroupid: Used to indicate the name of the library to be copied out need not add GroupID, default is not added
OutputDirectory: Used to specify the location of the libraries after the copy is placed


In addition to the artifactitems, there are no default values that need to be specified, and all other options can be ignored:

1     <Configuration>2         <Artifactitems>3             <Artifactitem>4                 <groupId>Junit</groupId>5                 <Artifactid>Junit</Artifactid>6                 <version>4.11</version>7             </Artifactitem>8         </Artifactitems>9     </Configuration>

The above configuration will copy the JUnit package to the Target/dependency directory with the file name: Junit-4.11.jar.

  unpack is similar to copy, except that it unlocks the copied bag. After JUnit has been copied, it is no longer the jar package or the contents of the jar package placed under Lib and lib2.

  copy-dependencies and unpack-dependencies
The copy and unpack operations described above are made by copying a package, which needs to specify which package to copy, which is not related to the current project's dependencies. Copy-dependencies is a bit similar to it, but it is used to copy the current project's dependency package, typically, for example, we have a Web application, and when it comes to a war package, all its dependencies need to be hit into the application.

Copy-dependencies parameters are many, detailed can be consulted: Copy-dependencies Doc, but almost all have default values. So one of the simplest definitions is as follows:

1 <plugin>2         <groupId>Org.apache.maven.plugins</groupId>3         <Artifactid>Maven-dependency-plugin</Artifactid>4         <version>2.8</version>5         <executions>6             <Execution>7                 <Phase>Package</Phase>8                 <Goals>9                     <goal>Copy-dependencies</goal>Ten                 </Goals> One             </Execution> A         </executions> -     </plugin>

No configuration is specified here, all parameters are default, and all dependencies (direct, indirect) of the current project will be copied to the Target/dependency directory.
You can also use OutputDirectory to specify the storage in the. In addition, the following parameters can control which dependencies will be copied out (or excluded):
Excludeartifactids
Excludeclassifiers
Excludegroupids
Excludescope
Excludetransitive
Excludetypes
Includeartifactids
Includeclassifiers
Includegroupids
Includescope
Includetypes

Maven Project hit jar package, how to add dependencies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.