maven 學習筆記(四)-創建簡單的eclipse+maven工程(簡單的java項目打包爲jar並混淆jar文件)

    1、按前面例子創建一個簡單的java項目

     2、創建項目後,項目結構如下圖:

   

     2、pom.xml文件配置內容如下:

<?xml version="1.0" encoding="UTF-8"?><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>com.special.test</groupId>

	<artifactId>simple-test4</artifactId>

	<version>0.0.1-SNAPSHOT</version>

	<name>simple-test4 - Application</name>

	<packaging>jar</packaging>



	<dependencies>

		<dependency>

			<groupId>org.apache.commons</groupId>

			<artifactId>lang</artifactId>

			<version>2.4</version>

		</dependency>


	</dependencies>


	<properties>

		<project.build.name>simple-demo4</project.build.name>

		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

	</properties>


	<build>

		<finalName>${project.artifactId}-${project.version}</finalName>

		<sourceDirectory>src/main/java</sourceDirectory>

		<resources>

			<resource>

				<directory>${basedir}/src/main/java</directory>

				<excludes>

					<exclude>**/*.java</exclude>

				</excludes>

			</resource>

		</resources>


		<plugins>

			<plugin>

				<artifactId>maven-compiler-plugin</artifactId>

				<version>3.1</version>

				<executions>

					<execution>

						<phase>compile</phase>

					</execution>

				</executions>

				<configuration>

					<source>1.6</source>

					<target>1.6</target>

					<encoding>${project.build.sourceEncoding}</encoding>

				</configuration>

			</plugin>


			<plugin>

				<artifactId>maven-source-plugin</artifactId>

				<version>2.1</version>

				<configuration>

					<!-- <finalName>${project.build.name}</finalName> -->

					<attach>true</attach>

					<encoding>${project.build.sourceEncoding}</encoding>

				</configuration>

				<executions>

					<execution>

						<phase>compile</phase>

						<goals>

							<goal>jar</goal>

						</goals>

					</execution>

				</executions>

			</plugin>


			<!-- clean插件 -->

			<plugin>

				<artifactId>maven-clean-plugin</artifactId>

				<version>2.4.1</version>

				<configuration>

					<filesets>

						<fileset>

							<directory>${project.build.directory}</directory>

						</fileset>

					</filesets>

				</configuration>

			</plugin>


			<plugin>

				<groupId>org.apache.maven.plugins</groupId>

				<artifactId>maven-jar-plugin</artifactId>

				<version>2.3.1</version>

				<configuration>

					<archive>

						<!-- 不把pom.xml打入jar中 -->

						<addMavenDescriptor>false</addMavenDescriptor>

						<manifestFile>

							${basedir}/src/main/META-INF/MANIFEST.MF

						</manifestFile>

						<manifest>

							<!-- <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> -->

						</manifest>

					</archive>

				</configuration>

			</plugin>


		</plugins>

	</build>



	<profiles>


		<!-- 打包混淆 -->

		<profile>

			<id>proguard</id>

			<activation>

				<activeByDefault>true</activeByDefault>

			</activation>


			<properties>

				<project.build.release>false</project.build.release>

				<project.build.obfuscate>true</project.build.obfuscate>

			</properties>

			<build>

				<!-- <filters> <filter>src/main/resources/env-debug.properties</filter> 

					</filters> -->


				<plugins>

					<plugin>

						<groupId>com.pyx4me</groupId>

						<artifactId>proguard-maven-plugin</artifactId>

						<version>2.0.4-SONATYPE</version>

						<executions>

							<execution>

								<phase>package</phase>

								<goals>

									<goal>proguard</goal>

								</goals>

							</execution>

						</executions>

						<configuration>

							<obfuscate>${project.build.obfuscate}</obfuscate>

							<proguardInclude>${basedir}/proguard-project.txt</proguardInclude>

							<release>${project.build.release}</release>


							<libs>

								<!-- <lib>libs/commons-lang-2.4.jar</lib> <lib>libs/commons-lang-2.4.jar</lib> -->

							</libs>


						</configuration>

					</plugin>

				</plugins>

			</build>

		</profile>



		<profile>

			<!-- 上線發佈(不混淆jar) -->

			<id>release</id>

			<activation>

				<activeByDefault>false</activeByDefault>

			</activation>

			<properties>

				<project.build.obfuscate>false</project.build.obfuscate>

				<project.build.release>true</project.build.release>

			</properties>

			<build>

				<!-- <filters> <filter>src/main/resources/env-release.properties</filter> 

					</filters> -->

				<plugins>

				</plugins>

			</build>

		</profile>

	</profiles>

</project>





   5、混淆文件配置:proguard-project.txt文件配置內容如下:

# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}


#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
# ----------------------------------  
#  通過指定數量的優化能執行  
#  -optimizationpasses n  
# ---------------------------------- 
-optimizationpasses 5
#確定統一的混淆類的成員名稱來增加混淆 
-useuniqueclassmembernames

#優化時允許訪問並修改有修飾符的類和類的成員   
-allowaccessmodification 


#不優化泛型和反射
-keepattributes Signature
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-adaptresourcefilenames **.properties
-adaptresourcefilecontents **.properties,META-INF/MANIFEST.MF,META-INF/LICENSE.txt,META-INF/NOTICE.txt

# ----------------------------------  
#   混淆時不會產生形形色色的類名   
#   -dontusemixedcaseclassnames  
# ---------------------------------- 
-dontusemixedcaseclassnames
#      指定不去忽略非公共的庫類  
#  -dontskipnonpubliclibraryclasses  
# ---------------------------------- 
-dontskipnonpubliclibraryclasses
# ----------------------------------  
#       不預校驗  
#    -dontpreverify  
# ----------------------------------  
-dontpreverify
 #這1句是屏蔽警告,腳本中把這行註釋去掉
-ignorewarnings
# ----------------------------------  
#      輸出生成信息  
#       -verbose  
# ----------------------------------  
-verbose  
  
#混淆時應用侵入式重載   
-overloadaggressively   
   
#優化時允許訪問並修改有修飾符的類和類的成員   
-allowaccessmodification  
#確定統一的混淆類的成員名稱來增加混淆   
-useuniqueclassmembernames  

#優化
#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*


-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}

#第三分庫不混淆
-keep class android.support.v4.** {*;}
-keep class org.apache.http.entity.mime.** {*;}

#使用 gson 需要的配置
-keep class com.google.gson.JsonObject { *; }


#-dontwarn org.apache.commons.validator.**


-keepclasseswithmembers class * {
    public <init>(android.content.Context,android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context,android.util.AttributeSet,int);
}

-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

-keep class * extends android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

# Keep - Library. Keep all public  classes, fields, and methods.
-keep public class * {
    public <fields>;
    public  <methods>;
}
# Keep - Library. Keep all public and protected classes, fields, and methods.
#-keep public class * {
#   public protected <fields>;
#    public protected <methods>;
#}

# Also keep - Enumerations. Keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum  * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# Keep names - Native method names. Keep all native class/method names.
-keepclasseswithmembers,allowshrinking class * {
    native <methods>;
}



#加載依賴庫
-libraryjars libs/commons-lang-2.4.jar
#-libraryjars libs/commons-validator-1.4.0.jar


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章