We have two different artifacts that is going to be published to two different maven repositories.
We use the plugin maven-publish where you configure a set of publications and reposistories. The plugin then generates tasks for all combinations of publications and repositories (see tasks list at the bottom). Currently the tasks publish
and publishRelease
is doing what we want, but there are tasks we don't want.
Some solutions might be:
repo.publish(artifact)
or something like that)?I have looked at the source code of PublishToMavenRepository
. It seems like the action I want to achive is located in protected void doPublish
.
*Wanted tasks:
**Unwanted tasks:
Gradle file:
apply plugin: 'maven-publish' publishing { publications { ProjectXMergedWar(MavenPublication) { artifact mergeWar artifactId = 'projectx' } ProjectXJarRelease(MavenPublication) { artifact releaseJar artifactId = 'projectx' } } repositories { maven { name 'MyMavenRepo1' url 'http://artifactory/url/our-snapshot-local' credentials { (...) } } maven { name 'MyMavenRepo2' url 'http://artifactory/url/our-release-local' credentials { (...) } } } } task publish(dependsOn: [ 'generatePomFileForProjectXMergedWarPublication', 'publishProjectXMergedWarPublicationToMyMavenRepo1Repository' ], overwrite: true) { // We override the normal publish which would have tried to publish all combinations of defined // publications and repositories: // - publishProjectXMergedWarPublicationToMyMavenRepo1Repository (we use this in normal snapshot publish) // - publishProjectXMergedWarPublicationToMyMavenRepo2Repository (not to be used) // - publishProjectXJarReleasePublicationToMyMavenRepo1Repository (not to be used) // - publishProjectXJarReleasePublicationToMyMavenRepo2Repository (we use this one in publishRelease) } task publishRelease(dependsOn: [ 'generatePomFileForProjectXJarReleasePublication', 'publishProjectXJarReleasePublicationToMyMavenRepo2Repository' ])
Output from tasks:
$ ./gradlew tasks (...) Publishing tasks ---------------- generatePomFileForProjectXJarReleasePublication - Generates the Maven POM file for publication 'ProjectXJarRelease'. generatePomFileForProjectXMergedWarPublication - Generates the Maven POM file for publication 'ProjectXMergedWar'. publishProjectXJarReleasePublicationToMavenLocal - Publishes Maven publication 'ProjectXJarRelease' to the local Maven repository. publishProjectXJarReleasePublicationToMyMavenRepo1Repository - Publishes Maven publication 'ProjectXJarRelease' to Maven repository 'MyMavenRepo1'. publishProjectXJarReleasePublicationToMyMavenRepo2Repository - Publishes Maven publication 'ProjectXJarRelease' to Maven repository 'MyMavenRepo2'. publishProjectXMergedWarPublicationToMavenLocal - Publishes Maven publication 'ProjectXMergedWar' to the local Maven repository. publishProjectXMergedWarPublicationToMyMavenRepo1Repository - Publishes Maven publication 'ProjectXMergedWar' to Maven repository 'MyMavenRepo1'. publishProjectXMergedWarPublicationToMyMavenRepo2Repository - Publishes Maven publication 'ProjectXMergedWar' to Maven repository 'MyMavenRepo2'. publishToMavenLocal - Publishes all Maven publications produced by this project to the local Maven cache. (...) Other tasks ----------- (...) publish publishRelease (...)
You could disable and hide the "invalid" tasks like so:
apply plugin: 'maven-publish' publishing { repositories { maven { name 'Dev' url 'http://dev/' credentials { username 'username' password 'password' } } maven { name 'Prod' url 'http://prod/' credentials { username 'username' password 'password' } } } publications { // This will only be enabled on Dev MyDevJar(MavenPublication) { artifactId "test" version "1.0" groupId "org.example" artifact file('abc') ext.repo = 'Dev' } // This will only be enabled on prod MyJar(MavenPublication) { artifactId "test" version "1.0" groupId "org.example" artifact file('abc') ext.repo = 'Prod' } } } afterEvaluate { tasks.withType(PublishToMavenRepository) { task -> if (task.publication.hasProperty('repo') && task.publication.repo != task.repository.name) { task.enabled = false task.group = null } } }
I've just started playing with gradle and it's other plugins and @knut-saua-mathiesen solution was really interresting, however it wasn't working for me.
Inside the 'AfterEvaluate' the task.publication wasn't set to it's correcte value but initialized to 'null'. So i tried a few other things and came up with this solution :
afterEvaluate { tasks.withType(PublishToMavenRepository).all { publishTask -> publishTask.onlyIf { task -> if (task.publication.hasProperty('repo') && task.publication.repo != task.repository.name) { task.enabled = false task.group = null return false } return true } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With