buildship icon indicating copy to clipboard operation
buildship copied to clipboard

Transfer the source file encoding from Gradle to Eclipse

Open donat opened this issue 8 years ago • 15 comments

Original post: https://bugs.eclipse.org/bugs/show_bug.cgi?id=488947

donat avatar Feb 08 '17 15:02 donat

Related: gradle/gradle#898

What workaround could be applied until this is solved?

spoenemann avatar Aug 21 '18 12:08 spoenemann

Not really at the moment, however, we'll soon implement new APIs that might be useful for you. We're going enable clients to hook into the project synchronization and to access the Tooling APIs to query arbitrary configuration from the Gradle build.

donat avatar Aug 22 '18 14:08 donat

@spoenemann Our workaround is this:

eclipse {
    project {
        file { beforeMerged { gp ->
            setEclipsePreference(file('.settings/org.eclipse.core.resources.prefs'), 'encoding/<project>', FILE_ENCODING)
        }}
    }
}

project.ext.setProperty = { File file, String key, String value ->
    def s = file.text.replaceAll("(?ms)(^\\Q${key}\\E\\s*=).*?\$", '')
    s = s.replaceAll('\n{2,}', '\n')
    file.text = s + key + '=' + value + '\n'
}

project.ext.setEclipsePreference = { File prefsFile, String key, String value ->
    println("edit ${prefsFile}:\t${key} := ${value}")
    if (!prefsFile.exists()) {
        prefsFile.parentFile.mkdirs()
        prefsFile.write("eclipse.preferences.version=1\n")
    }
    setProperty(prefsFile, key, value)
}

FILE_ENCODING is a property that we set in gradle.properties in order to set compileJava.options.encoding = project.FILE_ENCODING etc.

Unfortunately, the setting is not in effect by only a Buildship project import. It seems you need to also run the gradle eclipse task and then refresh the project in Eclipse.

mkq avatar Aug 27 '18 13:08 mkq

Any news on this?

FrankS77 avatar Apr 03 '19 09:04 FrankS77

+1

laflaneuse avatar Jul 09 '19 14:07 laflaneuse

+1

UdoW avatar Jul 31 '19 08:07 UdoW

I have (sort of a) workaround. I've implemented a sample Buildship extension that reads the encoding from the Java compile options and sets it the corresponding Eclipse workspace project. It's not production-ready (no test coverage, etc.), but it should do the job. Please try it out and give me feedback. If it works fine, I might be able to merge it into the Buildship core.

donat avatar Dec 19 '19 17:12 donat

Is this fix part of the next release?

UdoW avatar Apr 06 '20 12:04 UdoW

The sample extension did not work for me

mjameson-se avatar Jun 09 '20 17:06 mjameson-se

I'm the original reporter of this feature request in Eclipse Bugzilla back in March 2016. I still think this is a really important feature as soon as you work in a mixed-OS environment and/or you don't use a default text file encoding. This feature was present in the old Gradle-Eclipse plugin from SpringSource and it's surprising that it's not yet in Buildship, where a recurring motto is that the build script should be "the truth".

mauromol avatar Jun 22 '20 13:06 mauromol

Since Eclipse 2022-06, Eclipse warns if the encoding of the project is not set. Is there a plan for support by Buildship?

Warn when a project does not have the encoding set https://bugs.eclipse.org/bugs/show_bug.cgi?id=479451

cypher256 avatar May 19 '22 05:05 cypher256

From @onmishkin

The just-released Eclipse 2022-06 (4.24) has some changes WRT project-level info about what character encoding is used for files in the project. See [this 4.24 New & Noteworthy section](https://www.eclipse.org/eclipse/news/4.24/platform.php#no-explicit-encoding-project-warning) about the change.

The projects created by Buildship apparently do not set the project's encoding info. The result is that Eclipse reports a warning like this:

    Project 'xxx' has no explicit encoding set

for every imported project. There doesn't seem to be a way in Eclipse to suppress this warning. Buildship should do something to make Eclipse not warn.

donat avatar Jun 16 '22 06:06 donat

Since this is a requirement now, the feature should be prioritized.

donat avatar Jun 16 '22 06:06 donat

Good news :-)

mauromol avatar Jun 16 '22 07:06 mauromol