In the previous article about component-based development, we mentioned that we can extract and package some common function libraries, such as network library, image loading library, tool library and so on, and use them as basic library. How to use these libraries after they are extracted? It also requires each developer to import the source file and use it. Obviously, this method is not convenient and is not beneficial to the maintenance of the base library. So can we use Gradle to download it from the repository like relying on other third-party libraries? The answer is yes. We can set up our own Maven private repository for the corporate team.

Maven’s remote repository is divided into central repository and private repository. Central repositories, notably JCenter and Maven Central, host dependencies uploaded by users from around the world. Open source third party dependencies are uploaded to these two Central repositories, so we can simply add links to these two Central repositories to download any dependencies we need. Within the company, a Maven repository can be set up to upload commonly used development libraries or encapsulated code in daily development, and upload the dependency packages that do not want to open source to the private server repository, which has better confidentiality, security and uniformity.

Through the establishment of the team’s own development warehouse, developers can be unified coding standards, unified development framework, unified use of some functions and so on, convenient team management, but also conducive to the team’s mutual cooperation and so on.

Here are some introductions to Maven private repository created by Sonatypec Nexus for your reference…

Build Maven private repository using Nexus

Download and run Nexus

Go to Sonatypec’s official website to download the software package. Choose the software package according to your operating system. Here, Windows operating system is used as an example.

After downloading, we unzip the package into a folder (for example: D:\nexus-3.24.0-02-win64) containing nexus-3.24.0-02 and Sonatype -work

Then go to the D: nexus-3.24.0-02-win64 nexus-3.24.0-02 bin directory

There is no graphical installation interface for the software, so we need to enter the command line and run nexu.exe

Before installing the Nexus, ensure that JDK1.8 or higher has been installed in the system

You can then start the Nexus with nexu.exe /run:

Using the run command, you can start the Nexus directly without installing it into a Windows service, which is simple but requires human intervention if the server is restarted. To close the Nexus, press Ctrl-C in the current command line window. We put it on the LAN server. When the server restarts for some reasons, manual intervention is required, which is obviously not in line with our requirements.

We can use nexus. Exe /install Nexus command, install Nexus to Windows service, and then set to automatic start, you can restart the server, automatic start private server.

Note: To run this command, you need to run the CMD command line window as an administrator (that is, in Windows accessories, go to the command prompt -> right-click -> Run as administrator).

This allows you to see the Installed Nexus service in the Windows service

Here we can start/close the service, and also set the service to automatic/manual

Once the Nexus service has been successfully installed and started, we can access the Nexus.

Visit the nexus

The default port is 8081. You can access the server IP address and port number (http://<server_host>:8081).

The default user name and password are admin and admin123 respectively. After login, we can change the password in the user center.

Maven repository types

There are only three Maven repository types: Hosted, Proxy, and Group

  • Hosted: Hosted internal project release repository dedicated to storing our own generated JAR files, AAR files. The development libraries we upload ourselves are uploaded to this type of repository.
  • Proxy: Proxy type A repository that searches for data from a remote central repository, such as the configurable Aliyun Maven repository.
  • The Nexus manages multiple warehouses through the concept of a warehouse group, so that when we directly request a warehouse group in a project, we can request multiple warehouses managed by the warehouse group.

Briefly: Group = Hosted + Proxy

As shown in the figure above, Maven-public is the group repository created by default.

Version type

  • Release: A JAR or AAR dedicated to deploying a release, corresponding to the Maven-Release host repository.
  • Snapshot: Jar or AAR dedicated to snapshot version deployment. The end of the JAR or AAR is -snapshot. In poM, version must end with -snapshot (in uppercase), corresponding to maven-snapshot repository.
  • Mixed: Can include release and Snapshot versions

Rights management

Data permissions: There are two types of repository permissions:

  • repository admin
  • repository view

Each type has five permissions: Add, Browse, Delete, Edit, and read. You can configure accounts as required.

Upload the library library to the repository

We are developing locally, packaging the development library, how to upload to the repository, here is a simple example. I’m creating a local Android project for Autobio_Android, and I’m creating an Android Library module (utilcode), which is the Library that we’ll encapsulate and upload to the repository for others to use. Or aar files.

Uploading to the repository is something that gradle builds automatically.

In the gradle.properties file of the project, the configuration needs to use global information

As you can see above, we can configure the Maven-Releases repository (which stores mature and stable libraries) and the Maven-snapshots repository (which stores libraries that are still in development and can change at any time). The NEXUS username and password; Generally, the groupId of a company and a team is the same, so the GROUP_ID is also set here.

GROUP_ID: For example, we rely on third-party library implementation ‘IO. Reactivex. Rxjava2: rxjava: 2.1.12’, this rxjava library GROUP_ID is IO. Reactivex. Rxjava2, we add the dependent libraries is: GROUP_ID:artifactId: version number.

Add the configuration upload code to the build.gradle file of the library module you want to upload

For neatness, create a new gradle file for upload_nexus. Gradle under the utilcode module as follows:

apply plugin: 'maven' afterEvaluate { project -> uploadArchives { repositories { mavenDeployer { snapshotRepository(url: MAVEN_SNAPSHOT_URL) { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } repository(url: MAVEN_URL) { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)} pum. project {version '0.0.2-SNAPSHOT' groupId GROUP_ID artifactId 'util-code' packaging 'aar' description 'dependences lib' } } } } task androidJavadocs(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { classifier = 'javadoc' from androidJavadocs.destinationDir } task androidSourcesJar(type: = 'sources' Jar) {classifier from android. SourceSets. Main. Java sourceFiles} / / solve the problem of JavaDoc comments generated failure in Chinese tasks.withType(Javadoc) { options.addStringOption('Xdoclint:none', '-quiet') options.addStringOption('encoding', 'UTF-8') options.addStringOption('charSet', 'UTF-8') } artifacts { archives androidSourcesJar archives androidJavadocsJar } }Copy the code

After writing upload_nexus. Gradle, we import it into the utilcode module’s build.gradle, as shown below:

Upload library development library module

Now that the configuration is complete, open Android Studio, open the Gradle sidebar on the right, open the Module Library, and see uploadArchives. This is the upload Task you just created. Click to upload

If the uploadArchives Task executes successfully, you can see the uploaded content in the Nexus repository. Click on the left navigation bar browse-Maven-snapshot to see the library we uploaded, as shown below:

Use uploaded development libraries in your project

Once uploaded to the warehouse, how do we use it in the project? It is used in the same way that we add dependencies using third-party libraries.

Build. Gradle adds the repository address to the root path of the project

Add the development library address to build.gradle in the application module app