How to install your project into Maven local repository example
In this tutorial, we will show you an easy way to install your project into your Maven’s local repository, using Apache Maven. Maven is able to build, package and deploy a project to our local repository.
First of all, we must understand how Maven builds and deploys a project. Each Maven project undergoes a specific build lifecycle. The default lifecycle has the following build phases:
1. validate | 5. integration-test |
2. compile | 6. verify |
3. test | 7. install |
4. package | 8. deploy |
For more information, please refer to the official Maven introduction to the build lifecycle.
In order to install our Maven project, we must first navigate to its folder, using the terminal (Linux or Mac) or the command prompt (Windows). Then, we must issue the following command:
1 | mvn install |
This command is responsible for executing Maven’s install
phase. Maven executes each build phase sequentially thus, before the install
phase, Maven will execute all previous phases.
1. Sample Execution
Inside the pom.xml
file, we can:
- Define our
packaging
type, such asjar
orwar
. - Define the name of our file.
- Define the version of our file.
1 2 3 | < packaging >jar</ packaging > < name >SampleExample</ name > < version >1.0.0</ version > |
By using the following pom.xml
file
pom.xml:
01 02 03 04 05 06 07 08 09 10 | < 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/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.javacodegeeks</ groupId > < artifactId >SampleExample</ artifactId > < packaging >jar</ packaging > < name >SampleExample</ name > < version >1.0.0</ version > </ project > |
we create a new file called SampleExample-1.0.0.jar
inside our local repository:
Finally, it is a good practice to use Maven’s install
command along with Maven’s clean
command as following:
1 | mvn clean install |
This command first, cleans our project by removing everything inside the target
folder and then, installs our executable file inside Maven’s local repository.
2. Use our Project as a Dependency
After we have successfully installed our project into Maven’s local repository, we can use it as a dependency to another project, by adding to our pom.xml
file, the following code snippet:
1 2 3 4 5 | < dependency > < groupId >com.javacodegeeks</ groupId > < artifactId >SampleExample</ artifactId > < version >1.0.0</ version > </ dependency > |
This was a tutorial on how to install a project, using Apache Maven.