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

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

Gradle can't copy png file in java project

问题

so I've added a png img to my resources, since I want to use it in my project.  
But Gradle seems not to let me build the project anymore, it's giving me the following error:  

    * What went wrong:
    Execution failed for task ':processResources'.
    > 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'.

Important part of Stacktrace is:  

    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:
    SimpleTemplateScript28.groovy: 4: illegal string body character after dollar sign;
       solution: either escape a literal dollar sign "$5" or bracket the value expression "${5}" @ line 4, column 99.
       o’¿dHÌIDATx^Ý?╝$Eı┼Ù¥Ö┘]r♫↕$-↓$g%(&êê;
                                     ^

My setup of the project:  
[![Image of file setup][1]][1]



build.gradle:

import java.text.SimpleDateFormat
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
    ext {
        springBootVersion = '2.1.5.RELEASE'
        set('springCloudVersion', 'Greenwich.RELEASE')
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
plugins {
    id 'com.palantir.git-version' version '0.11.0'
}
def appName = 'EmailService'
version = '1.1.5'
group = 'dk.xx.email'
if (!hasProperty('mainClass')) {
    ext.mainClass = 'dk.xx.email.EmailApplication'
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'
apply plugin: 'com.palantir.git-version'

sourceCompatibility = 1.8

repositories {
    maven {
        credentials {
            username XXX
            password XXXXXXX
        }
        url 'http://maven01.local:8080/repository/XX/'
    }
    mavenCentral()
    maven { url 'https://jitpack.io' }
}
bootJar {
    baseName = "${appName}"
    version =  "${version}"
}
dependencies {
    compile('net.sf.jt400:jt400:9.5:jt400_jdk8')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile group: 'org.springframework', name: 'spring-mock', version: '2.0.8'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.1.0.RELEASE'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version: '2.1.0.RELEASE'
    compile group: 'xx', name: 'NhoData-Entities', version: '0.1.99'
    compile group: 'xx', name: 'xx-Entities', version: '2.1.7'
    compile('dk.xx.stakeholder.document.archive:DocumentArchive:1.1.10:proxy')
    compile group: 'com.itextpdf', name: 'itext7-core', version: '7.0.4'
    compile('dk.xx.gui.start:EngagementGui:1.2.2:proxy')
    compileOnly("org.projectlombok:lombok:1.18.6")
    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok:1.18.6'
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile('org.springframework.boot:spring-boot-starter-mail')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')

    // Auto-produce swagger
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

}

task versionTxt()  {
    doLast {
        def versionFile = new File("$projectDir/.ci/version.txt");
        versionFile.getParentFile().mkdir();
        versionFile.text = """
            Version=$version
            BuildTime=${new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date())}
            ApplicationName=${appName}
            """
    }
}
def dependencyName = "${appName}-proxy-${version}"
task tJar(type: Jar, dependsOn: compileJava) {
    archiveName = "${dependencyName}.jar"
    classifier = 'proxy'
    from(sourceSets.main.output) {
        includeEmptyDirs=false
        include '**/feign/*'
        include '**/domain/*'
        include '**/entities/*'
    }
}
task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allJava
}
publishing {
    publications {
        maven(MavenPublication) {
            artifacts {
                groupId "${group}"
                artifactId "${appName}"
                version "${version}"
            }
            artifact tJar
            artifact sourcesJar
        }
        repositories.maven {
            url 'http://maven01.local:8080/repository/xx/'
            credentials {
                username xx
                password xxxxxxxx
            }
        }
    }
}

processResources {
    filter(ReplaceTokens, tokens:[gitVersion: gitVersion()])
    expand(project.properties)
}
static def buildTime() {
    final dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    dateFormat.timeZone = TimeZone.getTimeZone('UTC')
    dateFormat.format(new Date())
}
springBoot {
    // This statement tells the Gradle Spring Boot plugin to generate a file
    // build/resources/main/META-INF/build-info.properties
    // that is picked up by Spring Boot to display via /actuator/info endpoint.
    buildInfo {
        // Generate extra build info.
        properties {
            def details = versionDetails()
            additional = [
                by                   : System.properties['user.name'],
                time                 : buildTime(),
                operatingSystem      : "${System.properties['os.name']} (${System.properties['os.version']})",
                machine              : InetAddress.localHost.hostName,
                ci_enabled           : System.getenv('CI') ? true : false,
                ci_buildNumber       : System.env.'BUILD_NUMBER' ?: 'UNKNOWN',
                ci_jobName           : System.env.'JOB_NAME' ?: 'UNKNOWN',
                appVersion           : version
            ]
        }
    }
}

<details>
<summary>英文:</summary>
so I&#39;ve added a png img to my resources, since I want to use it in my project.  
But Gradle seems not to let me build the project anymore, it&#39;s giving me the following error:  
* What went wrong:
Execution failed for task &#39;:processResources&#39;.
&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;.
Important part of Stacktrace is:  
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:
SimpleTemplateScript28.groovy: 4: illegal string body character after dollar sign;
solution: either escape a literal dollar sign &quot;\$5&quot; or bracket the value expression &quot;${5}&quot; @ line 4, column 99.
o&#191;dH&#204;IDATx^&#221;?╝$Eı┼&#217;&#165;&#214;┘]r♫↕$-↓$g%(&#234;&#234;
^
My setup of the project:  
[![Image of file setup][1]][1]
build.gradle:
import java.text.SimpleDateFormat
import org.apache.tools.ant.filters.ReplaceTokens
buildscript {
ext {
springBootVersion = &#39;2.1.5.RELEASE&#39;
set(&#39;springCloudVersion&#39;, &#39;Greenwich.RELEASE&#39;)
}
dependencies {
classpath(&quot;org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}&quot;)
}
}
plugins {
id &#39;com.palantir.git-version&#39; version &#39;0.11.0&#39;
}
def appName = &#39;EmailService&#39;
version = &#39;1.1.5&#39;
group = &#39;dk.xx.email&#39;
if (!hasProperty(&#39;mainClass&#39;)) {
ext.mainClass = &#39;dk.xx.email.EmailApplication&#39;
}
apply plugin: &#39;java&#39;
apply plugin: &#39;eclipse&#39;
apply plugin: &#39;org.springframework.boot&#39;
apply plugin: &#39;io.spring.dependency-management&#39;
apply plugin: &#39;maven-publish&#39;
apply plugin: &#39;com.palantir.git-version&#39;
sourceCompatibility = 1.8
repositories {
maven {
credentials {
username XXX
password XXXXXXX
}
url &#39;http://maven01.local:8080/repository/XX/&#39;
}
mavenCentral()
maven { url &#39;https://jitpack.io&#39; }
}
bootJar {
baseName = &quot;${appName}&quot;
version =  &quot;${version}&quot;
}
dependencies {
compile(&#39;net.sf.jt400:jt400:9.5:jt400_jdk8&#39;)
compile(&#39;org.springframework.boot:spring-boot-starter-data-jpa&#39;)
compile(&#39;org.springframework.boot:spring-boot-starter-data-rest&#39;)
compile(&#39;org.springframework.boot:spring-boot-starter-actuator&#39;)
compile group: &#39;org.springframework&#39;, name: &#39;spring-mock&#39;, version: &#39;2.0.8&#39;
compile group: &#39;org.springframework.cloud&#39;, name: &#39;spring-cloud-starter-netflix-eureka-client&#39;, version: &#39;2.1.0.RELEASE&#39;
compile group: &#39;org.springframework.cloud&#39;, name: &#39;spring-cloud-starter-openfeign&#39;, version: &#39;2.1.0.RELEASE&#39;
compile group: &#39;xx&#39;, name: &#39;NhoData-Entities&#39;, version: &#39;0.1.99&#39;
compile group: &#39;xx&#39;, name: &#39;xx-Entities&#39;, version: &#39;2.1.7&#39;
compile(&#39;dk.xx.stakeholder.document.archive:DocumentArchive:1.1.10:proxy&#39;)
compile group: &#39;com.itextpdf&#39;, name: &#39;itext7-core&#39;, version: &#39;7.0.4&#39;
compile(&#39;dk.xx.gui.start:EngagementGui:1.2.2:proxy&#39;)
compileOnly(&quot;org.projectlombok:lombok:1.18.6&quot;)
annotationProcessor group: &#39;org.springframework.boot&#39;, name: &#39;spring-boot-configuration-processor&#39;
annotationProcessor &#39;org.projectlombok:lombok:1.18.6&#39;
testCompile(&#39;org.springframework.boot:spring-boot-starter-test&#39;)
compile(&#39;org.springframework.boot:spring-boot-starter-mail&#39;)
compile(&#39;org.springframework.boot:spring-boot-starter-thymeleaf&#39;)
// Auto-produce swagger
compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger2&#39;, version: &#39;2.9.2&#39;
compile group: &#39;io.springfox&#39;, name: &#39;springfox-swagger-ui&#39;, version: &#39;2.9.2&#39;
}
task versionTxt()  {
doLast {
def versionFile = new File(&quot;$projectDir/.ci/version.txt&quot;);
versionFile.getParentFile().mkdir();
versionFile.text = &quot;&quot;&quot;
Version=$version
BuildTime=${new SimpleDateFormat(&quot;dd-MM-yyyy HH:mm:ss&quot;).format(new Date())}
ApplicationName=${appName}
&quot;&quot;&quot;
}
}
def dependencyName = &quot;${appName}-proxy-${version}&quot;
task tJar(type: Jar, dependsOn: compileJava) {
archiveName = &quot;${dependencyName}.jar&quot;
classifier = &#39;proxy&#39;
from(sourceSets.main.output) {
includeEmptyDirs=false
include &#39;**/feign/*&#39;
include &#39;**/domain/*&#39;
include &#39;**/entities/*&#39;
}
}
task sourcesJar(type: Jar) {
classifier = &#39;sources&#39;
from sourceSets.main.allJava
}
publishing {
publications {
maven(MavenPublication) {
artifacts {
groupId &quot;${group}&quot;
artifactId &quot;${appName}&quot;
version &quot;${version}&quot;
}
artifact tJar
artifact sourcesJar
}
repositories.maven {
url &#39;http://maven01.local:8080/repository/xx/&#39;
credentials {
username xx
password xxxxxxxx
}
}
}
}
processResources {
filter(ReplaceTokens, tokens:[gitVersion: gitVersion()])
expand(project.properties)
}
static def buildTime() {
final dateFormat = new SimpleDateFormat(&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;)
dateFormat.timeZone = TimeZone.getTimeZone(&#39;UTC&#39;)
dateFormat.format(new Date())
}
springBoot {
// This statement tells the Gradle Spring Boot plugin to generate a file
// build/resources/main/META-INF/build-info.properties
// that is picked up by Spring Boot to display via /actuator/info endpoint.
buildInfo {
// Generate extra build info.
properties {
def details = versionDetails()
additional = [
by                   : System.properties[&#39;user.name&#39;],
time                 : buildTime(),
operatingSystem      : &quot;${System.properties[&#39;os.name&#39;]} (${System.properties[&#39;os.version&#39;]})&quot;,
machine              : InetAddress.localHost.hostName,
ci_enabled           : System.getenv(&#39;CI&#39;) ? true : false,
ci_buildNumber       : System.env.&#39;BUILD_NUMBER&#39; ?: &#39;UNKNOWN&#39;,
ci_jobName           : System.env.&#39;JOB_NAME&#39; ?: &#39;UNKNOWN&#39;,
appVersion           : version
]
}
}
}
[1]: https://i.stack.imgur.com/VTGSG.png
</details>
# 答案1
**得分**: 1
我认为是你的 `processResources` 配置中的 `filter` 语句。你正在要求 Gradle 替换 PNG 内的文本,但它无法理解 PNG 中的文本(这是因为它内部没有文本;可能不符合任何合理的字符编码)。
<details>
<summary>英文:</summary>
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).
</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:

确定