The environment that

  • Server: Tencent Cloud lightweight application server, Centos 7.6;
  • The JDK: 1.8 x
  • Nexus: 3.37.3-02 – Unix
  • Android:Android Studio (4.1.1), Gradle (4.1.1), Dokka — Android-Gradle-Plugin (0.9.16), gradle-wrapper (gradle-6.5-bin)

1.Nexus builds a private warehouse

Since developing with AndroidStudio, it is very convenient to use third-party libraries, which are stored in the code repository, such as jcenter, in a single word. If we want to write their own libraries use, so we need to put our code library to the warehouse, such as the former can choose Bintray (now does not provide services), or more convenient jitpack, also can upload the arrival mountable access maven repository (can be local warehouse, warehouse) can also be the clouds above. So the main purpose of this article is to set up your own Maven repository.

There are basically two steps to build a Nexus service:

  1. Install and configure the JDK
  2. Install and enable the Nexus service

1.1 to install the JDK

Method 1: Download the JDK package, upload it to the cloud server, and decompress it

Mode 2: Centos 7.6 can be installed using YUM

  1. The cloud currently supports installed JDK versions

Execute the command yum search Java | grep JDK will list the current list of JDK support;

  1. After the version is selected, installation (run the following command) automatically installs JDK dependencies

Execute the command# yum install -y # yum install -yInstallation; If “Complete” is displayed, the installation is Complete.

  1. Ensure that the JDK is installed successfully

Run the Java -version command to view the current JDK version. If the installation is successful, the version number is displayed.

1.2 configure the JDK

  1. Find the JDK installation directory

Find the default JDK installation directory by searching Java files: run find / -name ‘Java’

Make sure the JDK installation directory is /usr/lib/jvm/java-xxxxx as shown above

  1. Configuring environment Variables

Modify /etc/profile to add the following content to the file:

Note: to performvi /etc/profileCommand to open the specified file using the vi editor. Use the vi editor as follows: Press I to enter the editing mode, and then press ESC to enter the command mode. Run the :wq command to save the configuration and exit :q! Forced out of

'#set Java environment' JAVA_HOME= JRE_HOME= JDK /jre CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin export  JAVA_HOME JRE_HOME CLASS_PATH PATHCopy the code

Save the configuration and exit. Then run the source /etc/profile command or restart the configuration for it to take effect.

Until this JDK installation and configuration is complete.

1.3 installation Nexus

  1. Downloading compressed packages

Download it from the Nexus website, and then you have to fill out a form to get the download address. It can also be downloaded by executing wget to the download address you get.

Attention attention: for a long time, I found that the compressed package could not be downloaded. It should be the need to climb the wall to download. Finally, I also asked my friend to help me download after opening the VPN

  1. Decompression package

Unzip the package in a directory, which I did in /usr/local/mysof/nexus

  • usemkdirCommand to create directory:mkdir -p /usr/local/mysoft/nexus(-p is a recursive directory creation, usually used to create multilevel directories)
  • Decompress the directory: Run a commandTar -zxvf {directory containing the decompressed package} -c {directory containing the decompressed package: /usr/local/mysoft/nexus}

When unzipped, you get two directories, the nexus-{version} installation directory and the Sonatype -work directory.

The Nexus installation is complete

  1. (Optional) Configuring the Nexus

Configuration file path… {Nexus installation directory}/etc/nexus-default.properties(Note: The file name and configuration may vary with nexus versions). Generally, use the default nexus configuration. The default nexus service uses port 8081.

Default administrator account password stored in… /sonatype-work{note that this directory is the same as the nexus installation directory}/nexus3/admin.password file

  1. The Nexus use

Switch to directory… {Nexus installation directory}/bin/ Run the following command:

  • Start the service:./nexus start, you can also use the command./nexus run
  • Stop service:./nexus stop

After starting the service, we can access Nexus service management through port 8081. As for how to use Nexus, please refer to the information.

1.4 Possible problems in Nexus service establishment

1.4.1 The above operations occur frequentlyPermission denied

When performing the above operations, if the login user does not have permission to the operation directory, it is most likely to have directory or file access permission problems, the solution is to switch to the root user operation or add sudo before the command, you can also change the ownership of the Nexus directory or change the operation permission of the user, etc.

Linux stuff, I really don't understand... I just got to know them.Copy the code

1.4.2 Judgment of startup success of Nexus service

After executing the command to start the Nexus, there is no output and it is not known whether the Nexus service is running properly. Here is a quote from Rango:

1. Query process have started ps - aux | grep Java data, proof process started. 2. With web services, view ports haven't netstate - a listening apln | grep 8081 so, general web page can be opened, but could not access. Curl 127.0.0.1:8081 The above three steps of verification are OK. Next comes the question of firewallsCopy the code

Curl confirmed that the service was working.

