maven集成eclipse android项目办法总结
经过不断的尝试,总结出一个maven项目在eclipse android环境下的集成办法。
集成的复杂性和一些问题的权衡
这个集成办法,最大的问题是比较复杂:
- eclipse集成了android adt插件;
- eclipse集成m2eclipse插件;
- maven使用了android部署的插件——maven android plugin;
- 为了把maven和eclipse集成起来的eclipse插件——m2eclipse android plugin。
这些插件关联,可以认为是串联,如果出现问题,就会影响整个软件项目。结果就碰到问题了,见关注m2eclipse集成android的eclipse插件bug,这种bug较难解决,因为要解决问题,该插件(m2eclipse-android-plugin)作者要向上游一个一个插件的排查,学习和了解他们的东西。
我原打算等待bug修改后再向下进行,但是4天了,bug还没确定问题的原因。也许哪天m2eclipse升级了,问题自然得到解决。
也曾经打算用其他办法。因为android官方支持使用ant脚本自动构建,也尝试了使用ant+ivy+ivyde的方式实现maven类似的功能。
结果是,ivy和ivyde很好的解决了类库自动依赖问题。但只能在eclipse项目中解决问题,如果使用android的ant task打包APK,不能自动将依赖类库打入包内。因此放弃。
还是用maven的方式吧。
使用maven集成的一些要点
首先是安装和环境的配置:
- eclipse项目集成maven,见为android项目集成maven
- eclipse增加m2eclipse android integration插件,目的是集成maven和eclipse adt,见[2728],我一般也不用m2eclipse,因为不需要,但是这个m2eclipse android integration插件需要,文档见[2728]
然后,针对[2732]提到的bug,我这样解决的:
取消项目的自动构建。测试了一下,这对android项目没啥影响,反正部署和运行android的项目,它自带的编译器还是要工作的。
最后一个问题,要能在工作机上打包debug签名的APK包,在服务器端,比如ubuntu server端,不借助eclipse环境下,通过服务器端全局设置,打包release签名的正式APK包。这里有个要求,pom.xml文件是一致的,在工作机上和在服务器上。不一样的是maven的settings.xml文件。
我把自己的GolfDemo生成了这样的项目,源代码见:
主要看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.easymorse</groupId> <artifactId>GolfDemo</artifactId> <version>1.2</version> <packaging>apk</packaging> <name>GolfDemo</name> <dependencies> <dependency> <groupId>android</groupId> <artifactId>android</artifactId> <version>2.1_r1</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.3.1</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId> com.jayway.maven.plugins.android.generation2 </groupId> <artifactId>maven-android-plugin</artifactId> <version>2.3.3</version> <configuration> <sdk> <path>${env.ANDROID_HOME}</path> <platform>7</platform> </sdk> <deleteConflictingFiles>true</deleteConflictingFiles> </configuration> <extensions>true</extensions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>sign</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jarsigner-plugin</artifactId> <version>1.2</version> <executions> <execution> <id>signing</id> <goals> <goal>sign</goal> </goals> <phase>package</phase> <inherited>true</inherited> <configuration> <archiveDirectory></archiveDirectory> <includes> <include>target/*.apk</include> </includes> <keystore>${keyFilePath}</keystore> <storepass>${storePassword}</storepass> <keypass>${keyPassword}</keypass> <alias>${keyAlias}</alias> </configuration> </execution> </executions> </plugin> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>maven-android-plugin</artifactId> <inherited>true</inherited> <configuration> <sign> <debug>false</debug> </sign> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
这个项目,如果这样:
mvn clean install
将生成debug签名的APK包,如果:
mvn clean install android:deploy
将会生成debug签名包,然后部署到手机设备上。
如果:
mvn clean install –Psign
将会激活pom.xml文件中的id为sign的profile,即使用正式签名的插件配置信息,这里的配置信息,比如密码,签名文件路径等,都是属性变量,需要在用户的settings.xml文件中设置。比如:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sign</id>
<properties>
<keyFilePath>/Users/user_name/key/your.release.keystore</keyFilePath>
<storePassword>your_password</storePassword>
<keyPassword>your_password</keyPassword>
<keyAlias>your_alias</keyAlias>
</properties>
</profile>
</profiles>
</settings>
这篇文章上的评论的 RSS feed TrackBack URI