The previous article said the approximate process and the effect we want to achieve. This article is mainly about the detailed configuration.
In fact, just once, it feels very simple. Only two files need to be configured.
Pom.xml and ${project.artifactid}-maven.pro These two files can be. Where the Pom.xml configuration plugin is used, the true optimization option is configured in the ${project.artifactid}-maven.pro file.
Let's take a look at the complete pom.xml.
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>Struts2Spring4Hibernate4XML</groupId> <artifactId> Struts2spring4hibernate4xml</artifactid> <packaging>war</packaging> <version>0.0.1- Snapshot</version> <name>struts2spring4hibernate4</name><description>integration of Struts 2, Spring 4 and Hibernate 4 frameworks</description><properties><project.build.sourceencoding >UTF-8</project.build.sourceEncoding><java-version>1.8</java-version>< org.springframework-version>4.1.4.release</org.springframework-version>< Org.strutsframework-version>2.3.20</org.strutsframework-version><org.hibernateframework-version >4.3.8.final</org.hibernateframework-version><org.mysqlconnector-version>5.1.34</org.mysqlconnector-version></properties>< dependencies><!--Spring--><dependency><groupid>org.springframework</groupid>< Artifactid>spring-context</artifactid><version>${org.springframework-version}</version> </dependency><dependency><groupId>org.springframework</groupId><artifactId> spring-context-support</artifactid><version>${org.springframework-version}</version></ Dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-orm </artifactId><version>${org.springframework-version}</version><type>jar</type> <scope>compile</scope></dependency><!--Struts--><dependency><groupid> org.apache.struts</groupid><artifactid>struts2-core</artifactid><version>${ org.strutsframework-version}</version≫</dependency><dependency><groupid>org.apache.struts</groupid><artifactid> struts2-spring-plugin</artifactid><version>${org.strutsframework-version}</version></ dependency><!--Hibernate--><dependency><groupid>org.hibernate</groupid>< Artifactid>hibernate-core</artifactid><version>${org.hibernateframework-version}</version ></dependency><!--Apache Commons DBCP--><dependency><groupid>org.apache.commons</ Groupid><artifactid>commons-dbcp2</artifactid><version>2.0</version></dependency ><!--MySQL Connector-java--><dependency><groupid>mysql</groupid><artifactid> mysql-connector-java</artifactid><version>${org.mysqlconnector-version}</version></ dependency><!--jstl--><dependency> <groupId>jstl</groupId> <artifactId>jstl< /artifactid> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> < Artifactid>standard</artifactid> <version>1.1.2</version> </dependency></ dependencies><build><sourcedirectory>src</sourcedirectory><finalname>abc</ finalname><resources><resource>< Exclude resource files when!--packaging--><directory>resources</directory ><excludes><exclude>*.*</exclude></excludes></resource></resources>< plugins><plugin><artifactid>maven-compiler-plugin</artifactid><version>3.1</ version><configuration><source>1.8</source><target>1.8</target></ Configuration></plugin><plugin><artifactid>maven-war-plugin</artifactid><version >2.4</version><configuration><warSourceDirectory>WebContent</warSourceDirectory> <failonmissingwebxml>false</failonmissingwebxml><packagingexcludes>web-inf/lib/${project.artifactid}-${version}.jar</ packagingexcludes><!--the class file into a jar package--><archiveclasses>true</archiveclasses><!-- Hit the resource file into the classes directory--><webresources><resource><directory>resources</directory>< targetpath>web-inf/classes</targetpath><filtering>true</filtering></resource></ webresources></configuration></plugin><!--BEGIN obfuscate--><plugin><groupid> com.idfconnect.devtools</groupid><artifactid>idfc-proguard-maven-plugin</artifactid>< Version>1.0.1</version><executions><execution><phase>prepare-package</phase> <goals><goal>obfuscate</goal></goals></execution></executions>< Configuration><options><repackageclasses>com.idfconnect.sample.obfuscated</repackageclasses ></options><inputfile>${project.builD.outputdirectory}</inputfile><inputfilefilter>**.class</inputfilefilter><outputartifacts ><outputartifact><file>${project.build.finalname}/web-inf/lib/${project.build.finalname}.jar </file><type>jar</type></outputartifact></outputartifacts><libraryartifacts ><libraryArtifact>junit:junit:4.11</libraryArtifact></libraryArtifacts>< Libraryjarpaths><libraryjarpath>${java.home}/lib/jsse.jar</libraryjarpath></libraryjarpaths ><proguardincludefile>${basedir}/resources/${project.artifactid}-maven.pro</proguardincludefile ></configuration><dependencies><dependency><groupId>net.sf.proguard</groupId> <artifactid>proguard-base</artifactid><version>5.0</version></dependency></ dependencies></plugin><!--END obfuscate--></plugins></build></project>
Three plugins are configured in Pom.xml as shown above:
1.maven-compiler-plugin: Just configure the compiler version that the project uses, nothing to say.
2.idfc-proguard-maven-plugin: Proguard is a plugin under MAVEN. Note that this plugin can only confuse. class files. You can only generate a jar package that cannot generate a war package. This means that we can only mix the. class file and optimize it into a jar package, and then use the Maven-war-plugin plugin to make the project into a war package. So the <phase> in execution should be configured as Prepare-package instead of the package. <outputArtifact> the file type to configure the output is the jar package. prepare-package Before you actually pack, do some of the necessary things to get ready to pack, and here is the obfuscation optimization process. Note <inputFileFilter> is filtering out which files are packaged in the jar package. At this point in our project, the. Hbm.xml is placed under the Resources folder. The jar package we want to generate is only in the. class file. The resource files are in the sibling directory under Web-inf/classes. So this is configured as **.class. Indicates that all. class files under any package are packaged in a jar package. If the. hbm.xml file and the Java file are in one place. You cannot configure
Look, we're going to use <proguardincludefile>${basedir}/resources/${project.artifactid}-maven.pro</proguardincludefile. > indicates where the optimization options file is located. It has a default location, but our MAVEN file structure is streamlined, so it needs to be clearly stated.
The Artifactid of our project is
<artifactId>Struts2Spring4Hibernate4XML</artifactId>
So ${project.artifactid}-maven.pro is Struts2spring4hibernate4xml-maven.pro. Of course, it can be changed to other names, this is to prevent the project Artifactid change the name.
<inputFile> is the input file, which is the MAVEN compiled output path.
<LIBRARYARTIFACT>: Generate additional Injars input entries to Proguard from the project artifacts. (My understanding is through artifact ID to indicate the need to add a confusing jar package)
<inputjarpaths>: Additional external (e.g. non-artifact) input to include to Proguard as Injars parameters (I understand A jar package that needs to be confused by specifying a path)
The relevant configuration options for this plugin please crossing the description of the network: http://mavenproguard.sourceforge.net/obfuscate-mojo.html
3.maven-war-plugin: The following configuration is critical.
<packagingExcludes>WEB-INF/lib/${project.artifactId}-${version}.jar</packagingExcludes>
Specifies certain jar packages that are excluded when the war package is hit.
Make our. class file into a jar package.
<archiveClasses>true</archiveClasses>
Place the resource file under the Web-inf/classes directory.
<webResources></span><resource></span><directory>resources</directory>< /span><targetpath>web-inf/classes</targetpath></span><filtering>true</filtering ></span></resource></webResources>
When hitting the jar package, ignore all files under the Resources folder. This is to implement only the. class file in the jar package. The configuration files are in the same sibling directory under Web-inf/class.
<sourceDirectory>src</sourceDirectory><finalName>abc</finalName><resources>< resource></span>< Exclude resource files when!--packaging--></span><directory>resources</directory></ span><excludes></span><exclude>*.*</exclude></span></excludes></ Resource></resources>
But why should we rule out the Web-inf/lib/${project.artifactid}-${version}.jar jar? Idfc-proguard-maven-plugin has generated a abc.jar for us, why should we set Archiveclasses to true in Maven-war-plugin?
If you don't set these, let's see what the effect is:
See if no, if not set archiveclasses to true. MAVEN will only deploy the pre-mixed files directly to the Web-inf/classes directory. But there is already a Abc.jar in Lib. Abc.jar is a confusing. class file. This is repeated with the. class file under Web-inf/classes.
Note, note that if Archiveclasses is not configured to true. MAVEN will only deploy the compiled files to the Web-inf/classes directory and will not generate the Struts2spring4hibernate4xml-0.0.1-snapshot.jar jar package. It should be the reason why I don't have maven Update,clean. I've been stuck here for a long time. So we strongly recommend that everyone. Change pom.xml on MAVEN >> Update Project, and then Project >> clean. Every time mvn clean, the MVN package refreshes the next item. Remember, these actions seem annoying, but they can't be saved. That's why I discovered it the next day when I restarted the machine and tried again. I think I'm a solid-state drive, fast, and read and write, and it's not really going to happen. The Anti-compilation tool Jd-gui best to close and reopen each time you run out. Don't be too troublesome, too slow. Slow is the cause of the machine, I just can't stand my notebook slow, just bought a desktop.
So we're going to set archiveclasses to true. This will make the. class file before the obfuscation optimization into a jar package (Struts2spring4hibernate4xml-0.0.1-snapshot.jar) instead of deploying it to the Web-inf/classes directory.
But there is already a Abc.jar in Lib. So we're going to configure Packagingexcludes to get rid of this jar package, because we just need a confusing abc.jar on the line.
Summary of the above a short paragraph: Configuration <archiveClasses>true</archiveClasses> is not to let web-inf/classes under the non-obfuscated optimization of the. class file. The packagingexcludes is configured to exclude Struts2spring4hibernate4xml-0.0.1-snapshot.jar from this jar package. Because we just need to go through the confusing optimization of the file Abc.jar on the line.
The end result is this:
Pom.xml said the same, let's talk about our optimization configuration file. Pro File
#保留调试信息 (source line of exception information)-renamesourcefileattribute Sourcefile-keepattributes sourcefile,linenumbertable# Do not form mixed case class names when confusing-dontusemixedcaseclassnames# preserve debug level properties #-keepparameternames# retain annotation information, signature information, exception information, internal class information-keepattributes * annotation*,signature,exceptions,innerclasses# Specifies the confusion when the method and the property name replace the dictionary file #-obfuscationdictionary shakespeare.txt#-keep Class net.codejava.framework.action.**{*;} #-keep class net.codejava.framework.dao.**{*;} #-keep class net.codejava.framework.model.**{*;} -keep public class * {public protected *;} -keepnames class Net.codejava.framework.action.**{<fields>;<methods>;} -keepnames class Net.codejava.framework.dao.**{<fields>;<methods>;} -keepnames class Net.codejava.framework.model.**{<fields>;<methods>;} #保留枚举类方法-keepclassmembers,allowoptimization enum * {public static **[] values (); public static * * VALUEOF (java.lang.String);} #保留所有实现序列化的类的素有属性-keepclassmembers class * Implements java.io.Serializable {private <fields>;} -printusage aaa.txt# OptimizationAllow access to and modify members of classes and classes with modifiers-allowaccessmodification #混淆时应用侵入式重载-overloadaggressively# to determine the member names of a uniform obfuscation class to increase confusion- Useuniqueclassmembernames
There are comments on every sentence here, which is relatively simple. I'll just say, "Keepnames and Keepattributes."
Names: We know the method name, field name and so on.
Attritutes: My understanding is the descriptive nature of the file. Official explanations are here: http://proguard.sourceforge.net/manual/attributes.html
Keepattributes, the official website has the description, generally added. How to configure the. Pro file, please refer to the official website document: http://proguard.sourceforge.net/
Keepnames indicates which name does not need to be renamed. This depends on your needs to decide whether or not to retain the original name.
Some fields, such as the UserService property in action. Some fields referenced by the EL expression in the JSP. Their names are changed as long as the getter and setter do not change. You can ensure that your code is running correctly.
It is important to note that these names interact with XML files or JSP pages. So be very careful when optimizing them.
Here's an example to see how the optimization works. This is the garbage code I intentionally added:
Private variables and methods have been optimized.
Look at the method in CDE. The variable j is replaced directly with an immediate number, and the parameter name in the method is also replaced. Note Because the-keepnames class net.codejava.framework.action.**{is configured
<fields>;
<methods>;
}
So the variable name in action is not confused with a,b,c ....
Summarize:
The degree of confusion and optimization now depends on how well you understand the. Pro file configuration. Try to write some garbage code, and see how you can optimize to what extent, don't forget to always update,clean,refresh.
This configuration is relatively simple to understand, mainly to try more. Personally feel that the difficulty is to fight the war package will be a little trouble. The article on the net on this is relatively few, said is not clear. Sum up your personal experience and hope to help you. Hopefully you'll be able to streamline the MAVEN project file structure and do confusing optimizations for apes rather than just coding.
Demo1 address (. Java,.hbm.xml together): http://download.csdn.net/detail/ahau10/9500953
Demo2 address (. Java,.hbm.xml not together): http://download.csdn.net/detail/ahau10/9500960
Using Idfc-proguard-maven-plugin obfuscation to optimize Jave Web Engineering II