This message was deleted.
# community-support
s
This message was deleted.
m
Copy code
tasks {
    named<Copy>("backendProcessResources") {
        val webpackTask = getByName<KotlinWebpack>(if (isDevelopment) "frontendBrowserDevelopmentWebpack" else "frontendBrowserProductionWebpack")
        val jsMinifyTask = getByName("jsMinify")
        val cssMinifyTask = getByName("cssMinify")

        exclude(
            // These files are preprocessed by the minify plugin, exclude them from the copy task
            "static/js/**",
            "static/css/**",
        )

        into("WEB-INF/") {
            // TODO: Does not work, instead places into /templates as if this doesn't exist
            into("templates/") {
                from("templates/")
            }

            into("static/") {
                into("js/") {
                    from(webpackTask.destinationDirectory)
                    from(jsMinifyTask) {
                        rename("(.*)\\.min\\.js", "$1.js")
                    }
                }
                into("css/") {
                    from(compileSass)
                    from(cssMinifyTask) {
                        rename("(.*)\\.min\\.css", "$1.css")
                    }
                }

                // TODO: Does not work, instead places into /static/img as if this doesn't exist
                into("img/") {
                    from("static/img/**")
                }
            }
        }
    }
}
n
into
returns
this
, so there's no such thing as nested
into
statements, they're all calling the same
rootSpec.into
(this being the AbstractCopyTask instance)
m
That's odd, because it does work for the js/css files. While it does return
this
, doesn't it create a new copy spec for the nested block's context receiver? From the javadoc:
Copy code
Creates and configures a child CopySpec with the given destination path. The destination is evaluated as per org.gradle.api.Project.file(Object).
But the main problem is that it doesn't seem to find the img and template folders, any clue what could be the issue there?
n
ah, interesting, I might've misinterpreted that api indeed 🙂
have you tried flipping the
from
and
into
? I've always found that to be more intuitive to reason about. I'm not sure how the copyspec closure interprets those relative paths in such a setup.
m
Copy code
into("WEB-INF/") {
            from("templates/") {
                into("atemplates/")  // different name to see if maybe it adds to root?
            }
The files are still put into /templates, no change in behavior here. while flipping the order can be more intuitive, having it as tree structure make the code a lot more clean imo
n
I mean, at the root. Treat individual
from
sources as separate copyspecs, instead of organising by target 🤔
but that said. If you change
from("templates")
to
from(layout.projectDirectory.dir("templates")
, does that fix it?
m
Copy code
from("templates") {
            into("WEB-INF/templates")
        }
also did not work
n
gotcha 👍
m
but that said. If you change
from("templates")
to
from(layout.projectDirectory.dir("templates")
, does that fix it?
Wouldn't that try to target the
templates
folder as if it were in the project root rather than from the resources root?
n
isn't that what you want? the first
into
isn't referencing the webpack task anywhere, right?
m
No, it should be relevant to the current task shouldn't it? this is the processResources task, not the webpack task
Here's a visual aid; Yellow is handled by the webpack task and the sass task, red is handled by the minifier, and orange is what I'm trying to copy into specific folders
thank you 1
n
what is the end artifact you are producing?
m
what do you mean? The end artifact is just a jar that'll run the website
n
are you getting anything inside the WEB-INF folder? because I can't get anything to show up there, using the processResources task 🤔
m
The js and css files from the other tasks show up just fine
n
Also, I'm pretty sure your
from
closures aren't working as you expect they should. You're just seeing those files coming from inside the resources folder in the output, because that's the default configuration of the task: https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/java/org/gradle/api/plugins/JavaBasePlugin.java#L191-L192
m
So how would I approach something like this
n
Copy code
2022-09-14T16:35:57.108+0200 [INFO] [org.gradle.api.internal.file.collections.DirectoryFileTree] file or directory '/test/templates', not found
2022-09-14T16:35:57.109+0200 [INFO] [org.gradle.api.internal.file.collections.DirectoryFileTree] file or directory '/test/static/img/**', not found
you have to make sure you point to the correct location in your from statements. if I change one to
from("src/main/resources/templates")
, it just works
but you do get duplicate files in your jar file, as these resources are copied twice
m
hmm, so there's no way to do it without hardcoding the resources path?
n
I would probably store these files somewhere else. You don't want them to be in the default location for resources, so set up a separate folder instead. Whether you want to leverage the processResources task to copy them, or configure a separate task is up to you.
m
So you're saying I should put my resources in a not-resources folder? Why? I'm just trying to relocate most of them to a separate directory
n
the problem is that they will be in 2 locations, right? both the default location, because of the default configuration of that task, as well as the "WEB-INF" location, once you correctly specify the
from
.
m
yeah, but even just getting it to place all files in WEB-INF isn't working as
into("WEB-INF")
makes it put the WEB-INF folder relative to the project root instead of the build/resources/... folder
n
you can't use the nested into approach if you want to affect the root copy spec (which is the one that's setup with build/resources as output root) hold on, that might not be correct, I'm not sure how the rootspec and mainspec in the AbstractCopyTask interact 🤔
m
It looks like the two are completely independent, which confuses me even more
n
I think for your own sanity, it's safer to consider those resources as an independent resource. In the jar, there's no resources folder anyway, so it doesn't matter for your end result.
m
But then how do I get intellij to still treat it as a resource root?
n
you can mark it as such using the idea plugin
Copy code
idea {
    module {
        resourceDirs.add(file("your_folder"))
    }
}