Configure Gradle to publish sources and javadoc

后端 未结 4 538
南方客
南方客 2021-02-05 04:55

How do I configure Gradle to publish sources and javadoc jars to a repository?

相关标签:
4条回答
  • 2021-02-05 05:08

    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'
    }
    

    Works with gradle publish and gradle publishToMavenLocal

    0 讨论(0)
  • 2021-02-05 05:11

    Add the following code to the build script:

    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
    }
    

    Tested with Gradle 1.10

    0 讨论(0)
  • 2021-02-05 05:14

    Solution as of Gradle 6.0

    Here’s the somewhat minimal configuration you can use if you’re on Gradle 6.0 or later; note the newly introduced withSourcesJar() and withJavadocJar() methods:

    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
           }
        }
    }
    

    Of course, you can also use the ivy-publish plugin instead of maven-publish.

    See also the Gradle docs:

    • on the two new methods
    • on the maven-publish plugin
    • on the ivy-publish plugin
    0 讨论(0)
  • 2021-02-05 05:28

    Paolo Fulgoni's answer used to work for until I bumped up my Gradle version to 3.1. To get the packageJavadoc Task to work with Gradle 3.1 I found I had to make a slight tweak to it as follows:

    task packageJavadoc(type: Jar) {
        from javadoc
        classifier = 'javadoc'
    }
    
    0 讨论(0)
提交回复
热议问题