How to iterate over sub-projects from external gradle script?

I’m trying to create a gradle based multi-module project. There is also an project that contains different gradle scripts that enables pluggable build configurations. One of those scripts is for publishing artifacts to maven repository. This is the content of that script:

apply plugin: 'maven-publish'
  configure(subprojects.findAll()) {
    if (it.name.endsWith('web')) {
        publishing {
            publications {
                mavenWeb(MavenPublication) {
                    from components.web
                }
            }
        }
    } else {
        publishing {
            publications {
                mavenJava(MavenPublication) {
                    from components.java
                }
            }
        }
    }
}
  build.dependsOn publishToMavenLocal

This script is included in the build gradle file of other project.

apply from: '../../food-orders-online-main/artifact-publish.gradle'

When I run build task it always shows that publishToMavenLocal task is up to date and I cannot find the artifacts in the local repository. I suppose that subprojects.findAll() is not returning a list of projects at all. If this is the case, is it possible to do it and how?

Instead of ‘configure(subprojects.findAll()) { … }’ you can just use ‘subprojects { … }’, but perhaps what you really want is ‘rootProject.subprojects { … }’.

I need to check the name of the project, because, if it ends with “web”, I want to deploy war artifact and jar artifact in other cases.

How does that relate to my comment? I wasn’t saying you shouldn’t do that.

If I write it this way: rootProject.subprojects { … }, then I’m getting the following error message: Maven publication ‘mavenJava’ cannot include multiple components Apparently this is not allowed.

Despite the if-statement? To help more I’d have to know more details, such as where this script is applied. I can’t tell if you want ‘subprojects’ (the current project’s subprojects) or ‘rootProject.subprojects’ (all subprojects). I was merely indicating that given your problem description, maybe you want the latter but don’t get it because the script is not applied to the root project (and you aren’t using ‘rootProject.subprojects’).

This is the script which imports the external script from above:

subprojects {
 apply from: '../../food-orders-online-main/base.gradle'
 apply from: '../../food-orders-online-main/test-coverage.gradle'
 apply from: '../../food-orders-online-main/code-analysis.gradle'
 apply from: '../../food-orders-online-main/artifact-publish.gradle'
 apply from: '../../food-orders-online-main/spring-boot.gradle'
}
  project(':food-orders-online-admin-data') {
 version = '1.0'
   dependencies {
  compile project(':food-orders-online-admin-domain')
 }
}
  project(':food-orders-online-admin-business') {
 version = '1.0'
    dependencies {
  compile project(':food-orders-online-admin-domain')
  compile project(':food-orders-online-admin-data')
 }
}
  project(':food-orders-online-admin-web') {
 version = '1.0'
   dependencies {
  compile project(':food-orders-online-admin-domain')
  compile project(':food-orders-online-admin-data')
  compile project(':food-orders-online-admin-business')
 }
}

The thing I want is to iterate over all subprojects of this food-orders-online-admin root project and deploy them to local maven repo as war if their name ends with “web” or jar in other case.