How to create a "resources" directory from gradle?

I want to write some data using gradle into the src/main/resources directory to be included int he deployed jar file. I have it working if the resources directory is already there, but is there a way to get gradle to create the directory if it isn’t already there?

As far as I know, unless you are generating a new gradle project, there’s no command that you can run to create the resources directory.

If you want to use the resources directory there’s no special set up that you have to do. You just have to create the directory in src/main/java and it will work as expected. If you have any questions about how to use the resources directory I can answer them or this might be a good starting point:

Actually, I just figuired it out. I had to add this:

task ensureResources() {
  doLast {
     String resourcesDir - "$projectDir/src/main/resources"
     mkdir "$resourcesDir"

And then add an appropriate dependsOn. Ran “build robot code” and there was the resources directory!

Have you looked at the deploy directory?

Huh, I guess I’ve never actually used the resources directory when deploying robot code. I didn’t know you had to add a dependsOn to get it to work.

EDIT: Oh, I get it now. You added dependsOn :ensureResources to get it to create the resources directory if it doesn’t exist.

I agree with @Joe_Ross, you might be better served to use the deploy directory, depending what you want to do with the files.

As noted in the docs:

Files placed in this directory will be deployed to the RoboRIO into the
‘deploy’ directory in the home folder. Use the ‘Filesystem.getDeployDirectory’ wpilib function
to get a proper path relative to the deploy directory.

It’s much easier to work with files on the filesystem vs. resources in the JAR.

I am using the deploy directory too. But the deploy directory goes to the filesystem, but the resources directory gets included in the jar file.

1 Like

I wouldn’t say it is easier, just different, but as I said to @joe_ross, I am using both for different things.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.