Newer
Older
labs / tiddlers / content / labs / lab07 / _Labs_07_Create the Static Resources Folder.md

NetBeans is not very good at creating missing source/resource directories so we will add a couple of Gradle tasks to the build.gradle file that will do this for us. Hopefully, you now are starting to see the power of a decent build tool like Gradle --- if an IDE or editor can't do something for us, we can probably do it through the build tool.

  1. Add the following code to the bottom of your build.gradle file:
    task createMissingSourceFolders {
        group = "Directories"
        description = "Create all of the missing source folders for this project."
        doFirst {
            sourceSets.each { def sourceRoot ->
                sourceRoot.allSource.srcDirTrees.each { def sourceDir ->
                    if (!sourceDir.dir.exists()) {
                        println "Creating ${sourceDir}"
                        mkdir sourceDir.dir
                    }
                }
            }
        }
    }
       
    task deleteEmptySourceFolders {
        group = "Directories"
        description = "Delete all empty source folders."
        doFirst {
            sourceSets.each { def sourceRoot ->
                sourceRoot.allSource.srcDirTrees.each { def sourceDir ->
                    if (sourceDir.dir.exists() && sourceDir.dir.isDirectory() && sourceDir.dir.list().length == 0) {
                        println "Removing empty ${sourceDir}"
                        sourceDir.dir.delete()
                    }
                }
            }
        }
    }
    Save the file once you have done this.
    1. To run the task, select the root of the project and look for the <

      createMissingSourceFolders">> task in the navigator pane. If you don't see the navigator pane then show it using < Navigator">>.

      Double click the task to run it.

    2. You should see a new folder in your project --- <>. In the files pane you will see this folder at <> in the root of your project.

      If you are in the labs --- NetBeans has a hard time detecting when new source folders have been created due to the network file servers. You may need to restart NetBeans to get it to notice the new folder.

      This folder is where you will add any JavaScript, HTML, CSS, images, and any other static resources that your web client will need.

The deleteEmptySourceFolders task be be used to get rid of any source folders that have been created but are not actually being used. This is handy to get rid of any uneeded source folders that are cluttering up your projects pane. Don't run it just yet since the static resources folder that was just created is still empty and will be deleted if you run the task now.