javagradlegroovyresourcessubproject

Gradle: copy subprojects files into rootproject when build it


Given this gradle project:

org.mycompany.rootProject
|
|--org.mycompany.subProject0001
|  |
|  |-src/
|  | |
|  | |-dist/
|  | | |
|  | | |-README.MD
|  | | |-runSubProject0001.cmd
|  | |-main/
|  | |-test/
|  | |
|  |-build.gradle
|
|--org.mycompany.subProject0002
|  | |
|  | |-dist/
|  | | |
|  | | |-README.MD
|  | | |-runSubProject0002.cmd
|  | |-main/
|  | |-test/
|  | |
|  |-build.gradle
|  .
|  .
|  . (goal is keeping going until subproject 1000)
|-build.gradle
|-settings.gradle

What can I do to get README and 'runSubProject' from each subproject and copy into my rootproject (target is '{rootProject}/build/distribution/{subprojectName}/') when I build it?

All my projects use "java-application" plugin.

I was trying to make gradle iterate over each subproject and copy those files into my rootproject, but I have failed.


Solution

  • So this is what I did in my build.gradle from org.mycompany.rootProject and it worked:

    task getArtifactsFromSubprojects {
        subprojects.each {
            if(it.getName().contains("cjv")){
                dependsOn it.getPath() + ':convertReadmeToPdf'
                def sourcePath = it.getProjectDir().toString() + '/src/dist'
                def destinationPath = getRootDir().toString() + '/src/dist/' + it.getName() + '/'
                it.copy {
                    from sourcePath
                    into destinationPath
                }
            }
        }
    }
    

    PS: I did some work on my subprojects and implemented a new task called convertReadmeToPdf which produces a README.PDF in the same folder as README.MD.

    After running this task, the result is like:

    org.mycompany.rootProject
    |
    |--src/
    |  |
    |  |-dist/
    |  | |
    |  | |-org.mycompany.subProject0001/
    |  | | |
    |  | | |-README.MD
    |  | | |-runSubProject0001.cmd
    |  | |
    |  | |-org.mycompany.subProject0002/
    |  | | |
    |  | | |-README.MD
    |  | | |-runSubProject0002.cmd
    |  | |
    |  | |-org.mycompany.subProject0003/
    |  | | |
    |  | | |-README.MD
    |  | | |-runSubProject0003.cmd
    |  | .
    |  | .
    |  | .
    |  | 
    |  |-main/
    |  | 
    |  |-test/
    |
    |
    |-build.gradle
    |-settings.gradle