This Gradle task collects .java source files and other files, concatenates their contents, and copies the result to your system clipboard.
It’s designed to help developers programmatically control what context is given to LLMs (like ChatGPT, Claude, Gemini) by automating the process of assembling relevant code and docs into a single pasteable chunk.
- Collects all
.javafiles undersrc/ - Includes key project files (
README.md,AGENTS.md,build.gradle.kts) - Joins all file contents with readable spacing
- Copies the result directly to your system clipboard (Windows-only)
- Ideal for sending real, runnable project context into LLM prompts
- Copy the task into your
build.gradle.kts:
tasks.register("copySourcesToClipboard") {
group = "custom"
description = "Collects project files and copies them to clipboard"
doLast {
val projectRoot = project.projectDir.toPath()
val sourceFiles =
Files.walk(projectRoot.resolve("src"))
.filter { it.toString().endsWith(".java") }
.toList()
val extraFiles =
listOf("README.md", "AGENTS.md", "build.gradle.kts")
.map { projectRoot.resolve(it) }
.filter { Files.exists(it) }
val allFiles = sourceFiles + extraFiles
val combined =
allFiles.joinToString(System.lineSeparator().repeat(2)) { path ->
Files.readString(path)
}
val tmpFile = Files.createTempFile("collected", ".txt")
Files.writeString(tmpFile, combined, StandardOpenOption.TRUNCATE_EXISTING)
exec {
commandLine("clip.exe")
standardInput = tmpFile.toFile().inputStream()
}
println("Copied ${allFiles.size} files to clipboard.")
}
}- Run the task:
./gradlew copySourcesToClipboard- Paste into your favorite LLM prompt window. It now has full project context for accurate answers, suggestions, or code generation.
- Debugging with AI assistants
- Prompt engineering from live project code
- Bootstrapping feature implementations from current source
- AI-generated documentation or refactoring plans
- This version only works on Windows (uses
clip.exe). - It runs successfully from WSL2 Ubuntu.
- On macOS, replace
clip.exewithpbcopy. - On Linux, use something like
xclip -selection clipboardorxsel.