配置Gradle以发布源和javadoc

时间:2014-02-20 09:58:18

标签: java gradle

如何配置Gradle将源和javadoc jar发布到存储库?

4 个答案:

答案 0 :(得分:17)

将以下代码添加到构建脚本中:

task packageJavadoc(type: Jar, dependsOn: 'javadoc') {
    from javadoc.destinationDir
    classifier = 'javadoc'
}
task packageSources(type: Jar, dependsOn: 'classes') {
    from sourceSets.main.allSource
    classifier = 'sources'
}
artifacts {
    archives packageJavadoc
    archives packageSources
}

使用Gradle 1.10进行测试

答案 1 :(得分:17)

2017,Gradle 4.0 Edition:

apply plugin: 'maven'
apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            artifact sourceJar
            artifact packageJavadoc
        }
    }
}

javadoc {
  source = sourceSets.main.allJava
  classpath = configurations.compileClasspath

  options
  {
    setMemberLevel JavadocMemberLevel.PUBLIC
    setAuthor true

    links "https://docs.oracle.com/javase/8/docs/api/"
  }
}

task sourceJar(type: Jar) {
  classifier = 'sources'
  from sourceSets.main.allJava
}

task packageJavadoc(type: Jar) {
    from javadoc
    classifier = 'javadoc'
}

适用于gradle publishgradle publishToMavenLocal

答案 2 :(得分:5)

从Gradle 6.0开始的解决方案

如果您使用的是Gradle 6.0或更高版本,则可以使用一些最低限度的配置;请注意新引入的withSourcesJar()withJavadocJar() 方法:

plugins {
    id 'java'
    id 'maven-publish'
}

group = 'com.example'

java {
    withSourcesJar()
    withJavadocJar()
}

publishing {
    repositories {
        maven {
            url = 'file:///tmp/my-repo'
        }
    }
    publications {
        myJava(MavenPublication) {
           from components.java
       }
    }
}

当然,您也可以使用ivy-publish插件代替maven-publish

另请参阅Gradle文档:

答案 3 :(得分:3)

Paolo Fulgoni的答案过去一直有效,直到我将Gradle版本提升到3.1。要让packageJavadoc任务与Gradle 3.1一起使用,我发现我必须对它进行一些轻微调整,如下所示:

task packageJavadoc(type: Jar) {
    from javadoc
    classifier = 'javadoc'
}