1.4.3 Failure to Access The Nexus Service

After ensuring that the service is started, it is found that the Nexus cannot be accessed externally (that is, the cloud server IP address 8081 cannot be accessed). Therefore, the initial guess is that the firewall is faulty. Since Nexus uses port 8081 by default, if we need external access to Nexus services, we will develop port 8081 in the firewall:

Here’s What Rango said:

Ok. The reasons are as follows: 1. Firewall problem. The server has its own Linux firewall on. Even though you configured it on the cloud, it was blocked by Linux. There are two methods: 1.1 Disabling the Firewall. That's what we do for Intranet applications. systemctl stop firewalld ; systemctl disable firewalld ; The corresponding command is systemctl start firewalld. systemctl enable firewalld ; Enable: firewall-cmd --permanent --add-port=8081/ TCP Reload configuration: firewall-cmd --reload View: firewall-cmd --list-allCopy the code

After enabling the firewall of the server, we also need to develop port 8081 access on the firewall of the cloud server:

Complete the above three steps, and you’ll basically have no problems.

1.4.4 Further description of Nexus Service usage

  • Nexus startup can be configured on the server.

  • Nexus logging can be configured

1.4.4.1 Role Management

Open the Nexus management website and log in with the administrator account. It is found that there are only two roles in user management: NX-admin and NX-Anonymous. The first role is the administrator role, and the second role is the tourist role

So I also need a role for developers to upload libraries to the repository, so I need to have read and write access to the repository. Since I only use Maven, I only have read and write access to Maven:

1.4.4.2 User Management

Create a user account so that you can log in to the Nexus to upload code or use automatic upload to upload code to the repository; By default there are already two users admin and Anonymous, assign yourself a development account and assign the nX-Deployment permissions you created earlier.

1.4.4.3 Warehouse Management

Since my purpose is to upload my Android libraries to this repository for management, and since these libraries are in the app domain, I plan to create a corresponding repository for app use (I can also not create one, several repositories are provided by default) :

Finally, I created three repositories, one group, two hosted (one for release and one for Snapshot), and the group is for external access.

When creating release and Snapshot, since we are creating a Hosted type repository to hold the uploaded code, we need to check “Allow Redeploy” (i.e. redeploy if I have previously released version 1.0 of a library, I can still re-publish to cover this version later) :

When creating a group, associate the release and snapshot created earlier with:

Once the Nexus is deployed and configured, try uploading the code to your repository.

【 Digression 】 Back up Range’s sayings:

1. Check the service running state [root @ VM - 0-9 - centos bin] # ps - aux | grep Java root 23948 5223272 1499152 1.5 79.6? Sl 11:38 2:24 /usr/li.... org.sonatype.nexus.karaf.NexusMain 2. Get it [root @ VM - 0-9 - centos bin] # CD/usr/local/mysoft/nexus/nexus - 3.37.3-02 / bin/root @ VM - 0-9 - centos bin] #. / nexus stop If the fault persists, run the kill command to forcibly kill it (run by user root). 3. Modifying a directory owner group: Run [root@VM-0-9-centos local]# CD /usr/local/ [root@VM-0-9-centos local]# chown superluo mysoft -r as user root [root@VM-0-9-centos local]# ll-ald Mysoft drWxr-xr-x 3 SuperLuo root 4096 3月 2 10:24 mysoft [root@VM-0-9-centos local]# CD mysoft/ [root@VM-0-9-centos mysoft]# drwxr-xr-x 4 Superluo root 4096 3月 2 11:34 nexus 4. Switch to the superluo startup services [superluo @ VM - 0-9 - centos bin] $CD/usr/local/mysoft/nexus/nexus - 3.37.3-02 / bin [superluo @ VM - 0-9 - centos bin]$ ll total 32 drwxr-xr-x 2 superluo root 4096 Mar 2 10:26 contrib -rwxr-xr-x 1 superluo root 18620 Dec 30 03:20 nexus -rw-r--r-- 1 superluo root 15 Dec 30 03:20 nexus.rc -rw-r--r-- 1 superluo root 1635 Dec 30 03:20 nexus.vmoptions [superluo@VM-0-9-centos bin]$ ./nexus start 5. Web page visit http://{your cloud server IP address}:8081/ default password I changed, changed to: {private information} default password address: First login page note [root @ VM - 0-9 - centos mysoft] # cat/usr/local/mysoft/nexus/sonatype - work/nexus3 / admin. The password 1d0da450-3725-4d6a-bcc9-d8f8696fc545Copy the code

2. Android uploads the AAR to the private library

2.1 Preparations

Before uploading the code, we need the following information: warehouse address, user name and password to access the warehouse;

View warehouse address:

The User name and password to access the warehouse are the User account and password added to the Nexus earlier.

