Maven从入门到提高(二)

来源:互联网 发布:写代码的软件 编辑:程序博客网 时间:2024/06/09 08:46

发布Android studio项目到本地Maven仓库

说明

Android studio 可以在build.gradle文件中添加如下的格式来引用第三方库,如:

compile 'com.dragonpass.sundy.modulefounder:limousine:1.0.3'

该格式由三个部分组成,你可以在pom 文件中找到。

<?xml version="1.0" encoding="UTF-8"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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.dragonpass.sundy.modulefounder</groupId>  <artifactId>limousine</artifactId>  <version>1.0.3</version>  <packaging>aar</packaging>  <dependencies>    <dependency>      <groupId>com.android.support</groupId>      <artifactId>appcompat-v7</artifactId>      <version>24.2.1</version>      <scope>compile</scope>    </dependency>  </dependencies></project>

groupId:唯一标识符
artifactId:类似于项目名称
version:版本号

pom 文件可以在你Maven 仓库对一个项目里的文件目录下找到,如:
这里写图片描述

现在我们自己来创建一个第三方Library 放到我们的本机的Maven 仓库

第一步

新建一个Android 项目。在gradle.properties 文件添加以下信息

#包信息GROUP_ID=com.dragonpass.sundy.modulefounder# Licence信息PROJ_LICENCE_NAME=The Apache Software License, Version 2.0PROJ_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txtPROJ_LICENCE_DEST=repo

GROUP_ID:就是刚才的groupId
Licence 信息是固定的

第二步

新建一个Library, 我命名为limousine(这个是我要从一个项目分出来的一个模块)
在Library 的根目录下新建gradle.properties 文件,添加

#包信息ARTIFACTID=limousineLIBRARY_VERSION=1.0.0#Mac下地址:LOCAL_REPO_URL=file:///Users/sundy/Documents/MavenRepo

ARTIFACTID:开始说的artifactId
LIBRARY_VERSION: 版本号
LOCAL_REPO_URL: 本地仓库的地址: file://+本地路径

第三步

在Library 也就是刚才创建的limousine下的build.gradle文件中添加

apply plugin: 'maven'uploadArchives{    repositories.mavenDeployer{        repository(url:LOCAL_REPO_URL)        pom.groupId = GROUP_ID        pom.artifactId = ARTIFACTID        pom.version = LIBRARY_VERSION    }}

第四步

最后在项目路径下执行gradlew 命名:

./gradlew -p <Library name> clean build uploadArchives --info

我自己的是:

./gradlew -p limousine clean build uploadArchives --info

如果自己懒得打字,可以用Android Studio 自带的Gradle 插件工具,只要点击一下就好了,找到该位置,如

这里写图片描述

执行结束没有报错一般会在你的Maven 仓库路径下有刚刚发布好的Library了。

LOCAL_REPO_URL=file:///Users/sundy/Documents/MavenRepo

第五步

引用,先在项目最外层的build.gradle 文件中添加Maven 库路径

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.2.0'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()        maven {            url 'file:///Users/sundy/Documents/MavenRepo'        }    }}task clean(type: Delete) {    delete rootProject.buildDir}

就是:

  maven {            url 'file:///Users/sundy/Documents/MavenRepo'        }

然后在需要引用这个库的项目的build.gradle中添加:

compile 'com.dragonpass.sundy.modulefounder:limousine:1.0.3'

然后就可以了。

项目GitHub 地址:https://github.com/xuxian361/MavenDemo-LocalRepo

1 0
原创粉丝点击