All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gradleup.gr8.EmbeddedJarTask.kt Maven / Gradle / Ivy

There is a newer version: 0.10
Show newest version
package com.gradleup.gr8

import com.gradleup.gr8.ZipHelper.buildZip
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import java.io.File

/**
 * A task that generates an embedded jar from a list of jars
 */
abstract class EmbeddedJarTask : DefaultTask() {
  @get:InputFile
  internal abstract val mainJar: RegularFileProperty

  @get:InputFiles
  internal abstract val otherJars: ConfigurableFileCollection

  @get:Input
  internal abstract val excludes: ListProperty

  @get:OutputFile
  internal abstract val outputJar: RegularFileProperty

  fun mainJar(fileProvider: Provider) {
    mainJar.fileProvider(fileProvider)
    mainJar.disallowChanges()
  }

  fun otherJars(any: Any) {
    otherJars.setFrom(any)
    otherJars.disallowChanges()
  }

  fun outputJar(file: File) {
    outputJar.set(file)
    outputJar.disallowChanges()
  }

  fun outputJar(): Provider = outputJar

  @TaskAction
  fun taskAction() {
    val regexes = excludes.getOrElse(emptyList()).map { Regex(it) }

    buildZip(outputJar.asFile.get()) {
      addZipFile(mainJar.asFile.get())

      otherJars.files.forEach {
        addZipFile(it) {
          if (regexes.any { it.matches(entry.name) }) {
            skip()
          }
        }
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy