Advertisement
Guest User

Untitled

a guest
Dec 5th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. `
  2. package org.example
  3.  
  4. import org.gradle.api.artifacts.transform.InputArtifact
  5. import org.gradle.api.artifacts.transform.TransformAction
  6. import org.gradle.api.artifacts.transform.TransformOutputs
  7. import org.gradle.api.artifacts.transform.TransformParameters
  8. import org.gradle.api.file.ArchiveOperations
  9. import org.gradle.api.file.FileSystemLocation
  10. import org.gradle.api.provider.Provider
  11. import java.util.jar.JarEntry
  12. import java.util.jar.JarInputStream
  13. import java.util.jar.JarOutputStream
  14. import javax.inject.Inject
  15.  
  16. abstract class JavaModuleTransform implements TransformAction<TransformParameters.None> {
  17.  
  18. @InputArtifact
  19. abstract Provider<FileSystemLocation> getInputArtifact()
  20.  
  21. @Inject
  22. abstract ArchiveOperations getArchiveOperations()
  23.  
  24. @Override
  25. void transform(TransformOutputs outputs) {
  26. def isModule = archiveOperations.zipTree(inputArtifact).any { it.name == "module-info.class" }
  27. if (isModule) {
  28. outputs.file(inputArtifact)
  29. } else {
  30. def originalJar = getInputArtifact().get().asFile
  31. def originalJarName = originalJar.name.substring(0, originalJar.name.lastIndexOf('.'))
  32.  
  33. if (this.class.getResourceAsStream("${originalJarName}/module-info.class") == null) {
  34. return;
  35. }
  36. println("${originalJarName}/module-info.class")
  37. def target = outputs.file(originalJarName + "-module.jar")
  38.  
  39. new JarInputStream(new FileInputStream(originalJar)).withStream { input ->
  40. new JarOutputStream(new FileOutputStream(target), input.manifest).withStream { output ->
  41. var jarEntry = input.nextJarEntry
  42. while (jarEntry != null) {
  43. output.putNextEntry(jarEntry)
  44. // Use a ByteArrayOutputStream to read the content of the entry
  45. ByteArrayOutputStream baos = new ByteArrayOutputStream()
  46. byte[] buffer = new byte[1024]
  47. int len
  48. while ((len = input.read(buffer)) != -1) {
  49. baos.write(buffer, 0, len)
  50. }
  51. byte[] bytes = baos.toByteArray()
  52. output.write(bytes)
  53. output.closeEntry()
  54. jarEntry = input.nextJarEntry
  55. }
  56.  
  57. output.putNextEntry(new JarEntry("module-info.class"))
  58. // Use getResourceAsStream directly without BufferedInputStream
  59. InputStream inputStream = this.class.getResourceAsStream("${originalJarName}/module-info.class")
  60. System.out.println("${originalJarName}/module-info.class")
  61. byte[] bytes = new byte[inputStream.available()]
  62. DataInputStream dataInputStream = new DataInputStream(inputStream)
  63. dataInputStream.readFully(bytes)
  64. output.write(bytes)
  65. output.closeEntry()
  66. }
  67. }
  68. }
  69. }
  70. }
  71. `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement