dwursteisen
dwursteisen

Reputation: 11515

Publishing artifact from Copy task

my gradle build copy files. I want to use the output of copy task as input for maven artifact publishing

example :

task example(type: Copy) {
    from "build.gradle" // use as example
    into "build/distributions"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact example
        }
    }
}

but gradle doesn't like it :

 * What went wrong:
 A problem occurred configuring project ':myproject'.
 > Exception thrown while executing model rule: PublishingPlugin.Rules#publishing(ExtensionContainer)
     > Cannot convert the provided notation to an object of type MavenArtifact: task ':myproject:example'.
       The following types/formats are supported:
       - Instances of MavenArtifact.
       - Instances of AbstractArchiveTask, for example jar.
       - Instances of PublishArtifact
       - Maps containing a 'source' entry, for example [source: '/path/to/file', extension: 'zip'].
       - Anything that can be converted to a file, as per Project.file()

Why ?

As I understand, the outputs from the task example should be setted by the Copy task. I assume it can be converted to some files. So it should be used as input of the publishing task, as files. But the error message tell me I'm wrong.

How can I fix it ?

Thanks

Upvotes: 5

Views: 2712

Answers (1)

dwursteisen
dwursteisen

Reputation: 11515

Gradle doesn't know how to convert a Copy task to an MavenArtifact, AbstractArchiveTask, PublishArtifact, .... Which explain the error message.

BUT it does know how to convert a String as a File, as it's explain in the last line of the error message.

The issue is how to force Gradle to build my task before publishing. MavenArtifact has a builtBy method which is here for that !

task example(type: Copy) {
    from "build.gradle" // use as example
    into "build/distributions"
}

publishing {
    publications {
        mavenJava(MavenPublication) {
           // file to be transformed as an artifact
           artifact("build/distributions/build.gradle") {
               builtBy example // will call example task to build the above file
           }
        }
    }
}

Upvotes: 11

Related Questions