我正在尝试创建gradle文件以发布我的 Artifact (.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'
}

不过,我已经能够将软件包发布到存储库中:
  • 已发布的程序包告诉我它没有依赖性。
  • 我想发布源代码和Java文档 Artifact 。

  • 有任何想法吗?

    最佳答案

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

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

    2)您需要首先创建 Artifact :
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }
    
    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }
    

    然后将这些 Artifact 包括在您的出版物中:
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                artifact sourcesJar
                artifact javadocJar
            }
        }
    ...
    

    08-25 14:24
    查看更多