-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Understanding Gradle #35 – Working with Files | ||
|
||
**👇 click thumbnail to watch video** | ||
|
||
[<img src="https://onepiecesoftware.github.io/img/videos/35.png" width="640">](https://www.youtube.com/watch?v=LhVhC86FVIY&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE) | ||
|
||
How to copy and package files with Gradle in an elegant way? | ||
|
||
## Explore this sample | ||
|
||
This sample is best explored in [IntelliJ IDEA](https://www.jetbrains.com/idea/download). | ||
Open **`35_Working_with_Files/my-project`** in IDEA and confirm with _Trust Project_. | ||
|
||
## Further readings | ||
|
||
* [Working With Files](https://docs.gradle.org/current/userguide/working_with_files.html) | ||
|
||
## Need Gradle support? | ||
|
||
Contact me, if you need help with Gradle: [onepiece.Software](https://onepiece.software). |
3 changes: 3 additions & 0 deletions
3
35_Working_with_Files/my-build-logic/java-plugins/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
plugins { | ||
`kotlin-dsl` | ||
} |
52 changes: 52 additions & 0 deletions
52
...ing_with_Files/my-build-logic/java-plugins/src/main/kotlin/my-java-application.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import org.example.GenerateStartScript | ||
|
||
plugins { | ||
id("java") | ||
} | ||
|
||
val generateStartScript = tasks.register<GenerateStartScript>("generateStartScript") { | ||
mainClass = "org.example.app.App" | ||
scriptFile = layout.buildDirectory.file("tmp/scripts/run") | ||
} | ||
|
||
val installSpec = copySpec { | ||
from(generateStartScript) | ||
|
||
into("app") { | ||
from(tasks.jar) | ||
} | ||
into("libs") { | ||
from(configurations.runtimeClasspath) { | ||
// filter, include, exclude, ... | ||
rename { | ||
it.substring(0, it.lastIndexOf("-")) + ".jar" | ||
} | ||
} | ||
// from(configurations.runtimeClasspath.get().elements.map { it.map { jar -> zipTree(jar) } }) { | ||
// exclude("META-INF/**") | ||
// } | ||
} | ||
} | ||
|
||
val packageSpec = copySpec { | ||
with(installSpec) | ||
|
||
into("meta") { | ||
from(layout.projectDirectory.file("version.txt")) | ||
} | ||
} | ||
|
||
tasks.register<Sync>("install") { // in special cases: <Copy> as alternative | ||
with(installSpec) | ||
destinationDir = layout.buildDirectory.dir("install").get().asFile | ||
} | ||
|
||
tasks.register<Zip>("package") { | ||
with(packageSpec) | ||
destinationDirectory = layout.buildDirectory.dir("dist") | ||
} | ||
|
||
tasks.build { | ||
dependsOn(tasks.named("install")) | ||
dependsOn(tasks.named("package")) | ||
} |
44 changes: 44 additions & 0 deletions
44
...with_Files/my-build-logic/java-plugins/src/main/kotlin/org/example/GenerateStartScript.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.example; | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.internal.file.FileOperations | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
import java.nio.file.Files | ||
import java.nio.file.attribute.PosixFilePermission.* | ||
import javax.inject.Inject | ||
|
||
@CacheableTask | ||
abstract class GenerateStartScript : DefaultTask() { | ||
|
||
@get:Input | ||
abstract val mainClass: Property<String> | ||
|
||
@get:OutputFile | ||
abstract val scriptFile: RegularFileProperty | ||
|
||
// @get:Inject | ||
// abstract val files: FileOperations | ||
|
||
@TaskAction | ||
fun generate() { | ||
val main = mainClass.get() // String | ||
val out = scriptFile.get().asFile // java.io.File | ||
val script = "java -cp 'libs/*:app/*' $main" | ||
|
||
out.writeText(script) | ||
Files.setPosixFilePermissions(out.toPath(), setOf( | ||
OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, | ||
GROUP_READ, GROUP_EXECUTE, | ||
OTHERS_EXECUTE, OTHERS_READ | ||
)) | ||
|
||
// files.sync { | ||
// ... | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
dependencyResolutionManagement { | ||
repositories.gradlePluginPortal() | ||
} | ||
|
||
include("java-plugins") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins { | ||
id("my-java-application") | ||
} | ||
|
||
dependencies { | ||
implementation("org.apache.commons:commons-text:1.11.0") | ||
} |
10 changes: 10 additions & 0 deletions
10
35_Working_with_Files/my-project/app/src/main/java/org/example/app/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.example.app; | ||
|
||
import org.apache.commons.text.CaseUtils; | ||
|
||
public class App { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(CaseUtils.toCamelCase("hello world \uD83D\uDE80", true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.34 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Locations of Gradle plugins | ||
pluginManagement { | ||
repositories.gradlePluginPortal() | ||
includeBuild("../my-build-logic") | ||
} | ||
|
||
// Location of other components | ||
dependencyResolutionManagement { | ||
repositories.mavenCentral() | ||
} | ||
|
||
include("app") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters