官方介绍地址
手动加密:https://github.com/core-lib/xjar
maven插件集成:https://github.com/core-lib/xjar-maven-plugin
本篇着重记录下maven集成xJar插件且项目中没有用到JPA
集成步骤
- 添加 https://jitpack.io Maven仓库
- 在 plugins 中添加 xJar 插件
<project>
<!-- 设置 jitpack.io 插件仓库 -->
<pluginRepositories>
<pluginRepository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</pluginRepository>
</pluginRepositories>
<!-- 添加 XJar Maven 插件 -->
<build>
<plugins>
<plugin>
<groupId>com.github.core-lib</groupId>
<artifactId>xjar-maven-plugin</artifactId>
<version>4.0.2</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
<!--可以改成 install-->
<phase>package</phase>
<configuration>
<password>xxxx</password>
<!-- 需要加密的资源路径表达式 -->
<includes>
<include>com/zsplat/**</include>
<include>mapper/*Mapper.xml</include>
<include>config/**</include>
</includes>
<!-- 无需加密的资源路径表达式 -->
<excludes>
<exclude>static/**</exclude>
<exclude>META-INF/**</exclude>
</excludes>
<!-- 目标jar存放目录 -->
<targetDir>${project.build.directory}\xJarDir\</targetDir>
<!-- 目标jar名称,也可以用表达式(参考官网) -->
<targetJar>zsplat.jar</targetJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
上面xJar插件绑定到指定的 package ,因此每次执行 mvn clean package 即可自动构建出加密的包,如下图:
根目录中 gateway-1.0.0.jar 是未加密的,而 xJarDir 文件夹下的 gateway-1.0.0.jar 就是加密后的。
强烈建议不要在 pom.xml 的 xjar-maven-plugin 配置中写上密码,这样会导致打包出来的 xjar 包中的 pom.xml 文件保留着密码,极其容易暴露密码!强烈推荐通过 mvn 命令来指定加密密钥!
启动运行加密后的jar
注意:用于编译的机器需要安装 Go 环境,用于运行的机器则可不必安装 Go 环境,具体安装教程请自行搜索
- 在对应的 xJarDir 文件夹目录编译脚本(go build xjar.go)。在不同的平台进行编译即可得到不同平台的启动器可执行文件,其中Windows下文件名为 xjar.exe 而Linux下为 xjar
- window下CMD运行命令:xjar java -Dfile.encoding=utf-8 -jar gateway-1.0.0.jar,Powershell 窗口需要加上 ./ 如:./xjar java -jar gateway-1.0.0.jar 并且utf-8编码也不支持。
- linux下后台运行命令 nohup ./xjar java -Dfile.encoding=utf-8 -jar gateway-1.0.0.jar &