使用gradle发布工件(带有源和javadoc)

时间:2017-01-24 10:25:42

标签: java gradle

我尝试创建gradle文件以发布我的工件(.jarsources.jarjavadoc.jar)。

到目前为止,我已经能够编写这个gradle文件了:

plugins {
    id 'java'
    id 'eclipse'
    id 'maven-publish'
    id 'net.nemerosa.versioning' version '2.5.1'
}

targetCompatibility = 1.8

eclipse {
    project {
        name = 'OAuthz Library'
        natures 'org.eclipse.buildship.core.gradleprojectnature'
    }
    classpath {
        downloadSources = true
        downloadJavadoc = true

        defaultOutputDir = file('build-eclipse')
    }
    jdt {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'javax.servlet:javax.servlet-api:3.1.0'
    compile 'org.codehaus.jettison:jettison:1.3.7'
    compile 'org.apache.directory.api:api-all:1.0.0-M30'
    compile 'com.whalin:Memcached-Java-Client:3.0.2'
    compile group: 'org.mongodb', name: 'mongo-java-driver', version: '2.14.3'
    compile 'commons-configuration:commons-configuration:1.10'
}

group = 'com.living'
version = versioning.info.display

manifest {
    attributes 'Implementation-Title': 'OAuthz Library'
}

publishing {
    publications {
        mavenJava(MavenPublication) {

        }
    }
    repositories {
        maven {
            credentials {
                username 'user'
                password 'passwd'
            }
            url "$url"
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '3.1'
}

我已经能够在存储库上发布我的包了:

  1. 已发布的软件包告诉我它没有依赖项。
  2. 我想发布源代码和java docs工件。
  3. 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

1)您包含一个发布块,但不包含任何工件。这样只会创建一个pom。您需要在发布定义中包含from components.java

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

2)您需要先创建工件:

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

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

然后将工件包含在您的出版物中:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
...

答案 1 :(得分:1)

我使用nebula-publishing-plugin

plugins {
    id 'nebula.javadoc-jar' version '4.4.4'
    id 'nebula.source-jar' version '4.4.4'
}

如果您不想使用这些插件,可以使用the docs

中的代码
tasks.create('sourceJar', Jar) {
    dependsOn tasks.classes
    from sourceSets.main.allSource
    classifier 'sources'
    extension 'jar'
    group 'build'
}
publishing {
    publications {
        nebula(MavenPublication) { // if maven-publish is applied
            artifact tasks.sourceJar
        }
        nebulaIvy(IvyPublication) { // if ivy-publish is applied
            artifact tasks.sourceJar
        }
   }
}