Gradle Plugin "maven-publish" with error "unmappable character for encoding"

I recently migrated a project from Maven to Gradle (IE: replaced the pom.xml file with the build.gradle file). Since my company still has a few downstream Maven projects that depend on it, we must continue to publish the pom file along with the JAR of the class, source and javadoc to our Nexus repository so that downstream projects can still use this project as a dependency Maven. This is what the 'maven-publish' plugin for Gradle does. This is what my build.gradle file looks like with configurations for the maven-publish plugin:

apply plugin: 'java'
apply plugin: 'maven-publish'

group = 'com.my.company'
version = 'myProduct-SNAPSHOT'

repositories {
    mavenLocal()
    maven { url 'http://nexus.my.company.net/nexus/content/repositories/build-test-release' }
    maven { url 'http://nexus.my.company.net/nexus/content/repositories/build-test-snapshot' }
    maven { url 'http://nexus.my.company.net/nexus/content/repositories/central' }
    maven { url 'http://nexus.my.company.net/nexus/content/repositories/thirdparty' }
}

dependencies {
    // Stuff
}

sourceSets {
    main {
        // Stuff
    }
}

task sourcesJar (type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allJava
}

task javadocJar (type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

publishing {
    publications {
        mavenJars(MavenPublication) {

            // Classes
            from components.java

            // Sources
            artifact(sourcesJar) {
                classifier = 'sources'
            }

            // Javadoc
            artifact(javadocJar) {
                classifier = 'javadoc'
            }
        }
    }
    repositories {
        maven {
            url 'http://nexus.my.company.net/nexus/content/repositories/build-test-snapshot'
            credentials {
                username 'user'
                password 'password'
            }
        }
    }
}

      

This creates a <Gradle Publishing task that will generate the required pom.xml file as well as the class, source and javadoc JAR classes. It will then publish the jars to the Nexus 'build-test-snapshot' repository.

This works great on my machine as well as our build servers and most of my colleagues' boxes. However, some employees are complaining because the "gradlew publish" command (note that the Gradle 2.4 wrapper was included in the project) does not work for them with errors like this:

C:\Users\user\Perforce\WORKSPACE\project\src\main\java\com\my\company\test\randomizati
on\RandomHelper.java:18: error: unmappable character for encoding Cp1252
    private static final String NON_ENGLISH_CHARACTERS = "1234567890─Ç─?─é─â─ä─à─å─ç─ê─ë─è─ï─î─?─Ä─?─?─æ─Æ─ô─ö─ò─û─ù─ÿ─Ö
─Ü─¢─£─?─₧─ƒ─á─í─ó─ú─ñ─Ñ─ª─º─¿─⌐─¬─½─¼─¡─«─»─░─▒─▓─│─┤─╡─╢─╖─╕─╣─║─╗─╝─╜─╛─┐┼Ç┼?┼é┼â┼ä┼à┼å┼ç┼ê┼ë┼è┼ï┼î┼?┼Ä┼?┼?┼æ┼Æ┼ô┼ö┼ò
┼û┼ù┼ÿ┼Ö┼Ü┼¢┼£┼?┼₧┼ƒ┼á┼í┼ó┼ú┼ñ┼Ñ┼ª┼º┼¿┼⌐┼¬┼½┼¼┼¡┼«┼»┼░┼▒┼▓┼│┼┤┼╡┼╢┼╖┼╕┼╣┼║┼╗┼╝┼╜┼╛┼┐╞Ç╞?╞é╞â╞ä╞à╞å╞ç╞ê╞ë╞è╞ï╞î╞?╞Ä╞?╞Æ╞á
╞í╞»╟?╟Ä╟?╟?╟æ╟Æ╟ô╟ö╟ò╟û╟ù╟ÿ╟Ö╟Ü╟¢╟£╟║╟╗╟╝╟╜╟╛╟┐├Ç├?├é├â├ä├à├å├ç├ê├ë├è├ï├î├?├Ä├?├?├æ├Æ├ô├ö├ò├û├ù├ÿ├Ö├Ü├¢├£├?├₧├ƒ├á├í├ó├ú
äåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ&-.*$";

      

Interestingly, the "gradlew build" command (which compiles the project using javac) works great on all machines. This is the "gradlew publish" command which has problems on some machines (either pass or crash on Mac and Windows).

I can't tell you what the key difference is here, but I suspect that the affected users have their Perforce clients misconfigured (no P4CHARSET specified) and all project files are synced to their machines with the default encoding. It's not clear to me how to check which Perforce encoding is used for synchronization, so this is just a theory. As far as I understand, even if they fix their configurations, they still have to kill their recruits and download everything from the Perforce server again to get all the files with the correct encoding (utf16le-bom).

Regardless of any Perforce-related issues, it’s my fault as I am the one who first switched us to Gradle. If there is a way I can tweak the maven-publish plugin to work around the problem, I would rather do it. If javac can compile non-standard symbols from these files into classes, then there is no reason the plugin cannot read them and generate javadoc for them, right? Maybe you need to explicitly specify which encoding to use?

I've already tried playing with the -Dfile.encoding = x-UTF-16LE-BOM option in JAVA_OPTS and GRADLE_OPTS (it didn't matter). I also found the following snippet used to specify the encoding of Compile tasks in Gradle:

tasks.withType(Compile) {
    options.encoding = 'x-UTF-16LE-BOM'
}

      

Unfortunately, this is for Compilation tasks and the project is already compiling successfully. I'm not sure what types of tasks the maven-publish plugin generates, so I don't know how to apply this approach for these tasks (is it possible?).

What else can I try to disable the Gradle maven-publish plugin to disable files by encoding files?

+3


source to share


1 answer


After diving into the Gradle API, I came up with the following:

// Force character encoding in case the workspace was not set up correctly
tasks.withType(Javadoc) {
    options.encoding = 'x-UTF-16LE-BOM'
}

      



This solved the problem on machines that did not previously run with coding errors when running 'gradlew publish'.

Note. For most use cases, options.encoding should be set to UTF8, not x-UTF-16LE.

+1


source







All Articles