I have multi-module project with `buildSrc/my-java...
# community-support
s
I have multi-module project with
buildSrc/my-java-conventions.groovy
plugin and this is applied to all modules to have some common stuff like dependencies, publishing etc..
Out of those modules, one is web-component which generates war and it also applies java-conventions plugin.. but i dont want jar to be published for my war component as it publishes war..
Copy code
my-java-conventions.groovy

plugins {
    id 'java-library'
    id 'maven-publish'
}

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

and my web's build.gradle..

plugins {
    id 'my.java-conventions'
    id 'war'
}

publishing {
    publications {
        myWar(MavenPublication) {
            from components.web  
        }
    }
}
but this approach publishing both jar and war for my web component..
I tried setting
jar {
enabled=false }
for web module, but its failing at plugin saying
jar is not generated for this module
as plugin also trying to publish this module's artifact.. can we do conditional publishing at plugin side? if jar task is disabled, can we skip publishing it?
t
You could disable the various publishMavenJavaPublicationToXxx tasks.
I would however consider splitting the conventions with a java-conventions that only applies java-base, and a java-library-conventions that applies java-conventions and java-library and configures the mavenJava publication (you could also use the java-base-conventions name so you don't rename java-conventions)
👆 1
s
I dont want to split the plugin.. bcoz i endup defining dependencies again for that plugin.. so is there better way?
t
As I said, you could disable the tasks you don't want, e.g.
Copy code
tasks.withType(PublishToMavenRepository).configureEach {
  onlyIf { publication == publishing.publications.myWar }
}
(to only enable tasks for your myWar publication, and disable everything else ; or you could instead
!= publishing.publications.mavenJava
)
s
sorry for my ignorance.. i shoud add this in war's buld.gradle or at plugin groovy file?
t
In the war's build yes
(also note that I haven't written Groovy for years, so you might have to adapt the syntax a bit)
s
ok no problem.. i will check..
l
@Sreenivas, if that doesn't work, you should really consider Thomas's first idea to split the convention plugin in two: 1. my-java-conventions.groovy 2. my-war-conventions.groovy
s
The problem with splitting the plugin is ..dependencies and repositories i need to define again which i want to avoid.. anyways, finally this worked out , which i placed in war's
build.gradle.
.
Copy code
tasks.matching { it.name.startsWith('publishMavenJavaPublicationTo') }.configureEach {
    enabled = false
}
v
You should really consider the original advice, it is much cleaner. 🙂 Your argument is not really valid actually. Split the plugin into 3. One that does the common stuff like dependencies and repositories (which I would move to settings script anyway). One that applies that one and configures jar publishing. One that applies the first one and configures war publishing.
👆 3
s
Okay.. i was able to define pluginManagement and dependencyResolutionManagement repositories in settings.gradle but for publishing its explicitly asking to define inside publishing block.. so i had to define publishing repositories inside that plugin.groovy and war's build.gradle..
v
Ah, yeah, publishing repos indeed. But as I said, no need to duplicate, just have another plugin with the common stuff that you apply from both.