Publishing a proprietary archive using maven-publish


(Stewart Bryson) #1

I am trying to incorporate an “external to Gradle” build process into the Gradle ecosystem. The external process goes through and generates a package with a non-standard extension name (let’s just say it’s “.ext”), but in reality, the file is a zip file.

I will eventually (hopefully) rip open this external process and claim it for Gradle… but for now, I’d like to still publish this artifact using maven-publish the way I publish all our other artifacts.

I have no way to control the name of the file generated by this external process… unless I rename it.

So my question… can I register an external file as an artifact, even though it doesn’t have standard publication naming? And if so… during the publication… will maven-publish “rename” the file using the artifact id, group, version, etc.?

Thank you.


(Chris Doré) #2

Try something like:

File aFileObject = new File( 'path/to/file/generated/by/external/tool' )
publishing {
    publications {
        thePublication( MavenPublication ) {
            groupId 'some.group' // defaults to project.group
            artifactId 'xyz'  // defaults to project.name
            version '1.2.3' // defaults to project.version

            artifact( aFileObject ) {
                classifier = 'abc' // if you actually need the classifier (ie, when you have multiple artifacts)
                extension = 'ext' // gradle will try to infer this from aFileObject, but you can override it
                // can wire up task dependencies
                builtBy someTask
            }
        }
    }
}

Some helpful links:
https://docs.gradle.org/current/javadoc/org/gradle/api/publish/maven/MavenPublication.html
https://docs.gradle.org/current/javadoc/org/gradle/api/publish/maven/MavenArtifact.html


(Stewart Bryson) #3

A+ Chris. Thank you very much.


(Mark Vieira) #4

It’s highly suggested that you do not use new File() inside a build script with relative paths. There’s no guarantee that the working directory is what you think it will be. You should use project.file() instead. This ensures that relative paths are always resolved relative to the current project directory.

artifact(file('path/to/my/archive.zip')) { ... }

(Chris Doré) #5

I completely agree with Mark’s recommendation.

I’ll also add that in many cases the artifact of a publication should be the output of a task.
If the task is not an AbstractArchiveTask:

artifact( tasks.someTask.outputs.singleFile ) {
    ...
    builtBy tasks.someTask
}

or if the task is an AbstractArchiveTask:

artifact( tasks.someTask )

@mark_vieira Is there any interest in having a shortcut in Gradle that handles non-AbstractArchiveTask’s in the manner I show above? If so, perhaps it’s something I could look at providing a pull request for.


Maven Publish depending on output of task
(Justin Uang) #6

+1 to artifact(tasks.someTask)

It was what I tried first, since copy tasks automatically handle that as well.