Maven Learning Series 7----package dependent files into jars

Source: Internet
Author: User
Tags log4j

When you publish your project, you may have the requirement that a jar package contains your own research code, as well as a dependent library (for example, after a log4j jar package), and there are two ways to do that when MAVEN packs. 1, using the Maven-assembly-plugin plugin

The Pom.xml configuration is as follows

         <build> <plugins> <plugin> &LT;ARTIFACTID&GT;MAVEN-COMPILER-PLUGIN&LT;/ARTIFACTID&G  
			    T  
			        <configuration> <source>1.7</source> <target>1.7</target> 
	        <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuratio
		          N> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef>  
                            </descriptorRefs> <archive> <manifest>
                   <mainClass>com.itsenlin.Helloworld</mainClass> </manifest>
		            </archive> </configuration> <executions> <execution><id>make-assembly</id> <!--This is used for inheritance merges--> &LT;PHASE&GT;PACKAGE&L T;/phase> <!--bind to the packaging phase--> <goals> <goal>single< /goal> </goals> </execution> </executions> &LT;/PLUGIN&G
		T </plugins> </build>

Description

1,assembly plug-in function is powerful, not only can play jar package can also play zip, war all kinds of bags. 2, the name of the jar package will be-with-dependencies end of 3, this way in the jar package, the classes file of the dependent package and this project classes file together into a jar package, As shown in the following illustration (the COM directory is the classes file for the project code, the org directory is the classes directory where the org.apache.log4j widget is introduced, and if other artifacts are dependent on the other Classes file directory)


2, copy the dependent jar package to the target/classes directory

Because MAVEN defaults to a jar package for all the files in the Target/classes directory, we can configure the dependent artifacts to be copied to the Target/classes directory at compile time, and note that this method requires configuration of classpath. The Pom.xml configuration is as follows:

<build> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifacti d> <configuration> <source>1.7</source> <target& Gt;1.7</target> <encoding>${project.build.sourceEncoding}</encoding> ;/configuration> </plugin> <plugin> <artifactId>maven-jar-plugin< /artifactid> <configuration> <archive> <manifest&gt  
                        ; <!--told Maven-jar-plugin to add a class-path element to MANIFEST.  
                        MF file, and include all dependencies in the Class-path element--> <addClasspath>true</addClasspath>  <!--all dependencies should be located in the Lib folder--> <classpathPrefix>lib/</classpathPrefix> </span> <!--When a user executes a jar using the Lib commandFile, use this element to define the class name that will be executed--> <mainClass>com.itsenlin.Helloworld</mainClass>
        
        </manifest> </archive> </configuration> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <exe
                    Cutions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-d
                        Ependencies</goal> </goals> <configuration> <!--${project.build.directory} is a built-in variable for MAVEN and the default is target--> &LT;OUTPUTDIRECTORY&GT;${PROJEC T.build.directory}/classes/lib</outputdirectory></strong></span> <!--indicate whether or not to include
         Indirect-dependent Package-->               <excludeTransitive>false</excludeTransitive> <!--indicates that the copied jar file removes version information-
                -> <stripVersion>true</stripVersion> </configuration> </execution> </executions> </plugin> </plugins> &LT;/BUILD&G T
Description

1, this method and do not rely on the configuration of the jar package is the difference is the location of the copy dependent packet changes, see the red section above 2,jar Packet decompression after the following image


3, this way the current test found a not very good place, it will also copy the scope test package to the jar package, although it has no effect on the function, but will increase the size of the entire jar package, and add a useless jar package, the following picture of JUnit, currently not found why, Find out why and then add the relevant content (there are known brothers can also be informed, thank you:)).


Reference:

Http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Resources

http://blog.csdn.net/laiahu/article/details/7105277


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.