Geoffroy Warin
Geoffroy Warin

Reputation: 647

Add a single file to gradle resources

I'm using the config var plugin for heroku (see https://devcenter.heroku.com/articles/config-vars).

It allows me to use a .env file which I do not push to source control to define the heroku environment.

So when I'm on heroku, I can access sensible information via System.properties.

In dev, I would like to read from this file so it would be best if it were on my classpath.

The .env file is at the root of my project so I cannot use something like this :

sourceSets {
    main {
        resources {
            srcDirs = ['src/main/resources', '/']
        }
    }
}

What is the simplest way to include a single file into gradle resources ?

Upvotes: 8

Views: 6683

Answers (3)

whaleberg
whaleberg

Reputation: 2189

The earlier answers seemed more complicated than I was hoping for, so I asked on the gradle forums and got a reply from a Sterling Green who's one of the gradle core devs.

He suggested configuring processResources directly

processResources {
   from(".env") 
}

He also mentioned that it might be a bad idea to include the project root as an input directly.

Upvotes: 9

Geoffroy Warin
Geoffroy Warin

Reputation: 647

What I ended up doing was to add project root directory as a resource folder including only the file I was interested in :

sourceSets.main.resources { srcDir file('.') include '.env' }

Seems to do the trick. I wonder if it's the best solution thought

Upvotes: 2

Marwin
Marwin

Reputation: 2763

After merging the previous answer here with another tips from other sites, I came up with the following solution:

sourceSets {
  specRes {
    resources {
      srcDir 'extra-dir'
      include 'extrafiles/or-just-one-file.*'
    }
  }
  main.resources {
    srcDir 'src/standard/resources'
    srcDir specRes.resources
  }
}
processResources {
  rename 'some.file', 'META-INF/possible-way-to-rename.txt'
}

I still wonder whether there is some better way.

Upvotes: 5

Related Questions