Install and use local Maven repository

1. Download Maven 

Maven download address: https://maven.apache.org/download.cgi

2. Install Maven 

Unzip the zip package to a local directory, such as: E:\maven\apache-maven-3.9.1

Set the E:\maven\apache-maven-3.9.1 and E:\maven\apache-maven-3.9.1\bin paths to environment variables. After the installation is successful, open the cmd command line window and enter mvn --version. If the version output appears, the installation is successful.

3. Publish the jar package to the local warehouse

Reference blog:  Maven publishes jar packages to local libraries

Maven command to install JAR package:

mvn install:install-file -Dfile=jar包的位置 -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar

Screenshot of successful command execution:

After successful installation, the generated pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tencent.shadow.dynamic.loader</groupId>
  <artifactId>loader</artifactId>
  <version>1.0.0</version>
  <description>POM was created from install:install-file</description>
</project>

mvn install:install-file -Dfile=jar包的位置 -DgroupId=groupId -DartifactId=artifactId -Dversion=version -Dpackaging=jar

The location of the jar package : It is the path of the jar to be published, for example: E:\codes\Shadow\Shadow-source\projects\sdk\dynamic\dynamic-loader\build\libs\dynamic-loader.jar groupId: it is
maven 's groupId information, such as the information in the <groupId> tag of the above xml
artifactld : it is the artifactld information of maven, such as the information in the <artifactld> tag of the above xml
version : it is the version information of maven, such as the <version of the above xml >Information in tags

4. Use Jar package from local Maven repository

Configure mavenLocal() in Android Studio's settings.gradle to import the local maven repository. Note that the configuration in settings.gradle can take effect.

Configure dependencies in the build.gradle of the corresponding module

// implementation "groudId : artifactId : version"
implementation "com.tencent.shadow.dynamic.loader:loader:1.0.0"

The path where the file is located: USER_HOME\.m2\repository\com\tencent\shadow\dynamic\loader\loader\1.0.0\loader-1.0.0.jar

If mavenLocal() is not configured in settings.gradle, it will not take effect. As follows, if mavenLocal() is configured in build.gradle in the root directory of the project, it will not take effect. gradle cannot find the dependent jar package in the path to mavenCentral(). As shown below:

If mavenLocal() is configured in app/build.gradle, a compilation error will be reported.

Build was configured to prefer settings repositories over project repositories but repository 'MavenLocal' was added by build file 'app\build.gradle'

As shown below:

The compilation error message says that the build warehouse configuration should be written to setting.gradle, not the project build.gradle, but the current build file build.gradle adds the MavenLocal warehouse. Therefore, we need to configure mavenLocal() in settings.gradle.

After adding mavenLocal() and implementation, sync project and build to complete compilation.

 

Reference documentation:

Maven Publish Plugin

Maven tutorial

 

Guess you like

Origin blog.csdn.net/xiaoyu_wu/article/details/129811278