Maven

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. validate5. integration-test
2. compile6. verify
3. test7. install
4. package8. 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 as jar or war.
  • 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
 
   <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:

mvn_install_cmd_WM

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.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button