Gradle 无法复制 Java 项目中的 PNG 文件。

huangapple go评论112阅读模式
英文:

Gradle can't copy png file in java project

问题

  1. so I've added a png img to my resources, since I want to use it in my project.
  2. But Gradle seems not to let me build the project anymore, it's giving me the following error:
  3. * What went wrong:
  4. Execution failed for task ':processResources'.
  5. > Could not copy file 'C:\workspace\Java\Utilities\EmailService\src\main\resources\img\watermark.png' to 'C:\workspace\Java\Utilities\EmailService\build\resources\main\img\watermark.png'.
  6. Important part of Stacktrace is:
  7. Caused by: groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
  8. SimpleTemplateScript28.groovy: 4: illegal string body character after dollar sign;
  9. solution: either escape a literal dollar sign "$5" or bracket the value expression "${5}" @ line 4, column 99.
  10. o’¿dHÌIDATx^Ý?╝$Eı┼Ù¥Ö┘]r♫↕$-↓$g%(&êê;
  11. ^
  12. My setup of the project:
  13. [![Image of file setup][1]][1]
  14. build.gradle:
  15. import java.text.SimpleDateFormat
  16. import org.apache.tools.ant.filters.ReplaceTokens
  17. buildscript {
  18. ext {
  19. springBootVersion = '2.1.5.RELEASE'
  20. set('springCloudVersion', 'Greenwich.RELEASE')
  21. }
  22. dependencies {
  23. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  24. }
  25. }
  26. plugins {
  27. id 'com.palantir.git-version' version '0.11.0'
  28. }
  29. def appName = 'EmailService'
  30. version = '1.1.5'
  31. group = 'dk.xx.email'
  32. if (!hasProperty('mainClass')) {
  33. ext.mainClass = 'dk.xx.email.EmailApplication'
  34. }
  35. apply plugin: 'java'
  36. apply plugin: 'eclipse'
  37. apply plugin: 'org.springframework.boot'
  38. apply plugin: 'io.spring.dependency-management'
  39. apply plugin: 'maven-publish'
  40. apply plugin: 'com.palantir.git-version'
  41. sourceCompatibility = 1.8
  42. repositories {
  43. maven {
  44. credentials {
  45. username XXX
  46. password XXXXXXX
  47. }
  48. url 'http://maven01.local:8080/repository/XX/'
  49. }
  50. mavenCentral()
  51. maven { url 'https://jitpack.io' }
  52. }
  53. bootJar {
  54. baseName = "${appName}"
  55. version = "${version}"
  56. }
  57. dependencies {
  58. compile('net.sf.jt400:jt400:9.5:jt400_jdk8')
  59. compile('org.springframework.boot:spring-boot-starter-data-jpa')
  60. compile('org.springframework.boot:spring-boot-starter-data-rest')
  61. compile('org.springframework.boot:spring-boot-starter-actuator')
  62. compile group: 'org.springframework', name: 'spring-mock', version: '2.0.8'
  63. compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.1.0.RELEASE'
  64. compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.1.0.RELEASE'
  65. compile group: 'xx', name: 'NhoData-Entities', version: '0.1.99'
  66. compile group: 'xx', name: 'xx-Entities', version: '2.1.7'
  67. compile('dk.xx.stakeholder.document.archive:DocumentArchive:1.1.10:proxy')
  68. compile group: 'com.itextpdf', name: 'itext7-core', version: '7.0.4'
  69. compile('dk.xx.gui.start:EngagementGui:1.2.2:proxy')
  70. compileOnly("org.projectlombok:lombok:1.18.6")
  71. annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
  72. annotationProcessor 'org.projectlombok:lombok:1.18.6'
  73. testCompile('org.springframework.boot:spring-boot-starter-test')
  74. compile('org.springframework.boot:spring-boot-starter-mail')
  75. compile('org.springframework.boot:spring-boot-starter-thymeleaf')
  76. // Auto-produce swagger
  77. compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
  78. compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
  79. }
  80. task versionTxt() {
  81. doLast {
  82. def versionFile = new File("$projectDir/.ci/version.txt");
  83. versionFile.getParentFile().mkdir();
  84. versionFile.text = """
  85. Version=$version
  86. BuildTime=${new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())}
  87. ApplicationName=${appName}
  88. """
  89. }
  90. }
  91. def dependencyName = "${appName}-proxy-${version}"
  92. task tJar(type: Jar, dependsOn: compileJava) {
  93. archiveName = "${dependencyName}.jar"
  94. classifier = 'proxy'
  95. from(sourceSets.main.output) {
  96. includeEmptyDirs=false
  97. include '**/feign/*'
  98. include '**/domain/*'
  99. include '**/entities/*'
  100. }
  101. }
  102. task sourcesJar(type: Jar) {
  103. classifier = 'sources'
  104. from sourceSets.main.allJava
  105. }
  106. publishing {
  107. publications {
  108. maven(MavenPublication) {
  109. artifacts {
  110. groupId "${group}"
  111. artifactId "${appName}"
  112. version "${version}"
  113. }
  114. artifact tJar
  115. artifact sourcesJar
  116. }
  117. repositories.maven {
  118. url 'http://maven01.local:8080/repository/xx/'
  119. credentials {
  120. username xx
  121. password xxxxxxxx
  122. }
  123. }
  124. }
  125. }
  126. processResources {
  127. filter(ReplaceTokens, tokens:[gitVersion: gitVersion()])
  128. expand(project.properties)
  129. }
  130. static def buildTime() {
  131. final dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
  132. dateFormat.timeZone = TimeZone.getTimeZone('UTC')
  133. dateFormat.format(new Date())
  134. }
  135. springBoot {
  136. // This statement tells the Gradle Spring Boot plugin to generate a file
  137. // build/resources/main/META-INF/build-info.properties
  138. // that is picked up by Spring Boot to display via /actuator/info endpoint.
  139. buildInfo {
  140. // Generate extra build info.
  141. properties {
  142. def details = versionDetails()
  143. additional = [
  144. by : System.properties['user.name'],
  145. time : buildTime(),
  146. operatingSystem : "${System.properties['os.name']} (${System.properties['os.version']})",
  147. machine : InetAddress.localHost.hostName,
  148. ci_enabled : System.getenv('CI') ? true : false,
  149. ci_buildNumber : System.env.'BUILD_NUMBER' ?: 'UNKNOWN',
  150. ci_jobName : System.env.'JOB_NAME' ?: 'UNKNOWN',
  151. appVersion : version
  152. ]
  153. }
  154. }
  155. }
  1. <details>
  2. <summary>英文:</summary>
  3. so I&#39;ve added a png img to my resources, since I want to use it in my project.
  4. But Gradle seems not to let me build the project anymore, it&#39;s giving me the following error:
  5. * What went wrong:
  6. Execution failed for task &#39;:processResources&#39;.
  7. &gt; Could not copy file &#39;C:\workspace\Java\Utilities\EmailService\src\main\resources\img\watermark.png&#39; to &#39;C:\workspace\Java\Utilities\EmailService\build\resources\main\img\watermark.png&#39;.
  8. Important part of Stacktrace is:
  9. Caused by: groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
  10. SimpleTemplateScript28.groovy: 4: illegal string body character after dollar sign;
  11. solution: either escape a literal dollar sign &quot;\$5&quot; or bracket the value expression &quot;${5}&quot; @ line 4, column 99.
  12. o&#191;dH&#204;IDATx^&#221;?╝$Eı┼&#217;&#165;&#214;┘]r♫↕$-↓$g%(&#234;&#234;
  13. ^
  14. My setup of the project:
  15. [![Image of file setup][1]][1]
  16. build.gradle:
  17. import java.text.SimpleDateFormat
  18. import org.apache.tools.ant.filters.ReplaceTokens
  19. buildscript {
  20. ext {
  21. springBootVersion = &#39;2.1.5.RELEASE&#39;
  22. set(&#39;springCloudVersion&#39;, &#39;Greenwich.RELEASE&#39;)
  23. }
  24. dependencies {
  25. classpath(&quot;org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}&quot;)
  26. }
  27. }
  28. plugins {
  29. id &#39;com.palantir.git-version&#39; version &#39;0.11.0&#39;
  30. }
  31. def appName = &#39;EmailService&#39;
  32. version = &#39;1.1.5&#39;
  33. group = &#39;dk.xx.email&#39;
  34. if (!hasProperty(&#39;mainClass&#39;)) {
  35. ext.mainClass = &#39;dk.xx.email.EmailApplication&#39;
  36. }
  37. apply plugin: &#39;java&#39;
  38. apply plugin: &#39;eclipse&#39;
  39. apply plugin: &#39;org.springframework.boot&#39;
  40. apply plugin: &#39;io.spring.dependency-management&#39;
  41. apply plugin: &#39;maven-publish&#39;
  42. apply plugin: &#39;com.palantir.git-version&#39;
  43. sourceCompatibility = 1.8
  44. repositories {
  45. maven {
  46. credentials {
  47. username XXX
  48. password XXXXXXX
  49. }
  50. url &#39;http://maven01.local:8080/repository/XX/&#39;
  51. }
  52. mavenCentral()
  53. maven { url &#39;https://jitpack.io&#39; }
  54. }
  55. bootJar {
  56. baseName = &quot;${appName}&quot;
  57. version = &quot;${version}&quot;
  58. }
  59. dependencies {
  60. compile(&#39;net.sf.jt400:jt400:9.5:jt400_jdk8&#39;)
  61. compile(&#39;org.springframework.boot:spring-boot-starter-data-jpa&#39;)
  62. compile(&#39;org.springframework.boot:spring-boot-starter-data-rest&#39;)
  63. compile(&#39;org.springframework.boot:spring-boot-starter-actuator&#39;)
  64. compile group: &#39;org.springframework&#39;, name: &#39;spring-mock&#39;, version: &#39;2.0.8&#39;
  65. compile group: &#39;org.springframework.cloud&#39;, name: &#39;spring-cloud-starter-netflix-eureka-client&#39;, version: &#39;2.1.0.RELEASE&#39;
  66. compile group: &#39;org.springframework.cloud&#39;, name: &#39;spring-cloud-starter-openfeign&#39;, version: &#39;2.1.0.RELEASE&#39;
  67. compile group: &#39;xx&#39;, name: &#39;NhoData-Entities&#39;, version: &#39;0.1.99&#39;
  68. compile group: &#39;xx&#39;, name: &#39;xx-Entities&#39;, version: &#39;2.1.7&#39;
  69. compile(&#39;dk.xx.stakeholder.document.archive:DocumentArchive:1.1.10:proxy&#39;)
  70. compile group: &#39;com.itextpdf&#39;, name: &#39;itext7-core&#39;, version: &#39;7.0.4&#39;
  71. compile(&#39;dk.xx.gui.start:EngagementGui:1.2.2:proxy&#39;)
  72. compileOnly(&quot;org.projectlombok:lombok:1.18.6&quot;)
  73. annotationProcessor group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-configuration-processor&#39;
  74. annotationProcessor &#39;org.projectlombok:lombok:1.18.6&#39;
  75. testCompile(&#39;org.springframework.boot:spring-boot-starter-test&#39;)
  76. compile(&#39;org.springframework.boot:spring-boot-starter-mail&#39;)
  77. compile(&#39;org.springframework.boot:spring-boot-starter-thymeleaf&#39;)
  78. // Auto-produce swagger
  79. compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger2&#39;, version: &#39;2.9.2&#39;
  80. compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger-ui&#39;, version: &#39;2.9.2&#39;
  81. }
  82. task versionTxt() {
  83. doLast {
  84. def versionFile = new File(&quot;$projectDir/.ci/version.txt&quot;);
  85. versionFile.getParentFile().mkdir();
  86. versionFile.text = &quot;&quot;&quot;
  87. Version=$version
  88. BuildTime=${new SimpleDateFormat(&quot;dd-MM-yyyy HH:mm:ss&quot;).format(new Date())}
  89. ApplicationName=${appName}
  90. &quot;&quot;&quot;
  91. }
  92. }
  93. def dependencyName = &quot;${appName}-proxy-${version}&quot;
  94. task tJar(type: Jar, dependsOn: compileJava) {
  95. archiveName = &quot;${dependencyName}.jar&quot;
  96. classifier = &#39;proxy&#39;
  97. from(sourceSets.main.output) {
  98. includeEmptyDirs=false
  99. include &#39;**/feign/*&#39;
  100. include &#39;**/domain/*&#39;
  101. include &#39;**/entities/*&#39;
  102. }
  103. }
  104. task sourcesJar(type: Jar) {
  105. classifier = &#39;sources&#39;
  106. from sourceSets.main.allJava
  107. }
  108. publishing {
  109. publications {
  110. maven(MavenPublication) {
  111. artifacts {
  112. groupId &quot;${group}&quot;
  113. artifactId &quot;${appName}&quot;
  114. version &quot;${version}&quot;
  115. }
  116. artifact tJar
  117. artifact sourcesJar
  118. }
  119. repositories.maven {
  120. url &#39;http://maven01.local:8080/repository/xx/&#39;
  121. credentials {
  122. username xx
  123. password xxxxxxxx
  124. }
  125. }
  126. }
  127. }
  128. processResources {
  129. filter(ReplaceTokens, tokens:[gitVersion: gitVersion()])
  130. expand(project.properties)
  131. }
  132. static def buildTime() {
  133. final dateFormat = new SimpleDateFormat(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;)
  134. dateFormat.timeZone = TimeZone.getTimeZone(&#39;UTC&#39;)
  135. dateFormat.format(new Date())
  136. }
  137. springBoot {
  138. // This statement tells the Gradle Spring Boot plugin to generate a file
  139. // build/resources/main/META-INF/build-info.properties
  140. // that is picked up by Spring Boot to display via /actuator/info endpoint.
  141. buildInfo {
  142. // Generate extra build info.
  143. properties {
  144. def details = versionDetails()
  145. additional = [
  146. by : System.properties[&#39;user.name&#39;],
  147. time : buildTime(),
  148. operatingSystem : &quot;${System.properties[&#39;os.name&#39;]} (${System.properties[&#39;os.version&#39;]})&quot;,
  149. machine : InetAddress.localHost.hostName,
  150. ci_enabled : System.getenv(&#39;CI&#39;) ? true : false,
  151. ci_buildNumber : System.env.&#39;BUILD_NUMBER&#39; ?: &#39;UNKNOWN&#39;,
  152. ci_jobName : System.env.&#39;JOB_NAME&#39; ?: &#39;UNKNOWN&#39;,
  153. appVersion : version
  154. ]
  155. }
  156. }
  157. }
  158. [1]: https://i.stack.imgur.com/VTGSG.png
  159. </details>
  160. # 答案1
  161. **得分**: 1
  162. 我认为是你的 `processResources` 配置中的 `filter` 语句。你正在要求 Gradle 替换 PNG 内的文本,但它无法理解 PNG 中的文本(这是因为它内部没有文本;可能不符合任何合理的字符编码)。
  163. <details>
  164. <summary>英文:</summary>
  165. I think it&#39;s the `filter` statement in your `processResources` config. You&#39;re asking Gradle to replace text inside the PNG, but it can&#39;t make sense of the text in the PNG (which is because there is no text in it; probably doesn&#39;t conform to any reasonable character encoding).
  166. </details>

huangapple
  • 本文由 发表于 2020年10月14日 14:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64347898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定