Android使用gradle的管理本地Maven服务器

匿名 (未验证) 提交于 2019-12-03 00:18:01

1 环境说明

操作系统 Ubuntu 14.04(x64) maven   maven3.3.9 jdk     jdk-1.8.0_65 nexus   nexus2.11.4 gradle  gradle2.9 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2安装

分别解压缩,然后添加进PATH环境变量。 
  • 1
  • 2

3使用

  1. 使用maven

    $M2_HOME/conf/settings.xml:    Maven全局配置 ${USER_HOME}/.m2/settings.xml: 用户的全局配置(优先)  <repositories>     <repository>         <id>central</id>         <name>Central Repository</name>         <url>http://repo.maven.apache.org/maven2</url>         <layout>default</layout>         <snapshots>             <enabled>false</enabled>         </snapshots>     </repository> </repositories>  <pluginRepositories>     <pluginRepository>         <id>central</id>         <name>Central Repository</name>         <url>http://repo.maven.apache.org/maven2</url>         <layout>default</layout>         <snapshots>             <enabled>false</enabled>         </snapshots>         <releases>             <updatePolicy>never</updatePolicy>         </releases>     </pluginRepository> </pluginRepositories>  其参数:localRepository 本地repository路径         interactiveMode:是否需要和用户交互以获得输入,true:需要         usePlugRegistry:是否需要plugin-registry.xml文件来管理插件版本。         offline 是否需要在离线模式下运行-->不能连接网络服务器时,有用         proxy   代理相关设置         server  服务器相关设置         mirrors 为仓库列表配置的下载镜像列表         repositories    远程仓库列表,是maven用来填充构建系统本地仓库所使用的一组远程项目
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  2. 使用nexus

    nexus start     启动服务 nexus stop  停止服务
    • 1
    • 2

    服务启动后,访问地址为:http://localhost:8081/nexus,系统管理员为admin/admin123


  3. nexus里面可以配置的仓库有三种:

    proxy   远程仓库的代理,当用户向这个proxy请求一个artifact,proxy首先会再本地找,         找不到再从远程服务器下载,然后返回 hosted  宿主仓库,用户可以把自己的一些构件,deploy到hosted中 group   仓库组,nexus的概念,多仓库聚合。
    • 1
    • 2
    • 3
    • 4


    配置Hosted Repository,Deployment Policy选项,Snapshots会配置成allow,其他的设置为disable。

    3rd party   用以保存第三方jar Snapshpts   用以项目组内部的快照 Release 用以项目内部的发布版 
    • 1
    • 2
    • 3
    • 4

    group是一个虚拟的仓库,通过对实体仓库(proxy.hosted)进行聚合,对外暴漏统一地址。


  4. 配置了一个server,其中增加了权限相关信息,

    <?xml version="1.0" encoding="UTF-8"?>  <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">     <servers>         <server>             <id>nexus-snapshots</id>             <username>admin</username>             <password>admin123</password>         <server>     <servers>      <mirrors>         <mirror>             <id>nexus</id>             <name>internal nexus repository</name>             <url>http://192.168.3.5:8081/nexus/content/groups/public/</url>             <mirrorOf>central</mirrorOf><!--这里只镜像central,可以改为*,匹配全部-->         </mirror>     </mirrors> <settings>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

