When you need to use a local JAR file in a Maven project, follow these steps:
1. Install the Local JAR File into the Local Maven Repository
First, install the local JAR file into your local Maven repository using Maven's install:install-file goal. The required parameters are:
file: Path to the JAR filegroupId: Identifier for the project or organizationartifactId: Identifier for the projectversion: Version of the JARpackaging: Package type, typicallyjar
For example, if you have a JAR file named example.jar and want to install it into the Maven repository, use this command:
bashmvn install:install-file \ -Dfile=path/to/example.jar \ -DgroupId=com.example \ -DartifactId=example \ -Dversion=1.0.0 \ -Dpackaging=jar
2. Add Dependency to the Project's pom.xml File
After installing the local JAR, add a dependency to the project's pom.xml file. This instructs Maven to include the JAR during project build. Ensure the dependency matches the coordinates of the JAR installed in the repository. For example:
xml<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> </dependency> </dependencies>
3. Build the Project
After adding the dependency, build your project as usual. Maven will automatically resolve and use the JAR from the local repository.
bashmvn package
Real-World Example
Suppose you're working on a project requiring a specific hardware interface, where the driver only provides a JAR file not published to the public Maven repository. Use the above method: first install the JAR into your local Maven repository, then declare the dependency in your project. This allows you to use the driver interface within your project.