前言
之前在开发过程中遇到了两个问题:一是本地项目依赖了本地路径下的jar包,Maven打包时无法将本地路径下的jar打进SpringBoot的jar中;二是在Windows系统下运行测试的jar包代码时,dos窗口中的中文日志乱码,特此记录解决方法。
环境
Windows 10
、Maven 3.3
、IDEA 2020.3
Maven依赖本地路径jar打包问题
还有一种思路是将外部jar放进私库中,项目中依赖后也能打进去项目jar,后续整理后再搭建私服玩玩
## 依赖的本地路径jar
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/xxx.jar</systemPath>
</dependency>
## 需要配置includeSystemScope为true,打包时会自动将外部依赖包打进去
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
复制代码
附上一篇SpringBoot-Maven打包依赖插件,取自若依(Thanks)
<!-- 插件配置在主启动jar模块的pom.xml中 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.1.RELEASE</version>
<configuration>
<!-- 如果没有该配置,devtools不会生效 -->
<fork>true</fork>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>-->
</plugins>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<targetPath>./</targetPath>
</resource>
<resource>
<directory>lib/</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
</build>
复制代码
jar在Windows下运行,窗口中文日志乱码
先给出解决方案,运行jar包时使用命令如下:
chcp 65001 & java -Dfile.encoding=utf-8 -jar xxx.jar
复制代码
说明
chcp 65001
将CMD默认编码格式设置为 UTF-8
,-Dfile.encoding=utf-8
将Java虚拟机的默认字符集设置为UTF-8
chcp
chcp 936
Chinese,即中文简体/GBKchcp 437
United States,即美国英语chcp 850
Multilingual (Latin I)chcp 852
Slavic (Latin II)chcp 855
Cyrillic (Russian)chcp 857
Turkishchcp 860
Portuguesechcp 861
Icelandicchcp 863
Canadian-Frenchchcp 865
Nordicchcp 866
Russianchcp 869
Modern Greekchcp 65001
utf-8