maven 学习笔记(四)-创建简单的eclipse+maven工程(简单的java项目打包为jar并混淆jar文件)

原创
2015/02/10 17:16
阅读数 606

    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



   6、运行混淆打包结果如图中目录target:



       6、demo地址下载链接地址如下:

       https://github.com/spring5555/mvn-android-simple-demo3



展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部