[Switch] Use Gradle and Sonatype Nexus to build a private maven warehouse

Please indicate the source for reprint:  http://blog.csdn.net/u011974987/article/details/52372185 

Foreword:

As the business grows and the demand increases, we have more and more App components. Almost most of the components use the same third-party libraries and libraries packaged inside the company, and everyone in the team repeats from maven The remote central warehouse is downloaded and constructed, which will increase the load of the warehouse and waste the bandwidth of the external network. If the network speed is slow, it will take a long time, half an hour, a few hours, it is possible! This obviously affects the development progress of the project. Some companies still develop on the intranet, and what if they cannot connect to the central warehouse? How can the public components developed within the company be shared by other projects? At this time, we had to build a private server maven repository for our team to improve our development efficiency.

At first, I was not very familiar with gradle and maven, and I was confused after reading the online tutorials. Later, after spending some time, I found that the whole process is still very simple. ↓↓

First let's find out,

1. What is Maven, Gradle?

Maven  is a project management and automated build tool. The place where Maven packages are stored centrally is the Maven repository. These repositories can be placed locally or on a remote server. It can be a private repository or a public one. Below is the list of libraries for development:

 

mavenCentral();
jcenter ()
maven {
     url 'file:///Users/my-user-name/Documents/Android/repo/'
}
maven {
    url 'http://localhost:8081/nexus/content/repositories/releases/'
}

 

Android  Studio Gradle mainly supports two Maven central repositories: mavenCentral and jcenter.

  • mavenCentral is the earliest maven central repository
  • jcenter is the default maven central repository from Android Studio 0.8
  • The third is my local repository
  • The fourth is the private warehouse deployed by the author on the intranet server

Gradle  is a project automation build tool based on Apache Ant and Apache Maven concepts. It uses a Groovy-based domain-specific language to declare project settings instead of traditional XML

2. Use Nexus to build maven private server

1. Nexus download and install:

Official website download address: http://www.sonatype.org/nexus/go/ , my development environment is Windows, and I downloaded All platforms nexus-2.13.0-01-bundle under Nexus Repository Manager OSS 2.xx .zip compressed file. ↓ 
write picture description here

2. Nexus startup:

After the download is complete, decompress and enter \nexus-2.1.2-bundle\nexus-2.1.2\bin\jsw\, select the folder according to the operating system type, I chose the windows-x86-32 folder, after entering, you can See the bat file as shown below. 
write picture description here

Double-click console-nexus.bat to run. Enter http://127.0.0.1:8081/nexus/ in the browser, and the picture (2) appears, which means that nexus has been successfully started.

write picture description here 
figure 2)

8081是默认的端口号,要修改端口号,进入\conf\打开nexus.properties文件,修改application-port属性值就可以了。 
默认的用户名和密码分别是:admin和admin123。点击右上角的log in 登录后如图所示: 
点击左侧的 repositories 查看现有的仓库列表: 
write picture description here

3.Nexus仓库:

这里的仓库分了四种类型

  1. hosted(宿主仓库):用来部署自己,第三方或者公共仓库的构件
  2. proxy(代理仓库):代理远程仓库
  3. virtual(虚拟仓库):默认提供了一个 Central M1虚拟仓库 用来将maven 2适配为maven 1
  4. group(仓库组):统一管理多个仓库

Public Repositories: 仓库组

3rd party: 无法从公共仓库获得的第三方发布版本的构件仓库

Apache Snapshots: 用了代理ApacheMaven仓库快照版本的构件仓库

Central: 用来代理maven中央仓库中发布版本构件的仓库

Central M1 shadow: 用于提供中央仓库中M1格式的发布版本的构件镜像仓库

Codehaus Snapshots: 用来代理CodehausMaven 仓库的快照版本构件的仓库

Releases: 用来部署管理内部的发布版本构件的宿主类型仓库

Snapshots:用来部署管理内部的快照版本构件的宿主类型仓库

4.建立Nexus宿主仓库