以上是直接使用maven的方式配置,对于Android而言,可以直接使用gradle托管,方便很多,具体如下:


  1. 方式一:每个项目更改,build.gradle文件

    allprojects {     repositories {         maven{         credentials{             username    'admin'             password    'admin123'         }         url 'http://192.168.3.5:8081/nexus/content/groups/public/'         }         //jcenter()         //mavenLocal()     } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    方式二:在USER_HOME/.gradle/目录下添加init.gradle文件,内容如下:

    allprojects{ repositories {     def REPOSITORY_URL = 'http://192.168.3.5:8081/nexus/content/groups/public/'     all { ArtifactRepository repo ->         if(repo instanceof MavenArtifactRepository){             def url = repo.url.toString()             if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {                 project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."                 remove repo                 }             }         }         maven {             url REPOSITORY_URL         }     } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    init.gradle是Gradle的初始化脚本,也是运行时全局配置。

  2. 发布到maven

    修改build.gradle,添加

    apply   plugin:'maven' uploadArchives{     repositories{         mavenDeployer{         pom.groupId = "com.felix.test"         pom.version = "1.0.0"          repository(url: "http://192.168.3.5:8081/nexus/content/groups/public/")         {         authentication(userName: admin, password: admin123)          }         }     } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    然后执行 gradle uploadArchives 上传到本地库。

  3. gradle浅析



    build.gradle 配置文件,负责构建的定义。每个module对应一个gradle文件

  4. project层的build.gradle分析

    buildscript {     repositories {  //构建过程中依赖的仓库,这里是指gradle脚本需要的资源位置。         jcenter()        }     dependencies {  //构建过程中依赖的库         classpath 'com.android.tools.build:gradle:1.5.0'  //申明gradle的版本     } } allprojects {   //这里配置当前project中每个module公有的依赖仓库,这样每个module不用单独配置。这里是项目需要的仓库资源位置。     repositories {         jcenter()     } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  5. 某个module中的build.gradle

    apply plugin: 'com.android.application'  //申明为Android APP  android {//android构建时需要的参数配置     compileSdkVersion 23    //sdk版本     buildToolsVersion "23.0.1"  //buildtool版本,以上两个版本,都需要你sdk中相应内容已经安装。否则会报错。(当然也可以改成你已经安装的版本)      defaultConfig { //默认配置,会同时应用到debug和release版本         applicationId "com.felix.testweatherfish"  //app包名,apply plugin中指定当前是com.android.application,才能包含此项。         minSdkVersion 15    //最低支持的SDK版本         targetSdkVersion 23 //目标(编译)SDK版本         versionCode 1       //app版本         versionName "1.0"   //app 版本号         multiDexEnabled true    //支持多dex,具体见后续分析     }      sourceSets {    //目录映射关系说明         main {  //系统默认的source set         manifest.srcFile 'AndroidManifest.xml'         java.srcDirs = ['src']         res.srcDirs = ['res']          compile         }         api{             //这里若是不设置 java.srcDirs 则默认使用${path-to-project}/src/api/java目录。             //这里若是不设置 res.srcDirs 则默认使用${path-to-project}/src/api/resources目录。             //当然也可以使用类似main中自定义目录。             //编译的时候使用gradle apiClasses            }     }      repositories{//编译本地aar依赖,最终需要在dependencies中添加引用         flatDir{             dir 'aarlib'    //aar依赖文件所在目录         }     }      signingConfigs {    //签名配置         HetConfig { //签名配置标签,可以有多个         //建议密码不要明文写这里,写在gradle.properties中,再通过storeFile引用         storeFile file("Het_KeyStore") //签名文件         storePassword "szhittech"   //签名密码         keyAlias "Clife"            //应用别名         keyPassword "szhittechclife"  //应用别名密码         }     }      lintOptions {   //移除lint检查的error         abortOnError false     }      compileOptions{ //编译选项,以下指定了Java版本         sourceCompatibility javaVersion.VERSION_1_7         targetCompatibility javaVersion.VERSION_1_7     }      buildTypes {    //这里可以配置debug和release版本的一些参数,比如混淆,签名等         release {   //指定release版本编译的特殊参数             minifyEnabled false //是否混淆,混淆的时候会去掉一些无用的方法等             shrinkResources true    //压缩资源,会去掉无用的资源文件             //混淆使用的文件,前面部分是系统默认的Android混淆文件,             //此文件位于sdk目录/tools/proguard/proguard-android.txt.              //后面试项目中自定义的混淆文件,两部分一起配合使用来混淆工程。             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'           signingConfig  signingConfigs.HetConfig //使用上面定义的签名配置文件         }         debug{         //debug版本一般使用默认签名就可以         //默认签名文件位于${home}/.android/debug.keystore         //默认签名的引用方法 signingConfig  android.signingConfig.debug         }     } }  task HelloWorld <<{ //定义一个任务,具体分析见下面     println "Hello World!" }  task copyFile(type: Copy){  //自定义任务,将xml文件夹下的所有内容拷贝到destination。这两个目录都是build.gradle所在目录下的目录。     from 'xml'     into 'destination'     description "test"  //描述信息 }  task taskA(dependsOn: copyFile){         //taskA依赖于copyFile这个task,     //即在执行gradle taskA的时候,会先执行 gradle copyFile      def source = fileTree('srcDirs')    //文件夹和文件位置定义     def destination = file('destination.txt')      //设置任务的输入输出路径,以实现增量编译     inputs.dir source     outputs.file destination      doLast{         destination.withPrintWriter {writer ->             sources.each {source ->             writer.println source.text             }         }     } }  dependencies {  //当前模块的依赖     compile fileTree(dir: 'libs', include: ['*.jar']) //编译lib目录里面的所有*.jar文件     testCompile 'junit:junit:4.12'     compile 'com.android.support:appcompat-v7:23.1.1'     provided 'com.android.support:design:23.1.1' //编译时依赖,但不包含     compile project(':test:SimpleAndroid') //编译test目录下的SimpleAndroid     compile 'com.android.common:baseprj:23.1.1@aar' //编译依赖第三方的aar文件     compile (name:'xxx', ext:'aar')编译本地aar依赖文件     compile (group:'xxxx', name: 'xxxx', version: 'xxxx'){         exclude group:'xxx', module:'xxx'  //排除依赖,解决依赖冲突,module对应的就是artifactId。     }     compile 'com.android.support:multidex:1.0.0' //支持多dex,具体见后续分析 }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    解析说明:

    apply plugin: ''

    com.android.application 申明为Android APP com.android.library     申明为Android Library maven   使用maven作为jar/aar包的依赖管理,通过maven仓库下载项目依赖 war     指定web项目编译(build)生成war包 java    指定项目为Java项目,项目编译时生成jar包  jetty   加入jetty支持,代码修改后直接 gradle jettyRun即可运行项目 可以添加自定义插件 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    defaultConfig中涉及到除了上面例子中内容外,还有

    siginingConfig/proguardFiles等        
    • 1
    • 2


    首先在defaultConfig中,设置multiDexEnabled true
    然后,在dependencies中添加依赖compile 'com.android.support:multidex:1.0.0'
    最后,在application中,复写attachBaseContext方法

    ``` @Override protected void attachBaseContext(Context base) {     super.attachBaseContext(base);     MultiDex.install(this); } ``` MultiDex这部分内容加载是在静态方法之后,故若application中如果有静态方法,不要引用非第一个dex文件中的方法。而且编译的buildtool必须是21以上。 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    自定义任务task说明

    可以通过gradle tasks 方法查询当前定义的所有task gradle properties   查看project中定义的property gradle dependencies 查看当前project的依赖关系 <<  表示向HelloWorld中加入执行代码(groovy代码) 
    • 1
    • 2
    • 3
    • 4
    • 5
  6. wrapper目录下的gradle-wrapper.properites文件

    该文件申明了gradle目录与下载路径以及当前项目使用的gradle版本。

  7. setting.gradle

    include ':app',':test:SimpleAndroid'
    • 1

    主要用于申明需要加入gradle的module。

  8. gradle任务




    clean 清理输出任务

  9. 关于gradle更多资料,可以参考这篇博客


  10. 发布SNAPSHOT版本的时候REPO_RELEASE = ‘false’ 否则REPO_RELEASE =’true’.

uploadArchives{         repositories{             def REPO_RELEASE = 'false'             if(REPO_RELEASE == 'true'){                 mavenDeployer{                 pom.groupId = "com.felix.test"                 pom.version = "1.0.0"                  repository(url: "http://192.168.3.5:8081/nexus/content/groups/public/")                 {                 authentication(userName: admin, password: admin123)                  }             }else{             mavenDeployer{                 pom.groupId = "com.felix.test"                 pom.version = "1.0.0-≈"                  snapshotRepository(url: "http://192.168.3.5:8081/nexus/content/groups/public/")                 {                 authentication(userName: admin, password: admin123)                  }             }         }     } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  1. 解决gradle配置完成后,REPO_RELEASE无法自动获取最新版本
compile group:"com.android.common",name:"BaseProject",version:"1.0.0-SNAPSHOT",changing:"true"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!