简体   繁体   English

使用Gradle发布工件(带有源代码和JavaDoc)

[英]Publishing artifacts(with sources and javadoc) with gradle

I'm trying to create a gradle file in order to publish my artifacts ( .jar , sources.jar and javadoc.jar ). 我正在尝试创建gradle文件以发布我的工件( .jarsources.jarjavadoc.jar )。

Up to now, I've been able to write this gradle file: 到目前为止,我已经能够编写此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'
}

I've been able to publish my package on repository, nevertheless: 不过,我已经能够将软件包发布到存储库中:

  1. The published package is telling me that it has no dependencies. 已发布的程序包告诉我它没有依赖性。
  2. I'd like to publish sources and java docs artifacts. 我想发布源代码和Java文档工件。

Any ideas? 有任何想法吗?

1) You include a publishing block, but include no artifacts. 1)您包括一个发布块,但不包含任何构件。 That way only a pom is created. 这样,只会创建一个pom。 You need to include a from components.java in your publishing definition: 您需要在发布定义中包括一个from components.java

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

2) You need to first create the artifacts: 2)您需要首先创建工件:

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

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

Then include the artifacts into your publication: 然后将这些工件包括在您的出版物中:

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

I use the nebula-publishing-plugin 我使用星云发布插件

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

If you don't want to use these plugins, you could use the code from 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
        }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM