编程 Java Maven 如何在maven中添加自动混淆 RandomEnch 2024-07-28 2024-07-28 前言 最近在项目中使用了allatori进行代码混淆,但是每次打包完jar文件后还需要手动混淆,这让我很不爽,所以想着能不能在maven中自动进行代码混淆,直接打包出混淆后的jar文件。
步骤 1. 下载allatori混淆器,并解压到工作目录 下载地址:https://www.allatori.com/
将其解压并放到工作目录,例如我将下载好的allatori.jar解压到放到了[root]\allatori\lib
目录下。 然后在[root]\allatori\
目录下创建allatori.xml
配置文件,具体内容可以查看其官方文档结合你的项目情况进行配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <config > <input > <jar in ="xxx-origin.jar" out ="xxx-obfuscated.jar" /> </input > <ignore-classes > <class template ="class *org*" /> <class template ="class com.*" /> </ignore-classes > <keep-names > <class template ="class *" /> </keep-names > <property name ="log-file" value ="log.xml" /> <property name ="classes-naming" value ="custom(configuration/ClassNaming.txt)" /> <property name ="methods-naming" value ="custom(configuration/MethodNaming.txt)" /> <property name ="packages-naming" value ="custom(configuration/PackageNaming.txt)" /> </config >
2. 修改pom.xml文件 1.在项目信息中插入name
1 2 3 4 <groupId>xxx.xxx</groupId> <artifactId>xxx</artifactId> <version>V0.x.x</version> + <name>xxx</name>
在properties
·标签下添加customArtifactFile
1 2 3 4 5 6 <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <customArtifactFile>${project.build.directory}/${project.name}-${project.version}-origin.jar</customArtifactFile> </properties>
在plugin
标签下添加插件,此处为了便于阅读,我去除了缩进,实际使用时请保持一致。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-resources-plugin</artifactId > <version > 2.6</version > <executions > <execution > <id > copy-and-filter-allatori-config</id > <phase > package</phase > <goals > <goal > copy-resources</goal > </goals > <configuration > <outputDirectory > ${basedir}/target</outputDirectory > <resources > <resource > <directory > allatori</directory > <includes > <include > allatori.xml</include > </includes > <filtering > true</filtering > </resource > </resources > </configuration > </execution > <execution > <id > copy-and-filter-allatori-folder</id > <phase > package</phase > <goals > <goal > copy-resources</goal > </goals > <configuration > <outputDirectory > ${basedir}/target/configuration</outputDirectory > <resources > <resource > <directory > allatori/configuration</directory > </resource > </resources > </configuration > </execution > </executions > </plugin > <plugin > <groupId > org.codehaus.mojo</groupId > <artifactId > exec-maven-plugin</artifactId > <version > 1.2.1</version > <executions > <execution > <id > run-allatori</id > <phase > package</phase > <goals > <goal > exec</goal > </goals > <configuration > <executable > java</executable > <arguments > <argument > -Xms128m</argument > <argument > -Xmx512m</argument > <argument > -jar</argument > <argument > allatori/lib/allatori.jar</argument > <argument > ${basedir}/target/allatori.xml</argument > </arguments > </configuration > </execution > </executions > </plugin >
3. 打包 执行mvn clean package
命令,即可完成打包,打包后的jar文件会自动进行混淆,最终会生成一个未混淆的jar文件和成一个混淆后的jar文件,命名规则为xxx-version-origin.jar
和xxx-version-obfuscated.jar
。
4. 补充说明 allatori混淆后的jar文件用压缩工具打开后,会发现有压缩文件注释中有一行This file is obfuscated by Allatori Obfuscator
,这行注释是混淆器的标识,可以用来判断混淆后的文件是否正确。 如果想删除混淆器标识,可以使用压缩工具的压缩选项,将This file is obfuscated by Allatori Obfuscator
这一行注释去除即可。或者我直接写了一个CommentRemover.jar 用于删除注释,直接在maven中使用即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 <plugin > <groupId > org.codehaus.mojo</groupId > <artifactId > exec-maven-plugin</artifactId > <version > 1.2.1</version > <executions > <execution > <id > run-allatori</id > <phase > package</phase > <goals > <goal > exec</goal > </goals > <configuration > <executable > java</executable > <arguments > <argument > -Xms128m</argument > <argument > -Xmx512m</argument > <argument > -jar</argument > <argument > allatori/lib/allatori.jar</argument > <argument > ${basedir}/target/allatori.xml</argument > </arguments > </configuration > </execution > <execution > <id > run-comment-remover</id > <phase > package</phase > <goals > <goal > exec</goal > </goals > <configuration > <executable > java</executable > <arguments > <argument > -jar</argument > <argument > allatori/lib/CommentRemover.jar</argument > <argument > ${basedir}/target/${project.name}-${project.version}-obfuscated.jar</argument > <argument > ${basedir}/target/${project.name}-${project.version}.jar</argument > </arguments > </configuration > </execution > </executions > </plugin >