• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2025 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.gradle.build
18 
19 import org.gradle.api.DefaultTask
20 import org.gradle.api.file.DirectoryProperty
21 import org.gradle.api.file.RegularFile
22 import org.gradle.api.provider.ListProperty
23 import org.gradle.api.provider.MapProperty
24 import org.gradle.api.tasks.Input
25 import org.gradle.api.tasks.InputFiles
26 import org.gradle.api.tasks.OutputDirectory
27 import org.gradle.api.tasks.PathSensitive
28 import org.gradle.api.tasks.PathSensitivity
29 import org.gradle.api.tasks.TaskAction
30 import org.gradle.work.DisableCachingByDefault
31 
32 /**
33  * A task for copying JAR resources files located in the repository structure into a generated resource source set
34  * that matches the JAR's resources structure. This is necessary due to the repository's structure not being the
35  * standard Gradle source set structure.
36  */
37 @DisableCachingByDefault(because = "Not worth caching")
38 abstract class ResourceCopyTask : DefaultTask() {
39 
40     /**
41      * Specifications of resource files to copy and their destination directory within the JAR.
42      */
43     @get:Input
44     abstract val resourceSpecs: MapProperty<String, String>
45 
46     @get:InputFiles
47     @get:PathSensitive(PathSensitivity.RELATIVE)
48     abstract val inputFiles: ListProperty<RegularFile>
49 
50     @get:OutputDirectory
51     abstract val outputDirectory: DirectoryProperty
52 
53     @TaskAction
54     fun execute() {
55         val specMap = resourceSpecs.get()
56         inputFiles.get().forEach { resourceFile ->
57             val inputFile = resourceFile.asFile
58             check(inputFile.exists()) {
59                 "Resource file does not exist: $inputFile"
60             }
61             check(inputFile.isFile) {
62                 "Resource file must be a file not a directory: $inputFile"
63             }
64             val jarOutputDir = specMap.getValue(inputFile.path)
65             val outputFile = outputDirectory.get().dir(jarOutputDir).file(inputFile.name).asFile
66             inputFile.copyTo(outputFile, overwrite = true)
67         }
68     }
69 }