当前位置:   article > 正文

Gradle入门_graddle allprojects mavenlocal mavencentral

graddle allprojects mavenlocal mavencentral

本人也是初次接触到 Gradle,之前一直用 Maven, 关于 Gradle 的运行原理及概念就不多说了,主要介绍一下,用 Gradle 如何实现 Maven 的各种功能吧。

使用的 Gradle 版本是 7.2。

主要的配置就是 gradle.build 这个文件。

配置文档:https://docs.gradle.org/current/dsl/org.gradle.api.Project.html

插件文档:https://docs.gradle.org/current/userguide/plugin_reference.html#plugin_reference

常用命令:

  1. ./gradlew clean

  2. ./gradlew build

  3. ./gradlew publish

一、使用阿里云的源

单体项目:

repositories {
    maven {
        url 'https://maven.aliyun.com/repository/public/'
    }
    mavenLocal()
    mavenCentral()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如果是包含子项目,则这样配置:

allprojects {
    repositories {
        maven {
            url 'https://maven.aliyun.com/repository/public/'
        }
        mavenLocal()
        mavenCentral()
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

其中配置了三个源,分别是阿里云源、本地仓库以及Maven中心仓库,下载 jar 包时会按照从上到下的顺序去下载 jar 包。

二、添加依赖 & 排除依赖

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation 'cn.hutool:hutool-all:5.7.16'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

命令查看依赖项:gradle -q dependencies --configuration=compileClasspath

三、配置 IDEA 编辑器自动下载 javadoc 包和 source 包

plugins {
	// 引入 idea 插件
    id 'idea'
}

idea {
    module {
        downloadJavadoc=true
        downloadSources=true
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

四、生成本项目的 javadoc 包和 source 包

plugins {
	// 引入 java 插件
    id 'java'
}

java {
    withJavadocJar()
    withSourcesJar()
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

五、发布 jar 包到私服

plugins {
	// maven 插件已经废弃,这里使用新版的插件进行发布
    id 'maven-publish'
}

publishing {
    // 配置要发布的仓库
    repositories {
    	// 本地仓库
        mavenLocal()
        // 远程私服仓库
        maven {
            // 判断是否是测试版本
            if (!version.endsWith('SNAPSHOT')) {
                // 此处直接使用 gradle.properties 中的 key 即可,此文件与 gradle.build 文件同级
                // 注意 gradle.properties 文件不要上传到 git 上
                url = releasesRepoUrl
                // 私服认证需要
                credentials {
                    username = releasesGradleUsername
                    password = releasesGradlePassword
                }
            } else {
                url = snapshotsRepoUrl
                credentials {
                    username = snapshotsGradleUsername
                    password = snapshotsGradlePassword
                }
            }
        }
    }
    
    // 配置发布的项目信息
    publications {
    	// 文档 https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html
        bootJava(MavenPublication) {
            // 可以在这里手动指定 groupId artifactId version,也会有默认值
            groupId = 'com.test'
            artifactId = 'gradle-demo'
            version = '1.0.0'

            // 上传三个 jar 包(针对于 SpringBoot 项目)
            artifact bootJar
            artifact sourcesJar
            artifact javadocJar

            // 补充一下 pom 文件的信息
            pom {
                name = 'GradleDemoName'
                description = 'A concise description of my library'
                url = 'http://www.example.com/library'
                licenses {
                    license {
                        name = 'The Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'wql'
                        name = 'WuQinglong'
                        email = 'snail.wu@foxmail.com'
                    }
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

如果是普通 Java 项目的话,需要将下面这里进行替换

            artifact bootJar
            artifact sourcesJar
            artifact javadocJar
  • 1
  • 2
  • 3

替换为:

			from components.java
  • 1

简单一行即可,就可以把 jar 包、javadoc 包和 source 包都发布到私服里,如果是 web 项目,需要引入插件

plugins {
	id 'war'
    id 'maven-publish'
}
  • 1
  • 2
  • 3
  • 4

则这里需要改为

			from components.web
  • 1

总结一下就是 java 项目使用 'java' 插件,发布时使用 from components.java,而 web 项目使用 'war' 插件,发布时使用 from components.web,SpringBoot项目使用 org.springframework.boot 插件,发布时使用

            artifact bootJar
            artifact sourcesJar
            artifact javadocJar
  • 1
  • 2
  • 3

这个配置。

还有一点,

六、设置编码

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}
tasks.withType(Javadoc) {
    options.encoding = 'UTF-8'
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

七、设置Java版本

// 设置 jdk 版本
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
  • 1
  • 2
  • 3

八、跳过测试

方法一:修改 gradle.build 文件

test {
    enabled = false
    useJUnitPlatform()
}
  • 1
  • 2
  • 3
  • 4

方法二:

./gradlew build -x test

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/266212
推荐阅读
相关标签
  

闽ICP备14008679号