英文:
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'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
]
}
}
}
[1]: https://i.stack.imgur.com/VTGSG.png
</details>
# 答案1
**得分**: 1
我认为是你的 `processResources` 配置中的 `filter` 语句。你正在要求 Gradle 替换 PNG 内的文本,但它无法理解 PNG 中的文本(这是因为它内部没有文本;可能不符合任何合理的字符编码)。
<details>
<summary>英文:</summary>
I think it's the `filter` statement in your `processResources` config. You're asking Gradle to replace text inside the PNG, but it can't make sense of the text in the PNG (which is because there is no text in it; probably doesn't conform to any reasonable character encoding).
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论