2014-02-20 118 views

回答

12

下面的代碼添加到構建腳本:

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 
} 

測試與搖籃1.10

+5

毆打它:p我還製作了'sourcesJar'(你稱'packageSources'依賴於'classes';它避免了在源代碼無法編譯時生成源代碼jar:p – fge

+0

謝謝@fge,我測試了你的建議並更新了答案WER –

2

保羅Fulgoni的回答用於爲直到我碰到了我的搖籃版本3.1工作。要獲得packageJavadoc任務與搖籃3.1工作,我發現我不得不做出點小文章,如下所示:

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

2017年,搖籃4.0版:

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