SoFunction
Updated on 2025-05-06

Detailed explanation of how to manually install jar packages into local Maven repository

1. Preparation

1.1 Download the JAR package

First, make sure you have downloaded the required JAR package from the internet or other avenues and saved it to a directory on your local computer.

1.2 Determine the coordinates of the JAR package

In Maven, each JAR package has a unique coordinate, which consists of the following parts:

  • groupId: A unique identifier for an organization or project group.
  • artifactId: The unique identifier of the project.
  • version: The version number of the project.
  • Packaging (optional): Packaging type, default to ​​jar​​.

For example, if you have a JAR package called ​​​, you can define the following coordinates for it:

  • ​​groupId​​: ​​​​
  • ​​artifactId​​: ​​mylib​​
  • ​​version​​: ​​1.0.0​​
  • ​​packaging​​: ​​jar​​

2. Install the JAR package using the command line

2.1 Open the command line tool

Open a command line tool (such as Windows' CMD, PowerShell, or Linux's terminal) and navigate to the directory containing the JAR package.

2.2 Execute the installation command

Use the mvn install:install-file command to install the JAR package into the local Maven repository. The command format is as follows:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

in:

  • ​​<path-to-file>​​: The full path to the JAR package.
  • ​​<group-id>​​: The organization or project group ID of the JAR package.
  • ​​<artifact-id>​​: The project ID of the JAR package.
  • ​​<version>​: The version number of the JAR package.
  • ​​<packaging>​​: The packaging type of JAR package, default to ​​jar​​.

2.3 Example

Assume that your JAR package path is /home/user/, and you want to install it into your local Maven repository, the coordinate information is as follows:

  • ​​groupId​​: ​​​​
  • ​​artifactId​​: ​​mylib​​
  • ​​version​​: ​​1.0.0​​
  • ​​packaging​​: ​​jar​​

Execute the following command:

mvn install:install-file -Dfile=/home/user/ -DgroupId= -DartifactId=mylib -Dversion=1.0.0 -Dpackaging=jar

After executing the above command, Maven will install the JAR package to the corresponding location in the local repository.

3. Verify the installation

3.1 Check the local warehouse

By default, Maven's local repository is located in the .m2/repository directory under the user's home directory. You can navigate to the directory and check if the JAR package has been successfully installed.

For example, for the above example, the JAR package should be installed to the following path:

~/.m2/repository/com/example/mylib/1.0.0/

3.2 Reference in POM files

In your project, you can add dependencies on newly installed JAR packages in the file. For example:

<dependency>
    <groupId></groupId>
    <artifactId>mylib</artifactId>
    <version>1.0.0</version>
</dependency>

It is easy to install your own downloaded JAR packages into your local Maven repository and reference these JAR packages in your project. This not only facilitates project management and construction, but also improves development efficiency.

4. Method supplement

In actual development, sometimes we need to add our downloaded third-party libraries or custom JAR packages to Maven's local repository to use these dependencies in our project. Here is a detailed step and sample code showing how to add a JAR package to Maven's local repository.

step

Download the JAR package: Make sure you have downloaded the required JAR package.

Determine the coordinates of the JAR package: You need to determine the coordinates of the JAR package (groupId, artifactId, version).

Use the Maven command to install the JAR package: Use the mvn install:install-file command to install the JAR package to the local repository.

Example

Suppose you have a JAR package called my-custom-library-1.0. and you want to install it into your local repository. You can follow the steps below:

1. Open command line tools: Open your command line tools (such as Windows CMD, PowerShell, or Linux/Mac terminal).

2. Navigate to the directory where the JAR package is located: Use the cd command to navigate to the directory containing the JAR package. For example:

cd /path/to/your/jar/directory

Run the Maven command: Use the mvn install:install-file command to install the JAR package to the local repository. The command format is as follows:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

For example, if your JAR package path is /path/to/your/jar/directory/my-custom-library-1.0.​ and the coordinates you want to use are:my-custom-library:1.0.0, then the command is as follows:

mvn install:install-file -Dfile=my-custom-library-1.0. -DgroupId= -DartifactId=my-custom-library -Dversion=1.0.0 -Dpackaging=jar

Verify installation: After the installation is completed, you can check the corresponding directory in the local Maven repository to confirm whether the JAR package has been successfully installed. By default, the local repository is located in the .m2/repository directory under the user's home directory. For example:

~/.m2/repository/com/example/my-custom-library/1.0.0/

Reference JAR packages in POM files

Once the installation is complete, you can reference this JAR package in the file of your Maven project. For example:

<dependencies>
    <dependency>
        <groupId></groupId>
        <artifactId>my-custom-library</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

Through the above steps, you can add your downloaded JAR package to Maven's local repository and reference it in your project. This way, you can use this JAR package like you would with other Maven dependencies. Hope this example helps you! If you have any questions, feel free to ask.

When using Maven for project development, sometimes it is necessary to add some custom or third-party JAR packages that cannot be obtained from public repositories to your local Maven repository. Maven provides a command line tool to accomplish this task, namely the mvn install:install-file command.

The following are the detailed steps and command formats:

1. Prepare JAR file

Make sure you have downloaded the JAR file you need to add to your local repository and know its path.

2. Open the command line tool

Open command line tools (such as Windows CMD, PowerShell, or Linux terminal).

3. Use mvn install:install-file command

Enter the following command in the command line to install the JAR file into the local Maven repository:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

Parameter description

  • ​​-Dfile=<path-to-file>​: Specify the path to the JAR file.
  • -DgroupId=<group-id>​​: Specifies the group ID of the JAR file, usually a reverse domain name, for example.
  • -DartifactId=<artifact-id>​​: Specifies the artifact ID of the JAR file, usually the name of the project, such as my-library​.
  • -Dversion=<version>​​: Specify the version number of the JAR file, such as 1.0.0.
  • -Dpackaging=jar: Specify the packaging type as JAR.

Example

Suppose you have a file named my-custom-library-1.0., located in the C:\Users\YourName\Downloads directory, and you want to install it into your local Maven repository, you can use the following command:

mvn install:install-file -Dfile=C:\Users\YourName\Downloads\my-custom-library-1.0. -DgroupId= -DartifactId=my-custom-library -Dversion=1.0.0 -Dpackaging=jar

4. Verify the installation

After the installation is complete, you can verify that the JAR file has been successfully added to the local Maven repository by:

Check the local Maven repository directory (the default path is ~/.m2/repository) and confirm whether the JAR file exists in the corresponding directory.

Add dependencies to your project and try to build the project to ensure it compiles and runs properly.

5. Use in the project

Add the corresponding dependencies to your file, for example:

<dependency>
    <groupId></groupId>
    <artifactId>my-custom-library</artifactId>
    <version>1.0.0</version>
</dependency>

This way, Maven will get and use this JAR file from the local repository.

Things to note

Make sure the path, group ID, artifact ID and version number of the JAR file are correct.

If the JAR file has the corresponding source code or documentation, you can use a similar command to install it into the local repository together, just change the -Dpackaging parameter to sources or javadoc.

The above is a detailed explanation of how to manually install jar packages in the local Maven repository. For more information about installing jar packages in the local Maven repository, please pay attention to my other related articles!