如何不使用任何插件将非Maven依赖项安装到Maven项目中?
Got it, let's tackle this problem step by step—since you need to include non-Maven repository JARs in your pom.xml and ensure they're properly resolved during mvn package in a CI environment, there are two reliable approaches you can use:
Approach 1: Use System Scope with Project-Local JARs
This method keeps your custom JARs directly in your project repository, so they're automatically pulled into the CI environment when the code is checked out.
- First, create a dedicated folder (like
lib/) at your project root, and place all your non-Maven JARs inside it. This ensures CI has access to the files without needing external setup. - Next, add the dependencies to your
pom.xmlusing thesystemscope, with a relative path to the JAR:
<dependency> <groupId>com.yourteam</groupId> <!-- Custom group ID, use something consistent --> <artifactId>your-custom-library</artifactId> <!-- Custom artifact ID matching your JAR --> <version>1.0.0</version> <!-- Custom version number --> <scope>system</scope> <systemPath>${project.basedir}/lib/your-custom-library.jar</systemPath> </dependency>
- Important note: System scope dependencies aren't automatically packaged into your final artifact (like a WAR or executable JAR). To fix this, add the
maven-dependency-pluginto copy these JARs during the build:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.1</version> <executions> <execution> <id>copy-system-jars</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <includeScope>system</includeScope> <outputDirectory>${project.build.directory}/classes/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
Approach 2: Install JARs to the CI's Local Maven Repository (More Maven-Compliant)
This method treats your custom JARs like standard Maven dependencies, avoiding the limitations of the system scope.
- First, add a step in your CI build script to install the JARs to the local Maven repo before running
mvn package. For example, if your JAR is inlib/, the command would be:
mvn install:install-file \ -Dfile=lib/your-custom-library.jar \ -DgroupId=com.yourteam \ -DartifactId=your-custom-library \ -Dversion=1.0.0 \ -Dpackaging=jar \ -DgeneratePom=true
- Then, add the dependency to your
pom.xmljust like any regular Maven dependency—no special scope needed:
<dependency> <groupId>com.yourteam</groupId> <artifactId>your-custom-library</artifactId> <version>1.0.0</version> </dependency>
- Example CI script snippet (for GitHub Actions):
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' cache: maven - name: Install custom JARs to local repo run: | # Repeat this command for each custom JAR you have mvn install:install-file -Dfile=lib/your-custom-library.jar -DgroupId=com.yourteam -DartifactId=your-custom-library -Dversion=1.0.0 -Dpackaging=jar -DgeneratePom=true - name: Build with Maven run: mvn -B package --file pom.xml
Which Approach Should You Use?
- Use Approach 1 if you have a small number of JARs and want minimal CI script changes. Just be mindful of the packaging setup.
- Use Approach 2 for longer-term projects or multiple custom JARs—it follows Maven's best practices and avoids the edge cases of system scope dependencies.
内容的提问来源于stack exchange,提问作者Mayank Gupta
相关产品推荐
相关产品推荐

