Java系列--第二篇 基于Maven的Android开发HelloAndroidWorld

曾经写过一篇Android环境配置的随笔,个人感觉特繁琐,既然有Maven,何不尝试用用Maven呢,经网上搜索这篇文章但不限于这些,而做了一个基于Maven的Android版的Hello Android World.

1, 前提安装sdk以及adt这些就不讲了。网上有的下,我这里的使用的插件是ADT-22.0.5, 要保持最新,可以关注一下官网。贴一张我的SDK Manager图在这里感觉一下。

http://images.cnblogs.com/cnblogs_com/SLKnate/515382/o_android1.5-4.3.png

2,生成MavenQuickStart项目,名为HelloMavenAndroid.

mvn archetype:generate -DarchetypeArtifactId=android-quickstart -DarchetypeGroupId=de.akquinet.android.archetypes -DarchetypeVersion=1.0.11 -DgroupId=com.vanceinfo.android -DartifactId=HelloMavenAndroid

3,导入Kepler, 修改一下.project文件如下,默认只有org.eclipse.jdt.core.javabuilder和org.eclipse.m2e.core.maven2Builder, nature节点中也只有org.eclipse.jdt.core.javanature和org.eclipse.m2e.core.maven2Nature,我在这里折腾了一天才发现加上com.android.ide.eclipse.adt.ResourceManagerBuilder,com.android.ide.eclipse.adt.PreCompilerBuilder,com.android.ide.eclipse.adt.ApkBuilder以及com.android.ide.eclipse.adt.AndroidNature才能算是真正的android项目。

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>HelloMavenAndroid</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>com.android.ide.eclipse.adt.ApkBuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>com.android.ide.eclipse.adt.AndroidNature</nature>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>
.project

4, 修改完上面的,就可以在eclipse里面使用maven clean 和maven install了。但不知为什么在cmd下面执行mvn install会失败。由于这篇文章实在把人弄伤了,而实际上没有什么实质性的代码,所以就不上传源码了。

5, 继续分析,可能是我根据M2E plugin execution not covered在处理以下错误时

    Multiple annotations found at this line:
    - Plugin execution not covered by lifecycle configuration: 
     com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.1:proguard (execution: default-
     proguard, phase: process-classes)
    - Plugin execution not covered by lifecycle configuration: 
     com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.6.1:generate-sources (execution: 
     default-generate-sources, phase: generate-sources)
android-maven-plugin_lifecycle

http://images.cnblogs.com/cnblogs_com/SLKnate/515382/o_yello_lifecycle.png

 加了以下plugin的原因,因为如果由eclipse自动fixed的话,会加上

<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->这一句话的。

<plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId> com.jayway.maven.plugins.android.generation2</groupId>
                                        <artifactId> android-maven-plugin</artifactId>
                                        <versionRange> [3.6.1,)</versionRange>
                                        <goals>
                                            <goal>generate-sources</goal>
                                            <goal>proguard</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
...

 6,2013/10/10日无意间在Kepler上面发现内置的m2e是3.0.4版本的maven.

Window->Preferences->Maven->Installations->Embedded(3.0.4/1.4.0.20130531-2315)

于是我果断去到官网的binaries里下载回了3.0.4版本了。然后环境变量中将maven的变量指向了这个刚下载的旧版本,于是第4点出现的错误解决了。也就是可以用

mvn clean package android:deploy android:run

了。当然之前我也起过将内置的3.0.4的m2e改成3.1.0的,但可惜的是我没有找到最新的插件,在这里 看到的1.4版本就是最新版本,所以只好退回用旧版本Maven了。 

原文地址:https://www.cnblogs.com/SLKnate/p/Java_Serial_Maven_Android_HelloWorld.html