gradle多模块打jar,上传本地仓库,并给本地其他项目使用

栏目: Java · 发布时间: 6年前

内容简介:本篇主要讲述:gradle多模块打jar包,上传本地仓库,并交由本地其他项目使用注:gradle版本不同,引入依赖方式可能不同,如果你发现本地导包是OK的,但是打包就报错,可以看看是不是gradle版本所引起的问题【

1、前言

本篇主要讲述:gradle多模块打jar包,上传本地仓库,并交由本地其他项目使用

2、环境准备

  • 操作系统: mac os
  • gradle版本:4.1.0
  • 开发软件:idea

注:gradle版本不同,引入依赖方式可能不同,如果你发现本地导包是OK的,但是打包就报错,可以看看是不是gradle版本所引起的问题

3、多模块gradle文档

gradle多环境讲解,官方文档 】,官方文档描述了多模块配置中的几个必闭包的常规使用

allprojects{}, subprojects{}。前者包括root模块之内的所有模块,后者只应用子模块,详细的使用规则进入官方文档详细了解

4、项目实战

项目1结构

|──root-project-one             项目1名称
    ├── common-project          基础子项目 
    │   └── build.gradle        基础子项目配置文件
    ├── example-project         依赖common项目的示例项目
    │   └── build.gradle        依赖common项目的示例项目的配置文件
    ├── build.gradle            项目1的配置文件

项目2结构

|──root-project-two           项目2名称  
    ├── build.gradle          项目2的配置文件(需要引用项目1中的example-project)

各个项目的配置文件(此处只列举主要配置)

root-project-one/build.gradle

//所有子项目共享配置
subprojects {
    apply plugin: 'maven'
    // 获取本地仓库路径
    def localRepositoryPath = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath
    //打包至本地仓库配置
    uploadArchives {
        repositories {
        //mavenDeployer 需要依赖 apply plugin: 'maven'
            mavenDeployer   { 
                repository(url: localRepositoryPath)
                pom.project {
                    name = project.name        // 当前项目名称
                    version = project.version   //当前项目版本  0.0.1
                    groupId = project.group     //  当前项目组  com.xxx
                }
            }
        }
    }
}

common-project/build.gradle

//jar SKIPPED 问题,添加如下配置
jar.enabled=true    //(不设置可能会无法打jar)

example-project/build.gradle

dependencies {
    //依赖 common-project
    compile project(':common-project')
}
//jar SKIPPED 问题,添加如下配置
jar.enabled=true

上述配置完毕后刷新gradle配置,进入每个子模块中打开命令行执行如下命令进行打包

gradle -q uploadArchives

或者打开右侧的gradle面板,到模块下的task>upload>uploadArchives,点击执行即可生成相关jar

本地项目引用jar

root-project-two /build.gradle

repositories {
    //优先查找本地maven库,性能最好
    mavenLocal() //注:引用本地jar包时,必须加上本地maven库
    maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
}
dependencies {
    //依赖本地jar
    compile('com.xxx:example-project:0.0.1')
}

5、总结

  • gradle版本可以通过修改项目下gradle>wrapper>gradle-wrapper.properties文件的distributionUrl对应地址的版本来修改,修改版本后需要刷新gradle
  • 当多模块相互依赖,打包给本地其他项目使用的时候,相互依赖的模块建议都打jar包上传本地仓库

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Writing Windows VxDs and Device Drivers, Second Edition

Writing Windows VxDs and Device Drivers, Second Edition

Karen Hazzah / CMP / 1996-01-12 / USD 54.95

Software developer and author Karen Hazzah expands her original treatise on device drivers in the second edition of "Writing Windows VxDs and Device Drivers." The book and companion disk include the a......一起来看看 《Writing Windows VxDs and Device Drivers, Second Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具