赞
踩
多模块跟单模块一样,在需要混淆模块的pom文件中加入以下依赖:
<plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.6.0</version> <executions> <execution> <id>proguard</id> <!-- 在打包阶段混淆--> <phase>package</phase> <goals> <!-- 只使用混淆功能--> <goal>proguard</goal> </goals> </execution> </executions> <configuration> <proguardVersion>7.1.0</proguardVersion> <!-- 原始jar包--> <injar>${project.build.finalName}.jar</injar> <!-- 混淆之后的jar包名 此处与原始一样 混覆盖原始jar包--> <outjar>${project.build.finalName}.jar</outjar> <obfuscate>true</obfuscate> <options> <option>-dontshrink</option> <option>-dontoptimize</option> <!-- ##对于类成员的命名的混淆采取唯一策略--> <option>-useuniqueclassmembernames</option> <!-- This option will replace all strings in reflections method invocations with new class names. For example, invokes Class.forName('className')--> <option>-adaptclassstrings</option> <!-- ## 混淆时不生成大小写混合的类名,默认是可以大小写混合--> <option>-dontusemixedcaseclassnames</option> <!-- This option will save all original annotations and etc. Otherwise all we be removed from files.--> <option>-keepattributes Exceptions, InnerClasses, Signature, Deprecated, SourceFile, LineNumberTable, *Annotation*, EnclosingMethod </option> <!-- This option will save all original names in interfaces (without obfuscate).--> <option>-keepnames interface **</option> <!-- This option will save all original methods parameters in files defined in -keep sections, otherwise all parameter names will be obfuscate.--> <option>-keepparameternames</option> <!-- This option will save all original class files (without obfuscate) but obfuscate all in domain and service packages.--> <option>-keep class com.xxx.aaa.zzzSApplication { public static void main(java.lang.String[]); } </option> <!-- This option ignore warnings such as duplicate class definitions and classes in incorrectly named files--> <option>-ignorewarnings</option> <!-- This option will save all original class files (without obfuscate) in service package--> <!-- <option>-keep class com.slm.proguard.example.spring.boot.service { *; }</option>--> <!-- This option will save all original interfaces files (without obfuscate) in all packages.--> <option>-keep interface * extends * { *; }</option> <!-- This option will save all original defined annotations in all classes in all packages.--> <option>-keep class com.xxx.aaa.config.** { *; }</option> <option>-keep class com.xxx.aaa.mapper.** { *; }</option> <option>-keep class com.xxx.aaa.model.** { *; }</option> <option>-keep class com.xxx.aaa.filter.** { *; }</option> <option>-keep class com.xxx.aaa.enums.** { *; }</option> <option>-keep class com.xxx.aaa.controller.** { *; }</option> <option>-keep class com.xxx.aaa.entity.** { *; }</option> <option>-keep class com.fasterxml.jackson.** { *; }</option> <option>-keep class org.json.JSONObject.** {** put(java.lang.String,java.util.Map);}</option> <option>-keep class org.gitlab4j.**</option> <option>-keepclassmembers class * { @org.springframework *; @org.springframework.context.annotation.Bean *; @org.springframework.beans.factory.annotation.Autowired *; @org.springframework.beans.factory.annotation.Value *; } </option> <option>-dontwarn com.fasterxml.jackson.databind.**</option> <option>-dontwarn com.fasterxml.jackson.**</option> </options> <injarNotExistsSkip>true</injarNotExistsSkip> <libs> <!-- java11--> <!-- <lib>${java.home}/jmods/java.base.jmod</lib>--> <!-- <lib>${java.home}/jmods/java.datatransfer.jmod</lib>--> <!-- <lib>${java.home}/jmods/java.prefs.jmod</lib>--> <!-- <lib>${java.home}/jmods/java.xml.jmod</lib>--> <!-- <lib>${java.home}/jmods/java.desktop.jmod</lib>--> <!-- <lib>${java.home}/jmods/</lib>--> <!-- java8--> <!--Put here your libraries if required--> <lib>${java.home}/lib/rt.jar</lib> </libs> </configuration> <dependencies> <dependency> <groupId>com.guardsquare</groupId> <artifactId>proguard-base</artifactId> <version>7.1.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.guardsquare</groupId> <artifactId>proguard-core</artifactId> <version>7.1.0</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <mainClass>com.xxx.aaa.sssApplication</mainClass> </configuration> </execution> </executions> </plugin>
<option>-keep class com.xxx.aaa.config.** { *; }</option>
<option>-keep class com.xxx.aaa.mapper.** { *; }</option>
<option>-keep class com.xxx.aaa.model.** { *; }</option>
<option>-keep class com.xxx.aaa.filter.** { *; }</option>
<option>-keep class com.xxx.aaa.enums.** { *; }</option>
<option>-keep class com.xxx.aaa.controller.** { *; }</option>
<option>-keep class com.xxx.aaa.entity.** { *; }</option>
若项目中存在xxxMapper.xml,对于xml文件中引用的相关entity、model等类,这些类不能混淆,要排除。
项目中存在过滤器类、枚举也不能混淆;
2 混淆后的jar包内不存在BOOT-INF
需要加入这两个依赖,一般后一个依赖没加
<dependencies>
<dependency>
<groupId>com.guardsquare</groupId>
<artifactId>proguard-base</artifactId>
<version>7.1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.guardsquare</groupId>
<artifactId>proguard-core</artifactId>
<version>7.1.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
3,对于java不同版本
<libs>
<!-- java11-->
<!-- <lib>${java.home}/jmods/java.base.jmod</lib>-->
<!-- <lib>${java.home}/jmods/java.datatransfer.jmod</lib>-->
<!-- <lib>${java.home}/jmods/java.prefs.jmod</lib>-->
<!-- <lib>${java.home}/jmods/java.xml.jmod</lib>-->
<!-- <lib>${java.home}/jmods/java.desktop.jmod</lib>-->
<!-- <lib>${java.home}/jmods/</lib>-->
<!-- java8-->
<!--Put here your libraries if required-->
<lib>${java.home}/lib/rt.jar</lib>
</libs>
4,启动jar包时:
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.xxx.aaa.zzzCSApplication]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'b' for bean class [com.xxx.aaa.c.b] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.aaa.a.b] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189) at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) at com.xxx.aaa.SSCSApplication.main(SSCSApplication.java:47) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'b' for bean class [com.xxx.aaa.c.b] conflicts with existing, non-compatible bean definition of same name and class [com.xxx.aaa.a.b] at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349) at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287) at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ... 21 common frames omitted
问题原因:
出现这个问题主要还是混淆之后,bean重名了,spring默认是把类名的首字母小写加载到容器里面,我们混淆类名之后,就容易造成beanName重复。
解决方案:
通过改变spring加载bean的命名策略来解决这个问题,把包名带上,同时在获取Spring上下文getBean的时候,加上包名路径即可
启动类配置,具体如下:
@SpringBootApplication public class CcnyApplication{ public static class CustomGenerator implements BeanNameGenerator { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return definition.getBeanClassName(); } } public static void main(String[] args) { SpringApplicationBuilder sab=new SpringApplicationBuilder(CcnyApplication.class) .beanNameGenerator(new CustomGenerator()); sab.run(args); } }
总结:个人感觉这个主要混淆service,很多不太适用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。