Preface
In modern backend development, building efficient and scalable web applications often depends on multiple third-party libraries and internal dependencies. These dependencies can come from public warehouses, or may be self-developed libraries within the company, or JAR packages that have not been published to public warehouses. This article will provide detailed information on how to handle local dependencies in a Maven project and ensure that these dependencies are properly packaged into the final executable JAR file. This article not only uses the Doris connector (flink-doris-connector) as an example, but also covers common methods for handling other local dependencies.
Why do you need to package the local dependency library?
Typically, dependency libraries can be easily accessed and managed through Maven central repository or other public repository. However, sometimes we need to use some local JAR packages that are not published to a public repository, for example:
- The library developed by the company
- A library provided by a third party but not uploaded to the Maven repository
- Special or customized versions of the library
- Direct reference to local dependency libraries can cause problems, especially during build and deployment. To ensure the portability and consistency of the project, these local dependencies must be correctly packaged into the final JAR file.
FAQ: Using system scope
-
In Maven, you can use the system scope to reference local JAR packages. However, this approach has several significant disadvantages:
- Not portability: The path to system scope dependencies is hardcoded, which other developers may not be able to find in different environments.
- Packaging issues: Dependencies using system scopes are not included in the final packaged JAR file by default, resulting in the lack of necessary dependencies at runtime.
-
Best practices for dependency management
To avoid the above problems, it is recommended to install the local dependency library into the Maven local repository and use the conventional dependency management mechanism for reference. In this way, the consistency and portability of the dependency library can be ensured, and it also facilitates subsequent dependency management and version control.
Solution: Package the local dependency library to the final JAR
Detailed steps
Here are detailed steps to show how to include and package the local dependency library in your Maven project into your final JAR file.
Step 1: Install the local JAR to the Maven local repository
First, you need to install the local JAR package into Maven's local repository. Suppose there is a local flink-doris-connector JAR file located in the project's libs directory.
Open the terminal and execute the following command:
mvn install:install-file \ -DgroupId= \ -DartifactId=flink-connector-doris_2.12 \ -Dversion=1.14_2.12-1.1.1 \ -Dpackaging=jar \ -Dfile=libs/flink-doris-connector-1.14_2.12-1.1.
Parameter description:
Through the above command, install the local JAR package into the Maven local repository, so that it can be managed by Maven like other dependencies.
- -DgroupId: The dependent organization ID, usually corresponding to the package name.
- -DartifactId: The name of the dependency module.
- -Dversion: dependent version number.
- -Dpackaging: The type of packaging that depends on, usually jar.
- -Dfile: The path to the local JAR file.
Step 2: Modify the dependency configuration in the file
After the installation is complete, the dependency needs to be referenced in the project's file. Remove the configuration that used the system scope before and change to the default compile scope.
Original dependency configuration (using system scope)
<dependency> <groupId></groupId> <artifactId>flink-connector-doris_${}</artifactId> <version>1.14_2.12-1.1.1</version> <scope>system</scope> <systemPath>${}/libs/flink-doris-connector-1.14_2.12-1.1.</systemPath> </dependency>
Modified dependency configuration
Note: The and \ element is omitted, and the default scope is compile, so that Maven will automatically handle the dependency.
<dependency> <groupId></groupId> <artifactId>flink-connector-doris_${}</artifactId> <version>1.14_2.12-1.1.1</version> </dependency>
Step 3: Rebuild the project
mvn clean package
This command will:
Clean up previous build products.
Compile the project source code.
Use the Maven Shade plugin to package all dependencies, including local dependencies, into the final JAR file.
This is the article about the implementation of packaging local dependency libraries into the final JAR in the Maven project. For more related content on the Maven local dependency libraries, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!