Thursday, October 30, 2014

How to include your custom library into Maven project on Eclipse?

Suppose you have a custom library, say, mylib.jar, in your HOME directory, and you have a Maven project on Eclipse. This posting helps you on how to include the library into the Maven project using Eclipse. It involves two steps.

  1. Add the library into your local Maven repository (usually, ~/.m2/repository/)
  2. Add a dependency on the library to your project (pom.xml)


[1st step]

In a command-line shell (such as bash), you may run the following command,

    $ mvn install:install-file -Dfile=$HOME/mylib.jar -DgroupId=org.mylib -DartifactId=mylib -Dversion=1.0 -Dpackaging=jar


In Eclipse, you select your Maven project with right-click, and then

    Run As -> Run Configuration -> Maven Build    to create a new configuration with

           Name: my-library
           Base directory: ${workspace_loc:/my-maven-project}
           Goals: install:install-file
           Paramenter Name & Value:
              file           $HOME/mylib.jar    ---> Probably $HOME must be replaced with some path.
              groupId    org.mylib
              artifactId   mylib
              version      1.0
              packaging  jar

After filling the fields as above, you enter Apply -> Run to execute the maven command. The console window will show the result of the installation.

Please keep in mind that you must run this configuration whenever your library has been changed. 



[2nd step]

In a command-line shell or in Eclipse, you may directly edit the pom.xml of your Maven project by adding

     <dependency>
           <groupId> org.mylib </groupId>
           <artifactId> mylib </artifactid>
           <version> 1.0 </version>
     </dependency>

after the list of dependencies that must be seen in pom.xml.


Also, in Eclipse, you select the pom.xml with right-click, and then

    Maven -> Add Dependency     to enter the dependency on the library as

             Group Id: org.mylib
             Artifactid: mylib
             Version 1.0

After filling the fields as above, you enter OK to find the entered text in the pom.xml.



Reference
 - How to include Custom Library into maven Local Repository, http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/