新建一个内部仓库,步骤为Repositories –> Add –> Hosted Repository,在页面的下半部分输入框中填入Repository ID和Repository Name即可,另外把Deployment Policy设置为Allow Redeploy,点击save就创建完成了。这里我点击添加宿主类型的仓库,在仓库列表的下方会出现新增仓库的配置,如下所示: 
write picture description here

建立好新的仓库之后需要配置一下相关账号信息.在安全选项下选择用户选项,可以看到三个默认的账号,分别是管理员账号,部署账号和Nexus账号.正常访问仓库内容的时候是不需要这三个账户的,一般也就是把部署账号暴露出去,方便仓库项目维护人员部署项目使用.所以这里可以用默认的Deployment账户(记得重置下密码).也可以新建一个账号来使用,新建的时候可以通过add role management来控制该账号的权限。 
点击新建的仓库的url可以直接如今仓库的路劲,因为现在还没有部署项目,所以是空的仓库。

至此,搭建私服的maven仓库就已经完成,下面继续介绍Android 端在AS 上面的应用。

三、上传库到Maven仓库

上传库到maven仓库有两种方式,我们先来介绍第一种: 
1. 首先我们创建一个新的AndroidStudio 项目,然后新建一个module,选择Android Library。

write picture description here

然后,我们随便写一个功能供别人使用。例如我写一个ToastUtils:

write picture description here

然后RebuildProject生成依赖的arr包。

2.在MavenRepoDemo项目的根目录的build.gradle中配置刚刚建立的仓库:

 

allprojects {
    repositories {
        jcenter()
        maven{ url 'http://localhost:8081/nexus/content/repositories/releases/'}

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

 

    1. Configure the gradle.properties file in the same directory to define common properties, so that if there are multiple libraries to be deployed, there is no need to modify the configuration in each library:
#Maven repository URL
MAVEN_REPO_RELEASE_URL=http://localhost:8081/nexus/content/repositories/releases/
MAVEN_REPO_SNAPSHOT_URL=http://localhost:8081/nexus/content/repositories/snapshots/
#Corresponding to the value of maven's GroupId
GROUP = common
#Login the username of nexus ossde
NEXUS_USERNAME=admin
#login to nexus oss password
NEXUS_PASSWORD=admin123
# groupid
GROUP_ID = common
# type
TYPE = ear
# description
DESCRIPTION = This is Toast lib

The warehouse here I use is the original warehouse of Nexus (you can replace it with the newly created warehouse address).

    1. Modify the build.gradle file corresponding to the module and add the following configuration:
apply plugin: 'com.android.library'

apply plugin: 'maven'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    lintOptions {
        abortOnError false
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

uploadArchives {
    configuration = configurations.archives
    repositories {
        mavenDeployer {
            snapshotRepository(url: MAVEN_REPO_SNAPSHOT_URL) {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            repository(url: MAVEN_REPO_RELEASE_URL) {
                authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
            }
            pom.project {
                version '1.0.0'
                artifactId 'toastutils-lib'
                groupId GROUP_ID
                packaging TYPE
                description DESCRIPTION
            }
        }
    }
}

artifacts {
    archives file('toastutils.aar')
}
  1. 在as右边栏,找到Gradle打开如下: 
    write picture description here

然后双击uploadArchives,编译脚本并上传arr文件到私有仓库,最后在控制台可以看到日志是否上传成功。

可以去仓库查看到刚刚上传的库文件:

write picture description here

第二种,就是直接通过Nexus直接上传,这种就不详细说了,有兴趣的自己去研究下吧! 嘿嘿

四、在项目中应用

  1. 在项目的根目录build.gradle配置如下:

write picture description here

  1. 在app目录下的build.gradle配置如下:

write picture description here

这样我们就完工了。在项目中调用我们库了,别人按照上面的配置就可以引用库使用了。

附上Demo的GitHub项目源码MavenRepoDemo

对于频繁更新的子项目是否适合采用这种方式。因为每次变动都需要上传,而主项目在引用该AAR的时候则需要每次都去检查是否更新, 这会使得编译时间大大增加,有了这个maven库,就不用那么麻烦了。

想了解更多有关的资料:

Nexus private server makes Maven more powerful

Embrace the androidStudio series articles blog is relatively complete

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326467608&siteId=291194637