Search code examples
gradlegradle-shadow-plugin

Gradle copy folder on installDist


Using gradle 3.4 but fairly new to it.

If I run gradlew installDist the files in src/main/java get copied to the build folder all that works fine.

But I also have an extra folder in src/main/conf I would like to copy over to build/install/my-artifact/conf

I don't want to put it in src/main/resources because that will get included inside the jar. I would like to keep it external.

My gradle file doesn't have anything special except the dependencies.

So how would I go about copying the folders/files when installDist runs?

EDIT:

Has to work with shadow plugin as well.


Solution

  • To manage distribution contents, you will need to modify the main distribution like the following :

    apply plugin: 'distribution'
    
    distributions {
        main {
            baseName = 'my-artifact'
            contents {
                from { 'src/main/java' }
                from('src/main') {
                    include 'conf/**'
                }
            }
        }
    }
    

    This will :

    • copy the files under src/main/java
    • copy the directory conf and the files under it

    The new structure would be like :

    build/install/my-artifact/
                       │
                       ├── com/
                       │   └── yourlib
                       │       └── ......
                       └── conf/
                           └── .....
    

    You could also include the source directory at the same level :

    build/install/my-artifact/
                       │
                       ├── java/
                       │    └── com/
                       │         └── yourlib
                       │               └── ......     
                       └── conf/
                            └── .....
    

    with the following :

    apply plugin: 'distribution'
    
    distributions {
        main {
            baseName = 'my-artifact'
            contents {
                from('src/main') {
                    include 'java/**'
                    include 'conf/**'
                }
            }
        }
    }
    

    Check CopySpec interface for more info