Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text resource from classpath #8472

Open
Vampire opened this issue Feb 11, 2019 · 8 comments
Open

text resource from classpath #8472

Vampire opened this issue Feb 11, 2019 · 8 comments
Labels
a:feature A new functionality in:resource-handler resources files

Comments

@Vampire
Copy link
Contributor

Vampire commented Feb 11, 2019

I didn't find a way to do it, besides having a separate non-transitive configuration with one dependency and then use resources.text.fromArchiveEntry(configurations.myConfiguration, 'my-file.txt').

It would be nice if you could do something like resources.text.fromClasspath('my-file.txt') or resources.text.fromUri('classpath:my-file.txt') or similar.

@big-guy
Copy link
Member

big-guy commented Feb 12, 2019

Are you trying to get it from the classpath of the plugin on the buildscript?

Have you tried something like:

def resourceUri = YourPluginClass.class.classLoader.getResource("path/to/resource")
def resource = resources.text.fromUri(resourceUri)

@Vampire
Copy link
Contributor Author

Vampire commented Feb 12, 2019

Ah, hm, somehow didn't think about fromUri, thanks.
After adding spotbugs to the classpath, I can successfully find the file on the classpath, unfortunately I cannot use it, as there seems to be an encoding problem.
This again is about the SpotBugs HTML stylesheets.
Well, like always with encoding problems it can be hairy.
The question probably only or at least firstly is, whether it is like expected or should work differently.
The fancy.xsl file is UTF-8 with BOM encoded.

Here some findings as run on Windows,
BOM means it prints the BOM as if it was read with ISO-8859-1 as the famous three characters sequence
? means it prints a literal question mark character
OK means it prints the line starting with <?xml like needed

configurations { spotbugsStylesheets { transitive false } }
dependencies { spotbugsStylesheets 'com.github.spotbugs:spotbugs:3.1.11' }

/* BOM */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl').asReader().readLine()
/* ?   */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl', 'UTF-8').asReader().readLine()
/* ?   */ /* ?  */ println resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asReader().readLine()
/* OK  */ /* OK */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl').asFile().readLines().first()
/* ?   */ /* OK */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl', 'UTF-8').asFile().readLines().first()
/* ?   */ /* OK */ println resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asFile().readLines().first()
/* BOM */ /* OK */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl').asFile('UTF-8').readLines().first()
/* OK  */ /* OK */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl', 'UTF-8').asFile('UTF-8').readLines().first()
/* OK  */ /* OK */ println resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asFile('UTF-8').readLines().first()
/* ?   */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl').asFile().readLines('UTF-8').first()
/* ?   */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl', 'UTF-8').asFile().readLines('UTF-8').first()
/* ?   */ /* ?  */ println resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asFile().readLines('UTF-8').first()
/* BOM */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl').asFile('UTF-8').readLines('UTF-8').first()
/* ?   */ /* ?  */ println resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy.xsl', 'UTF-8').asFile('UTF-8').readLines('UTF-8').first()
/* ?   */ /* ?  */ println resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asFile('UTF-8').readLines('UTF-8').first()

The main point here is, if not specifying any encodings, it just works with fromArchiveEntry, but with fromUri I have to specify UTF-8 on the asFile call to make it work properly.
Also, why is there no variant of fromUri that accepts an encoding like there is for fromArchiveEntry and fromFile?

@Vampire
Copy link
Contributor Author

Vampire commented Feb 12, 2019

Actually I also wonder that there is no variant of org.gradle.api.resources.TextResource#asReader that accepts an encoding like for org.gradle.api.resources.TextResource#asFile.
And I also added a second comment column above with the results when setting -Dfile.encoding=UTF-8

@Vampire
Copy link
Contributor Author

Vampire commented Feb 13, 2019

The encoding problem also is especially problematic if you do not use it yourself but give it to some task.
For example if you have

tasks.withType(SpotBugsTask) {
    reports {
        xml.enabled false
        html {
            enabled true
            stylesheet resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl'))
        }
    }
}

the transformation will not work due to the encoding problem.
You would have to do it like

tasks.withType(SpotBugsTask) {
    reports {
        xml.enabled false
        html {
            enabled true
            stylesheet resources.text.fromFile(resources.text.fromUri(getClass().classLoader.getResource('fancy.xsl')).asFile('UTF-8'))
        }
    }
}

to make it work.

@grv87
Copy link
Contributor

grv87 commented Mar 24, 2019

I've also hit encoding problem in TextResource and code quality plugins.
I think that TextResource should preserve encoding on creation and use it in asFile().
Otherwise, all config files not in system default encoding become corrupted.
This is especially a problem with XMLs since they declare encoding explicitly.

Should I open another issue specifically about encoding?

@stale
Copy link

stale bot commented Aug 5, 2020

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. If you're interested in how we try to keep the backlog in a healthy state, please read our blog post on how we refine our backlog. If you feel this is something you could contribute, please have a look at our Contributor Guide. Thank you for your contribution.

@stale stale bot added the stale label Aug 5, 2020
@Vampire
Copy link
Contributor Author

Vampire commented Aug 5, 2020

still an issue with version 6.5.1

@stale stale bot removed the stale label Aug 5, 2020
@jjohannes jjohannes added the in:resource-handler resources files label Apr 14, 2021
@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. If you're interested in how we try to keep the backlog in a healthy state, please read our blog post on how we refine our backlog. If you feel this is something you could contribute, please have a look at our Contributor Guide. Thank you for your contribution.

@stale stale bot added the stale label Apr 16, 2022
@stale stale bot removed the stale label May 5, 2022
@ov7a ov7a added the a:feature A new functionality label Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:feature A new functionality in:resource-handler resources files
Projects
None yet
Development

No branches or pull requests

5 participants