Converting Maven resources to gradle resources

We are moving from Maven to Gradle and I am stuck on the issue below. In our current pom.xml we have this:

<resources>
            <resource>
                <targetPath>deploy/config/directory1</targetPath>
                <directory>config/directory1</directory>
            </resource>
            <resource>
                <targetPath>deploy/config/directory2</targetPath>
                <directory>config/directory2</directory>
            </resource>
            <resource>
                <targetPath>deploy/scripts</targetPath>
                <directory>scripts</directory>
            </resource>

We have a unit test that does
getClass().getResource("/deploy/config/directory1/somefile.txt")
I do not want to change the pom.xml as that structure is needed for deployment reasons until we fully migrate to Gradle. I am trying to do the equivalent in gradle with not much success:

    sourceSets {
    main {
        java {
            srcDirs 'src'
        }
        resources {
            srcDirs = [
                    "config/directory1",
                    "config/directory2",
                    "scripts",
            ]
//            output.resourcesDir = "$buildDir/deploy"
        }
    }

A have a few issues with this:

  1. The files in the config/directory1 and config/directory2 are copied to the root location of the resources folder. I would like to keep the structure the same so there would be a single config directory under the resources dir and then config dir would have directory1 and directory2.
  2. I can set the output.resourcesDir as shown however that means I have to change my java code to
    getClass().getResource("somefile.txt") which is what I have to avoid.

Thankyou

You can configure processResources directly:

processResources {
   from("scripts") {
      into("deploy/scripts")
   }
   from("config/directory1") {
      into("deploy/config/directory1")
   }
   // etc
}
1 Like

Sterling thank you very much. That has worked perfectly. May I ask if you have any links to more information on this? I searched and read the closest I got was
from 'config/xml-include’
into 'deploy/config/directory1’
from ‘config/directory2’
into deploy/config/directory2’

but that didn’t work.

Thank you very much for your answer.

The Java plugin chapter in the user guide calls out processResources as a Copy task. The Copy task DSL reference has an example.

1 Like