This message was deleted.
# community-support
s
This message was deleted.
f
Copy code
// Register a custom task of type Copy
tasks.register<Copy>("copyDirtySrcToBuild") {
    // Configure the task to copy files from src to build/copied directory
    if(layout.projectDirectory.dir("src").asFile.exists()) {
        from(layout.projectDirectory.dir("src"))
        exclude("**/*.java")
        // "build/classes/java/main"
        into(layout.buildDirectory.dir("classes/java/main"))
    }
    mustRunAfter("compileJava")
}
// Make the build task depend on the custom copy task
tasks.named("build") {
    dependsOn("copyDirtySrcToBuild")
}
tasks.named("jar") {
    dependsOn("copyDirtySrcToBuild")
}
tasks.named("compileJava") {
    dependsOn("copyDirtySrcToBuild")
}
It seems that I need to add a bunch of those dependsOn? Otherwise the task will fail.
v
The answer to your initial question is simple
Don't even try to
Never
Ever
By doing so, you are polluting the output directory of another task (compileJava) and thus have overlapping outputs and that is very bad.
It prevents up-to-dateness, causes validation errors if Gradle detects it that you then try to solve with bad-practice
dependsOn
, disturbs build cache or causes corrupt cache entries, disables other optimizations, ...
f
Indeed, I had a lot of trouble setting it up. But the thing is as I am working in a very large old Eclipse based project, some sub project, I haven't checked how many and which, having some important files just saved besides their src file. And when I launch my application, which is using OSGi framework, for local dev there is a config in OSGi to point to the folder that contains classes, then the running class will look for the file besides itself... So in order to be able to use gradle also build and run my large application, I sort of need a solution that can keep those dirty files into the build/classes folder... 🤔
So perhaps you have a good idea to how to achieve my goal but also do not bring trouble as you mentioned above? 🤔
Some background - initially I thought it was an outlier in a place so I tried to fix it up, but then launching the application I got other errors. So I gave up the idea for now to fully clean up these "dirty files". After I setup gradle as above, I then can start the project (and also those dirty files get injected to jars and that is also what I need for now in order for the old code to run nicely.)
v
As I said, there is no clean way and you really shouldn't do it. There are ways to make OSGi work properly, I played with OSGi in the past, but I do not have something I could share. It might also highly depend on the project at hand. Maybe you could instead add one new
Sync
task that collects everything into a new directory and use that to run OSGi against instead of the
compileJava
output folder. Something like
Copy code
val foo by tasks.registering(Sync::class) {
    from(sourceSets.main.get().output)
    into(layout.buildDirectory.dir("foo"))
}
given the sources and resources are properly configured for the
main
source set.
f
Aha, that do sounds like some good idea - then I will need to also figure out a way to get those files into jars, if I change the setup?
v
If the point "given the sources and resources are properly configured for the
main
source set." is true, they will also be added to the jars already
f
Hmmm... interesting, so you mean to set the "resources" onto this synced folder foo? 🤔 For now I completely just set the "resources" folder to literally "xxxxxxx" as it cannot play well with the Eclipse folder structure here...
So basically under a sub-project it looks like this in my case:
Copy code
/src # which has all the source code + some dirty files 
/META-INFO
/whatever.xml
/x.icon
And then in the Eclipse generated jar it looks like such:
Copy code
/com.a.b...  // source structure containing .class and some dirty abc.xml
/META-INFO
/whatever.xml
/x.icon
And then in the Eclipse generated
bin
(similar to build/classes/main/java/) it has
Copy code
/com.a.b...  // source structure containing .class and some dirty abc.xml
So basically I want gradle to be able to build similar stuff, not exactly in to
bin
but all the dirty stuff beside code inside src should also be in build/classes/main/java
Perhaps I can just try set
resource
to point it onto `src`folder and somehow exclude all *.java files? 🤔
Otherwise this
sourceSets.main.get().output
will not include the dirty file besides Java file?
v
Probably just something like this:
Copy code
sourceSets {
    main {
        resources {
            srcDir("src")
        }
    }
}
f
Wouldn't that also get all the java file injected and dumped in jars in the end? 🤔
Aha... Seems it can remove the java files by default 😄
v
Should be, yes
Probably due to exactly that use-case
f
Woo, the Sync task seems working pretty well. Thank you so much. Maybe I should try write this in stackover flow, tried search around so many and could not find correct tips.
👌 1
By the way, with the Sync task, it should be safe to wire up task dependencies as such?
Copy code
tasks.named("compileJava") {
    finalizedBy("mergeClasssAndSrcDirtyFiles")
}
I guess now it is not a problem anymore as we do not write into the compile target folder, right?
v
It might be a bit strange, the dependency, but it should not be a problem. You just say to always execute that task after
compileJava
was run successfully (successfully because you also have a dependency on the
compileJava
output)
✅ 1
f
Great, thank you so much, test it a bit seems working pretty well 🙂
👌 1