maven打包scope=system依赖时无法将 jar打进包的问题

本文介绍了如何在Maven中处理system范围的依赖,正常情况下此类依赖不会被打包。通过设置`<includeSystemScope>true</includeSystemScope>`,可以将`feign-jackson`等system路径的jar包包含进应用。对于WAR包,需要额外的maven-war-plugin配置,将lib目录下的jar文件放入`WEB-INF/lib`下。

正常情况下,scope=system的包是不会打进包中的。

<dependency>
	<groupId>com.netflix.feign</groupId>
	<artifactId>feign-jackson</artifactId>
	<version>8.18.0</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/lib/feign-jackson-8.18.0.jar</systemPath>
</dependency>

此时如果需要将jar包打输出到lib中,需添加配includeSystemScope=true

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
		<includeSystemScope>true</includeSystemScope>
		<mainClass>com.startboot.StartApplication</mainClass>
	</configuration>
</plugin>

如果你需要打到war包中,此时并不会将本地jar包打入\WEB-INF\lib 下,而是在 \WEB-INF\lib-provided 下,所以需要添加以下插件配置

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<configuration>
		<webResources>
			<resource>
				<directory>${project.basedir}/lib/</directory>
				<targetPath>WEB-INF/lib/</targetPath>
				<includes>
					<include>**/*.jar</include>
				</includes>
			</resource>
		</webResources>
	</configuration>
</plugin>

 

<think> 我们参考了用户提到的引用内容,特别是引用[3]和引用[4],用户明确要求通过Maven插件配置将system作用域的本地依赖打进jar。虽然之前推荐使用`mvn install`安装到本地仓库的方法,但用户可能由于某些原因无法使用该方法(比如无法修改本地仓库,或者需要保持依赖的本地路径等),所以需要直接处理system作用域的依赖。 根据引用[3],在Spring Boot项目中,可以通过配置`spring-boot-maven-plugin`插件,设置`<includeSystemScope>true</includeSystemScope>`来将system作用域的依赖含进可执行jar。 同,引用[4]也提到了使用system作用域依赖遇到的问题:本地依赖在开发环境可用,但打包没有打进jar,导致运行缺失。 因此,解决方案如下: 1. 在pom.xml中,使用`system`作用域声明本地依赖,并指定`<systemPath>`(注意使用Maven属性如`${project.basedir}`来指定相对路径,保证可移植性)。 2. 配置`spring-boot-maven-plugin`插件,设置`includeSystemScope`为`true`。 具体步骤: 步骤1:在dependencies中声明本地依赖 例如,将位于项目根目录下`libs`文件夹中的`example.jar`作为依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/example.jar</systemPath> </dependency> </dependencies> ``` 步骤2:在build插件中配置`spring-boot-maven-plugin` 在`<build><plugins>`部分添加或修改该插件配置: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 关键配置:system作用域的依赖 --> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build> ``` 注意:如果不使用Spring Boot,而是普通的可执行jar(使用`maven-assembly-plugin`或`maven-shade-plugin`),则需要额外配置这些插件来system作用域的依赖。 对于普通的Maven项目(非Spring Boot),可以使用`maven-assembly-plugin`或`maven-shade-plugin`来打包含本地依赖jar。以`maven-shade-plugin`为例: 配置`maven-shade-plugin`: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 配置system作用域的依赖 --> <filters> <filter> <artifact>*:*</artifact> <includes> <include>**</include> </includes> </filter> </filters> </configuration> </execution> </executions> </plugin> ``` 但是,请注意:`maven-shade-plugin`默认不会system作用域的依赖。因此,我们需要手动将本地jar文件添加为插件的依赖。这可以通过在插件配置中显式添加依赖来实现: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <artifactSet> <!-- 含本地jar(通过指定group和artifact) --> <includes> <include>com.example:example</include> </includes> </artifactSet> </configuration> </execution> </executions> <dependencies> <!-- 在此处添加system作用域的依赖,这样插件才能找到它 --> <dependency> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/example.jar</systemPath> </dependency> </dependencies> </plugin> ``` 这种方法较为繁琐,因此对于非Spring Boot项目,我们仍然推荐将本地依赖安装到本地仓库(使用`mvn install:install-file`)然后以常规依赖引入。 总结: 对于Spring Boot项目,配置`spring-boot-maven-plugin`的`<includeSystemScope>true</includeSystemScope>`是最简便的方法。对于非Spring Boot项目,如果必须使用system作用域,则需要通过配置`maven-shade-plugin`并显式添加依赖,但更推荐安装到本地仓库。 注意:使用system作用域的依赖会降低项目的可移植性,因为其他开发者必须拥有相同路径的jar文件。因此,如果可能,优先使用安装到本地仓库的方式。 最后,请确保在打包后验证依赖是否已经含在jar文件中。可以通过解压jar文件查看BOOT-INF/lib(Spring Boot项目)或直接查看jar内容(普通项目)来确认。 参考引用:[^3][^4] </think> ### 通过 Maven Plugin 配置将 `system` 本地依赖打进 JAR 的方法 在 Maven 项目中处理本地依赖打包,若必须使用 `system` 作用域,可通过配置构建插件强制依赖。以下是具体操作步骤(基于引用[3]和[4]): #### 1. **声明本地依赖(`system` 作用域)** 在 `pom.xml` 中声明本地依赖,使用 `systemPath` 指定 **相对路径**(推荐 `${project.basedir}` 保证可移植性): ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>local-lib</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/local-lib.jar</systemPath> </dependency> </dependencies> ``` > 路径示例:将 JAR 放在项目根目录的 `lib` 文件夹下。 #### 2. **配置 Spring Boot Maven Plugin(关键步骤)** 通过 `<includeSystemScope>true</includeSystemScope>` 强制含 `system` 作用域的依赖(引用[3]): ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- 关键配置:system 作用域的依赖 --> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> </plugins> </build> ``` #### 3. **配置资源复制(可选)** 确保本地 JAR 文件被复制到构建目录: ```xml <resources> <resource> <directory>lib</directory> <targetPath>BOOT-INF/lib/</targetPath> <!-- Spring Boot 路径 --> <includes> <include>*.jar</include> </includes> </resource> </resources> ``` #### 4. **执行打包命令** ```bash mvn clean package ``` 生成的 `target/*.jar` 中将含本地依赖。 --- ### ⚠️ 注意事项 1. **路径问题** `systemPath` 必须使用 **Maven 属性**(如 `${project.basedir}`)避免硬编码,否则跨环境会失败(引用[4])。 2. **可移植性限制** 此方法要求所有开发者机器上的依赖路径相同,不适合团队协作。优先推荐 `mvn install:install-file` 安装到本地仓库[^2]。 3. **验证打包结果** 解压生成的 JAR 检查是否依赖: ```bash jar tf target/your-app.jar | grep local-lib.jar # 应输出 BOOT-INF/lib/local-lib.jar ``` > 完整配置参考:[^3][^4] --- ### 替代方案:更可靠的方法 若无需强制使用 `system` 作用域,**推荐安装到本地仓库**(引用[1][2]): ```bash mvn install:install-file \ -Dfile=lib/local-lib.jar \ -DgroupId=com.example \ -DartifactId=local-lib \ -Dversion=1.0.0 \ -Dpackaging=jar ``` 然后在 `pom.xml` 中正常声明依赖: ```xml <dependency> <groupId>com.example</groupId> <artifactId>local-lib</artifactId> <version>1.0.0</version> </dependency> ``` 此方法无路径问题,且打包自动依赖。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值