Skip to content

Commit

Permalink
35 – Working with Files
Browse files Browse the repository at this point in the history
  • Loading branch information
jjohannes committed Nov 6, 2023
1 parent 087caff commit e5c20e4
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ jobs:
- run: "gradle run -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 31_The_Module_Path/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 32_Artifact_Transforms/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 33_Classpath_and_Module_Path_in_Testing/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 34_Properties_and_Providers/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 34_Properties_and_Providers/my-project"
- run: "gradle build -Dorg.gradle.kotlin.dsl.skipMetadataVersionCheck=false --configuration-cache --warning-mode=fail -p 35_Working_with_Files/my-project"
20 changes: 20 additions & 0 deletions 35_Working_with_Files/README.MD
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).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
`kotlin-dsl`
}
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"))
}
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 {
// ...
// }
}
}
5 changes: 5 additions & 0 deletions 35_Working_with_Files/my-build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencyResolutionManagement {
repositories.gradlePluginPortal()
}

include("java-plugins")
7 changes: 7 additions & 0 deletions 35_Working_with_Files/my-project/app/build.gradle.kts
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")
}
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));
}
}
1 change: 1 addition & 0 deletions 35_Working_with_Files/my-project/app/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.34
12 changes: 12 additions & 0 deletions 35_Working_with_Files/my-project/settings.gradle.kts
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")
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ This gives you a general understanding of how things work in Gradle and enables
## More Fundamentals (Entries 34 - ??)

34. [Properties and Providers](34_Properties_and_Providers)
35. [Working with Files](35_Working_with_Files)

## Need Gradle support?

Expand Down

0 comments on commit e5c20e4

Please sign in to comment.