2016年8月4日 星期四

Maven command for extracting Maven Dependencies (3rd party libraries / jar files) externally


Although Gradle is becoming more common nowadays, Maven is still very popular and widely used. I believe many developers should be using the combination of Eclipse + Maven for convenience of their development and system build.


Commands for merely project build (i.e. clean package), or saving the built project to the local repository (i.e. clean install) should be very familiar to most of the developers as both should be used frequently from day to day. 

If you are building a j2ee project (i.e. the target output may be a .war or .ear file), there should have no special concerns as all dependent 3rd party libraries could be included to the exported package. All you need to do is merely deploy the exported .war / .ear to a web container (e.g. tomcat).



However, if you are building a core java project (i.e. the target output is a .jar file), the dependent 3rd party libraries will not be included in the exported package. As the exported package (i.e. the .jar file) still need to refer to the 3rd party library files (if any) for execution successful, it is necessary to collect / extract the Maven dependent libraries externally. For doing the extraction, is it the only way that jumping into the repository directory and the get the jar files one by one as shown below? 

Figure 1 Copy the path of the jar location


Figure 2 Copy and extract the jar from the location




Actually, there is another commands could help you to achieve this purpose. This is:
clean dependency:copy-dependencies


Figure 3 the target folder contains the exported jar of the project originally



Figure 4 Use the command of "clean dependency:copy-dependencies"


Figure 5 the target folder now contains all the Maven Dependencies which could be copies in one go





In conclusion, the commonly used maven commands are summarized below for reference convenience:
No.
Command
Purpose
1
clean package
1)    clean the target folder;
2)    then build and export the project base on pom.xml configuration to the target folder
2
clean install
1)    clean the target folder;
2)    then build and export the project base on pom.xml configuration to the target folder;
3)    also install the built project to the local repository (e.g. ../.m2/respository/.. ) which could be referenced by other projects easily
3
clean dependency:copy-dependencies
1)    clean the target folder;
2)    then download / extract the Maven Dependencies jar files to the target folder.

Hope you enjoy this article and it is helpful to you.

沒有留言:

張貼留言