Copy Task: Expand tar.gz and remove first folder from path

(Thomas Küstermann) #1

In my build script, I download the Keycloak tar.gz package and extract it to the build folder via Copy task. I configured the Copy task to remove the first folder in the package (keycloak-$version) from the target path.

task unzip(type: Copy) {
	configurations.server.asFileTree.each {
		from(tarTree(it)) {
			eachFile { copyDetails -> 
			    copyDetails.path = copyDetails.path.substring(copyDetails.path.indexOf("/") + 1)
			}
		}
	}

	into 'build/keycloak'
}

This works quite good at first sight. Unfortunately, the original folder structure is retained in the output folder, without any files though.

\---keycloak
	+---bin
	+---docs
	+---domain
	+---keycloak-4.8.3.Final
	|   +---.installation
	|   +---bin
	|   +---docs
	|   +---domain
	|   +---modules
	|   +---standalone
	|   \---themes
	+---modules
	+---standalone
	+---themes

I could specify includeEmptyDirs false to get rid of the unwanted folder but that would remove other folders as well that I’d ideally like to keep.

What’s the preferred way of modifying the target path while keeping up-to-date checks intact?