1 Maven and Maven private server
For Android developers, we use Gradle for project building and dependency management. However, we still need Maven Central, the Central repository of Maven. For example, in our root build.gradle file we can see code like this:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com. Android. Tools. Build: gradle: 7.1.2'
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
Copy the code
MavenCentral () refers to designating Maven Central as the project’s Maven repository, from which we try to search when we rely on certain third-party libraries.
Due to network issues, direct access to Mavan Central was often slow and we were often unable to download third-party libraries. In this case, we can solve this problem through the domestic Maven Central image repository, such as Ali Cloud cloud effect Maven image, it is also very simple to use:
// Central and jCenter aggregate repositories
maven{ url 'https://maven.aliyun.com/repository/public'}
/ / Google warehouse
maven{ url 'https://maven.aliyun.com/repository/google'}
Copy the code
Just add the code above to the Repository under AllProjects.
1.1 Why do we need private servers
Why do we need private servers when we have the central warehouse and the mirror station of the central warehouse? Because some of our artifacts have private business and are not suitable to be deployed in a public repository such as MavenCentral, we can choose to use the free Maven private service provided by Aliyunyun.com.
Another option is to set up a Maven private server for your internal environment, or use the Maven private server as a proxy for other Maven repositories. Steal a picture for everyone to understand:
In this way, Maven private server can cache artifacts from various Maven repositories, which can be used by other member users to improve the speed of pulling.
1.2 Using Docker to build Maven private server
The process is super simple and the steps are as follows:
- Download and install Docker
- pullNexus3 mirror
docker pull sonatype/nexus3
- Run the mirror
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
- Open the web site
http://localhost:8081/
- Find the password to log in to admin
- Innovate your account
The old Nexus password is admin123, the new Nexus password should be checked by docker:
- View a running container:
docker ps
- Enter the container:
Docker exec -it container id bash
- View password
vi opt/sonatype/sonatype-work/nexus3/admin.password
Note: When you look at the running container, you will see two running containers. Note the difference between NAMES. We are entering the nexus container.
2 Upload the AAR using maven-publish
2.1 upload the aar
In fact, the steps are very simple, but the online tutorials are either outdated or incomplete, causing a lot of interference.
We made the following changes in the build.gradle file of the Module that needs to be uploaded:
apply plugin: 'maven-publish'
/ /... Other configurations
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
// flavorBuildType
from components.release
// Group ID, artifact ID, version
groupId = 'xyz.junerver.fileselector'
artifactId = 'file-selector-phone'
version = '1.1'
}
}
repositories {
// If there are multiple repositories to upload, write several maven{}
maven {
// If private server is HTTP protocol need to write this line configuration
allowInsecureProtocol = true
// You can use any name you like
name = "nexus"
// Private server storage address
url = "http://localhost:8081/repository/maven-releases/"
credentials {
username = 'Private server Account'
password = 'Private password'
}
}
}
}
}
Copy the code
Important: From components. Release, variant (small hump), flavorBuildType (small hump), flavorBuildType (small hump);
After adding the above configuration and synchronizing the project, click on the Gradle sidebar and the following project should appear:
The Release marked in red is the name of the artifact we declared above, followed by Nexus is the name of the Maven private server we declared. Click this entry to publish our library to Maven private server!
2.2 Adding a Dependency
Just as we need to declare Maven for upload, we need to add our private repository information under AllProjects, and then add the dependencies.
implementation 'groupId:artifactId:version'
Copy the code
This is written with the dependencies declared in the POM message. If you want to rely solely on an AAR and not on the dependencies declared in the library, you can add @aar to the end of the version