Maven Tutorial - How To Install Your Project Into Maven Local Repository








When building software we often have to create some library to gather all functions and classes in common together and put that compiled library file to build path, so that the compiler can find the library when compiling the code.

In Java this usually means creating a jar file which has the library classes.

In Maven, we can use "mvn install" to package the project and deploy to our local repository automatically.

When "install" phase is executed, all of the phases before it, for example, "validate", "compile", "test", "package", "integration-test", "verify" phase , phase will be executed. After those phases install phase will be executed.





mvn install example

The following code shows a POM file generated from maven.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.java2s.ide</groupId>
  <artifactId>xmlFileEditor</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>xmlFileEditor</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Based on above pom.xml file, when "mvn install" is executed, it will package the project into jar file and copy to your local repository.

It's always recommended to run "clean" and "install" together, so that you are always deploy the latest project to your local repository.

mvn clean install

The command above generates the following result.


c:\mvn_test\xmlFileEditor>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ xmlFileEditor ---
[INFO] Deleting c:\mvn_test\xmlFileEditor\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to c:\mvn_test\xmlFileEditor\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 3 source files to c:\mvn_test\xmlFileEditor\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Surefire report directory: c:\mvn_test\xmlFileEditor\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.java2s.ide.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.java2s.ide.TestApp1
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.java2s.ide.TestApp2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ xmlFileEditor ---
[INFO] Building jar: c:\mvn_test\xmlFileEditor\target\xmlFileEditor-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ xmlFileEditor ---
[INFO] Installing c:\mvn_test\xmlFileEditor\target\xmlFileEditor-1.0-SNAPSHOT.jar to C:\Users\abc\.m2\repository\com\java2s\ide\xmlFileEditor\1.0-SNAPSHOT\xmlFileEditor-1.0-SNAPSHOT.jar
[INFO] Installing c:\mvn_test\xmlFileEditor\pom.xml to C:\Users\abc\.m2\repository\com\java2s\ide\xmlFileEditor\1.0-SNAPSHOT\xmlFileEditor-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.250 s
[INFO] Finished at: 2014-11-22T10:27:10-08:00
[INFO] Final Memory: 27M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>

We can view the installed jar file in our local maven repository.

null




Access Deployed Project

After installing the jar file to local repository we access our deployed "jar" file by declaring below dependency tag in their pom.xml file.

 

<dependency>
      <groupId>com.java2s.ide</groupId>
      <artifactId>xmlFileEditor</artifactId>
      <version>1.0-SNAPSHOT</version>
</dependency>