Flatten found files to copy in resources

I have my resources in all over the source code… however all of them are in various sub directories with dir name res…

I wish test task picks all the files in res directory, where ever they are found in the root and copy them into resource build/resources/test/ folder as flattened file… (i.e. ignoring the source all the sub-directory info

instead of this

test {
	java {
	    srcDir("src/main/java")
	    include("**/tests/**")
	}

	resources {
	    srcDir("src/main/java/com/tejasoft/sboot/api/tests/archunit/res")
	}
    }

I want to write as

test {
	java {
	    srcDir("src/main/java")
	    include("**/tests/**")
	}

	resources {
	    srcDir("src/main/")
            include ("**/res/**")
	}
    }

Can any reply please… ant copy as simple attribute to say flatten=true…

https://ant.apache.org/manual/Tasks/copy.html

ant is so simple and too clear… hope has something equivalent

<copy flatten="true" todir="${test}/WEB-INF/lib" overwrite="${overwrite}">
                <fileset refid="buildJars"/>
                <fileset dir="lib">
                    <include name="commons-collections-*.jar"/>
                    <include name="commons-io-*.jar"/>              
                    <include name="kodo/*.jar"/>
                    <include name="mail*.jar"/>    
                    <include name="activation*.jar"/>               
                    <include name="guava*.jar"/>
                    <include name="**/*.jar"/>                          
                </fileset>          
            </copy>

I really don’t want to list absolute directory names… wish to use ** to find all directories by gradle itself

Hello @Raja_Nagendra_Kumar1

first i am new to grade world so i could miss something

i tried with the sourceSet i it did not work

as follow


java {
    sourceSets {
        main {

            resources {
                srcDirs {
                    fileTree(layout.projectDirectory.dir("src/main/java/resources")).getFiles()
                }

            }
        }
    }
}

but i tried it with copy task as follow and it work fine

tasks.register("copyResources",Copy){

    from fileTree(layout.projectDirectory.dir("src/main/java/resources")).files

    into layout.buildDirectory.dir("resources")

}

you can use that task and make other task depend on it till you get a better solution from more experience people

hope that help and have a nice day :slight_smile:

1 Like

Thank You @justsomeone, I did not want to give an absolute path as I refactor a lot, hence wish to use ** notation so that, any refactoring, I am void of the extra burden of changing the paths in build scripts too.

That way, in ant the solution is refactoring friendly…this way…

<copy flatten="true" todir="${test}/WEB-INF/lib" overwrite="${overwrite}">
                <fileset refid="buildJars"/>
                <fileset dir="lib">                
                    <include name="**/activation*.jar"/>               
                    <include name="**/guava/***.jar"/>
                    <include name="**/own/**/*.jar"/>                          
                </fileset>          
            </copy>

what flatten does it irrespective of jar file in various sub-directories of lib, it would still copy them in ${test}/WEB-INF/lib folder, I am looking for similar solution in gradle…

you welcome got you then hope someone help you soon also try with stackoverflow :slight_smile:

nope… none are here to help… time to have our own tasks for this… gradle experts pl. share your expertise… on this simple and very trivial (as per ant style) need

@Raja_Nagendra_Kumar1 :joy:

they a way to call ant from gradle not sure if that would help you or not

https://docs.gradle.org/current/userguide/ant.html

hope they show up soon and have a nice day :slight_smile:

I didn’t test it, but something like this should flatten the files I think:

tasks.processResources {
    eachFile {
        relativePath = RelativePath(true, relativePath.lastName)
    }
}