gradle如何支持发布maven包

1、配置build.gradle

apply plugin: 'java'
apply plugin: 'maven'

// Use as maven groupId
group = 'com.github.liheyuan'
// Use as maven artifactId
project.archivesBaseName = 'coder4-gradle-maven-demo'
// Use as maven version
project.version = 0.1

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
}

// source pom
task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

// javadoc pom
task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

// source & javadoc pom
artifacts {
    archives sourcesJar
    archives javadocJar
}

dependencies {
    compile 'com.google.guava:guava:21.0'

    testCompile 'junit:junit:4.12'
}

简单解释几句吧:

  • group、project.archivesBaseName、project.version分别代表了maven将来pom的groupId、archiveId、mavenVersion
  • sources、javadoc和artifacts那几行主要是生成-sources和-docs的maven包

2、预览即将发布的pom文件

gradle install
cat ./build/poms/pom-default.xml

备注:这个实际会安装到local的maven仓库,如果想让别的项目能使用本地仓库,需要

repositories {
    mavenLocal()
}

3、预览即将发布的jar文件

ls ./build/libs/

coder4-gradle-maven-demo-0.1.jar
coder4-gradle-maven-demo-0.1-javadoc.jar
coder4-gradle-maven-demo-0.1-sources.jar

4、连接jetpack.io

https://jitpack.io/

5、github上发布版本

https://github.com/blog/1547-release-your-software

6、如何使用

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    compile 'com.github.liheyuan:gradle-maven-demo:0.1'
}

我写了2个demo,分别是同一个repo下一个pom和多个pom的,欢迎参考:

https://github.com/liheyuan/gradle-maven-multi-demo

https://github.com/liheyuan/gradle-maven-demo

Leave a Reply

Your email address will not be published. Required fields are marked *