Anroid builds Maven private service (Android Studio)

1. Scenario

  ① There are multiple projects in the company, which are developed by multiple people. A certain module in development needs to be depended on by modules of several other projects. The simplest and most rude way is to package it into aar or jar files and copy them to libs one by one for dependencies, but This method is too cumbersome and requires updating the code every time. It's even more troublesome if there are multiple such libraries. Therefore, according to the project scale and demand, in order to speed up the development efficiency of the team, it is necessary to build a Maven private server.

2. Build

  Through Rexus, you can quickly and easily build a Maven private server. This article describes how to build a Maven private server through Rexus, upload library files, and upgrade library files. To start building, first download Rexus:

  Official website address: https://www.sonatype.com/download-oss-sonatype

  Network disk link:    https://pan.baidu.com/s/1A7RW_PEh4KuB1w4xpfaaYQ Password: uyev (Window x64)

  Installation steps: (The installation method of different versions will be different, this is for the 3.x version)

  ① Download the compressed package, decompress it, run cmd as an administrator, enter the directory where nexus.exe is located after decompression, and type nexus.exe /install to install the Nexus service.

         

  ② Open the browser, open the address http://localhost:8081/  , and see the following interface, it means that the service is started successfully. If it does not start successfully, you need to check whether the service starts normally.

       

  If you need to change the configuration of Nexus, you can change it in this file:

          

  ③ After the startup is successful, you need to log in to the account. The default account/password is: admin / admin123. After the login is successful, we need to create our maven library and place our library files.

  ④ Login successfully, create repository

  

  1) You need to select the type of creation during the creation process. Let's take a look at the type difference:

Types of illustrate
proxy
Proxy for remote repositories. For example, a proxy of a central repository is configured in nexus. When a user requests an artifact from the proxy, the proxy will be searched locally. If it is not found, it will be downloaded from the remote repository and returned to the user. to act as a relay
hosted In the host repository, users can deploy some of their own components to hosted, or manually upload components to hosted.
group Warehouse group, there is no such concept in maven, is unique to nexus. The purpose is to aggregate the above multiple warehouses and expose a unified address to the user, so that the user does not need to configure multiple addresses in the pom, as long as the address of the group is configured uniformly.

  According to the current requirements, we just put some of our own components into the private library, so the selected type is the hosted type.

  2) One thing that needs to be done during the creation process is: you must change disable redeploy ——> allow redeploy  

  

  3) Artifact type  : release \ snapshot

     release : When building, the build tool will first check whether the dependency library already exists in this warehouse, and if not, it will go to the remote warehouse to pull it

    snapshot: It will first go to the remote warehouse to check whether there is the latest one, and if so, it will be downloaded and used, even if it is already in the local warehouse.

  At this point, nexus has basically completed the construction of maven private server, and the rest is to place our library library files in the private server we have built, and add dependencies.

3. Use private repositories

  We created the reader repository above, and the repository address is http://localhost:8081/repository/reader. If nexus is installed on the LAN server, it is basically similar, except that the localhost in the warehouse address is changed to the LAN server ip.

  ① Upload the library file

  For example, a library public component in our Android project needs to be uploaded to the private library for other projects to rely on. The configuration we need to do:

  1. Add the following configuration to the gradle.properties of the library project:

# maven local config
maven_local_url=http://192.168.142.26:8081/repository/reader/
maven_local_username=admin
maven_local_password=admin123
#common utils
maven_pom_version=1.0.0
maven_pom_groupid=com.dboy
maven_pom_artifactId=reader
maven_pom_packaging = aar
maven_pom_description=dboy reader
# maven_pom_archives_file=libs/CommonUtils.jar 上传某一个jar

  illustrate:

  • maven_local_url The address of the corresponding repository in the maven repository
  • maven_local_username The username for uploading the class library to the warehouse
  • maven_local_password Password for uploading the class library to the warehouse
  • maven_pom_version version number of the class library to upload
  • maven_pom_groupid class library grouping mark, generally use company name or package name
  • maven_pom_artifactId class library name
  • The format of the maven_pom_packaging class library can support jar, aar, so, etc.
  • maven_pom_description class library description
  • maven_pom_archives_file The location of the class library file in the project (relative to build.gradle)

  2. Add the task of uploading the class library to the build.gradle of the module where the corresponding class library is located

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: maven_local_url) {
                authentication(userName: maven_local_username, password: maven_local_password)
            }
            pom.project {
                version maven_pom_version
                artifactId maven_pom_artifactId
                groupId maven_pom_groupid
                packaging maven_pom_packaging
                description maven_pom_description
            }
        }
    }
}

/* artifacts {
    archives file(maven_pom_archives_file)
} */

  illustrate:

  • The path to the jar or aar to be uploaded can be specified in artifacts, which are specified by the property maven_pom_archives_file of gradle.properties for unified configuration
  • If you only need to upload the aar generated when the project is compiled, the artifacts can be omitted, because the artifacts include the aar or apk generated by the compilation by default

  3. Run the task of uploading the class library to upload the corresponding class library to the warehouse reader ——>  gradle uploadArchives ,双击运行

  

  After the upload is successful, you should be able to see the uploaded class library.

  

  4. Use the uploaded class library

  In the build.gradle file of the project that needs to depend on the uploaded class library, add:

allprojects {
    repositories {
        jcenter ()
        maven { url 'http://192.168.142.26:8081/repository/reader/' }
    }
}

  It can be used like other third-party libraries in the module's build.gradle:

compile 'com.dboy:reader:1.0.0'

  So we can use, very happy cutting. However, this class library is developed by ourselves, and it must be updated continuously, so the code in the private server must be updated, so let's talk about the update.

Fourth, update the private warehouse

  If the class library needs to be updated, the basic operation is the same as above.

  For example, the class library reader has a new version of 2.0.0, modify the gradle.properties configuration as follows (only need to modify the version number, others do not need to be modified)

maven_pom_version=2.0.0

  Then execute the gradle uploadArchivestask

  After the class library is updated, the latest class library is automatically obtained without changing the gradle configuration file. The way to add dependencies needs to be added like this: (get the latest code from maven every time)

dependencies {
    compile 'com.dboy:reader:1.+'
}

  or

dependencies {
    compile 'com.dboy:reader:+'
}

5. Reference

  Android builds maven private server management class library

6. Summary

  Multiple projects use the same library, package them into aar, and copy them all without worrying about the trouble. (This method can be seen in this article) During the interview, I was asked why I didn't build a private server. Oh, I was despised. Can not be afraid of trouble diligently.

  

Guess you like

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