2.2 Configuration Project

I’ll leave that aside for how to create an Android project.

2.2.1 Add the following information to the local.properties dialog box: Configure the Maven repository address, user name, and password

You can manage this information in any way you like, but I’m just thinking I have multiple libraries that need this information and instead of just putting it in one file and uploading it to the web, you can use this information directly, or you can use Graldle file management

2.2.2 Add the following information to the build.gradle file
! [image.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2b449224979d4c48ada1656f9fe8a472~tplv-k3u1fbpfcp-watermark .image?)Copy the code

Classpath ‘org. Jetbrains. Dokka: dokka – android – gradle – plugin: 0.9.18’ this configuration is critical, the back is explained;

2.2.3 Configure the following information in build.gradle of Module

Add this information at the bottom of the file

It takes more than one screen. Keep going

Attached with relevant codes:

Plugins {/* for kotlin to generate javadoc*/ id' org.jetBrains. Dokka-android '/* for uploading maven*/ id' maven'} // This part added at the end of the file is ok // kotlin android javadoc task generateSourcesJar(type: Jar) { group = 'jar' from android.sourceSets.main.java.srcDirs classifier = 'sources' } task javadoc(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task dokkaJavadoc(type: org.jetbrains.dokka.gradle.DokkaTask) { outputFormat = "javadoc" outputDirectory = javadoc.destinationDir } task generateJavadoc(type: Jar, dependsOn: dokkaJavadoc) { group = 'jar' classifier = 'javadoc' from javadoc.destinationDir } artifacts { archives generateJavadoc Archives generateSourcesJar} def PROJECT_NAME = '{generateSourcesJar}' def PROJECT_VERSION = '{generateSourcesJar}' def PROJECT_DESC = archives generateSourcesJar} def PROJECT_NAME = '{generateSourcesJar}' def PROJECT_VERSION = '{generateSourcesJar}' def PROJECT_DESC = ' Def ARTIFACT_ID = 'Properties = new Properties()' def ARTIFACT_ID = 'Properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def NEXUS_URL = properties.getProperty('NEXUS_URL') def NEXUS_USER = properties.getProperty('NEXUS_USER') def NEXUS_PWD = properties.getProperty('NEXUS_PWD') uploadArchives { repositories { mavenDeployer { repository(url: NEXUS_URL) { authentication(userName: NEXUS_USER, password: NEXUS_PWD)} pom.project {name PROJECT_NAME version PROJECT_VERSION artifactId ARTIFACT_ID groupId '{complete content according to your situation}' packaging 'aar' description PROJECT_DESC } } } }Copy the code

The configuration is complete

2.3 Upload the code to the warehouse

If the uploadArchives task runs successfully, then the code has been uploaded successfully. You can check whether the code has been uploaded through the Nexus background management system:

At this point we have successfully uploaded the code base to our Maven repository.

2.4 Use code from the repository

Configure the following contents in the build.gradle project:

In fact, that is, before the warehouse address configuration can be accessed.

Add dependency statements to buid.gralde in Module. Dependency statements are already known when you upload the group, artifactid, and version number.

The upload and use of this Android AAR is complete.

2.5 Important Note
2.5.1 JavaDoc Cannot be generated

The biggest problem I encountered was that I initially configured the Java project in the same way as the previous Java project, and when I finally uploaded it, I was told that JavaDoc generation failed and the upload failed.

In the end, all the errors indicating that JavaDoc cannot be generated show kotlin files instead of Java files. Therefore, it is assumed that kotlin wrote the file that caused the problem.

A search found Dokka to solve this problem, with the following references:

Summary of problems encountered by Kotlin Open Source library uploading Bintray(Maven) codebase

Dokka- Generates JavaDoc: Portal

Dokka – making: portal

2.5.2 Dokka Version Problems

As mentioned in the reference above, “Dokka- Generating JavaDoc: Portals”, there are two types of Kotlin project and Kotlin’s Android project, and there are differences between the two plug-ins, so be careful here.

Another problem here is that this article uses 0.9.15 Dokka. At the beginning, when I configured this version, I found that it could not be compiled successfully, and there was a problem that could not be solved. It should be that the plug-in of this version is incompatible with the Kotlin version used in my project. This is no longer a problem with 0.9.16. Later, I searched the data and found that version 0.9.18 exists. This version has not been used yet. I will try it later.

3. Relevant reference materials are attached

Build MAVEN+NEXUS private repository on LINUX

Linux builds The Nexus3.X private server

Maven private repository (Maven private server)

How to upload local code to nexus3 maven private server of alibaba cloud server Centos7

Android Studio uploads an AAR to a private Maven repository

Dokka- Generates JavaDoc: Portal

Summary of problems encountered by Kotlin Open Source library uploading Bintray(Maven) codebase

Dokka – making: portal