# Accessing message bundles in buildsrc

**URL:** https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030
**Category:** Help/Discuss
**Created:** [June 20, 2017, 10:18pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030 "2017-06-20T22:18:37Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [June 20, 2017, 10:18pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/1 "2017-06-20T22:18:38Z")

</div>

Back in 2013 I asked a question (now on old forum) about how to access message bundles in gradle scripts.

> [@How can I get localized property values in a gradle build file?](https://discuss.gradle.org/t/how-can-i-get-localized-property-values-in-a-gradle-build-file/4705):
>
> I want to generate some localized html files as part of my build and deployment. As a simple test, I tried this task task i18n(description:“i18n test”) \<\< { def locales = [Locale.ENGLISH, Locale.JAPANESE, Locale.GERMAN] def keys = [“s1”, “s2”, “s3”] [locales, keys].combinations().each{ locale, key -\> def labels = ResourceBundle.getBundle(‘html’, locale) println “Locale = ${locale.toString()}, key = $key, value = ${labels.getString(key)}” } } But it says Can’t find bundle for base nam…

The answer, to put them in buildSrc/src/main/resources worked great for many years. However, in recent versions, somewhere around 2.10 to 2.14 something changed, and it no longer works. It also does not work in version 3.x or 4.0 of gradle. I don’t know what changed, but I would very much like to know how to get it working again with more recent versions of gradle.

The error I get is  
“Can’t find bundle for base name html, locale en”

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [June 21, 2017, 10:30am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/2 "2017-06-21T10:30:32Z")

</div>

Perhaps the classloading hierarchy has changed and the plugin is no longer loaded by the same classloader as the rest of the script. In which case you’ll need to use `ResourceBundle.getBundle(String, Locale, Classloader)` instead.

See [here](https://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html#getBundle(java.lang.String,%20java.util.Locale))

> Gets a resource bundle using the specified base name and locale, and the caller’s class loader. Calling this method is equivalent to calling
> 
> getBundle(baseName, locale, this.getClass().getClassLoader()),

Eg:

`ResourceBundle.getBundle('html', locale, MyPlugin.classLoader)`

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [June 21, 2017, 10:51am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/3 "2017-06-21T10:51:24Z")

</div>

Thanks, but I’m not sure what plugin you are referring to. I do not have  
anything in the buildSrc directory except src/main/resources/\*.properties.

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [June 21, 2017, 12:10pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/4 "2017-06-21T12:10:52Z")

</div>

Perhaps you could fabricate a `UrlClassloader`

```gradle
URL[] urls = buildscript.configurations.classpath.files.collect { it.toURI().toURL() } 
Classloader cl = new java.net.URLClassloader(urls, null)
def bundle = ResourceBundle.getBundle('html', locale, cl)

```

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 8, 2017, 3:18pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/5 "2017-07-08T15:18:34Z")

</div>

Unfortunately that did not work. I still get the same error. Here is the exact code I tried:

> URL urls = buildscript.configurations.classpath.files.collect { it.toURI().toURL() }  
> ClassLoader classLoader = new URLClassLoader(urls, (ClassLoader) null)  
> def labels = ResourceBundle.getBundle(‘html’, Locale.ENGLISH, classLoader)

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 8, 2017, 3:24pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/6 "2017-07-08T15:24:30Z")

</div>

Can you add `buildSrc/src/main/java/DummyClass.java` then do

```gradle
Classloader classLoader = DummyClass.classloader
def labels = ResourceBundle.getBundle('html', Locale.ENGLISH, classLoader)

```

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 8, 2017, 3:59pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/7 "2017-07-08T15:59:36Z")

</div>

That did not work either, but I did notice something interesting.  
I can access the resource bundle from the build.gradle in the root project.  
In other words, ResourceBundle.getBundle(‘html’, Locale.ENGLISH) successfully retrieves the resource bundle from buildSrc when called in the root project, but not when called from a build.gradle in a subproject. Why is that? All the documentation says that there can only be a single buildSrc directory at the top level and all subprojects should be able to access it. Furthermore, it used to work fine (before version 2.10 or so).

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 8, 2017, 4:29pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/8 "2017-07-08T16:29:14Z")

</div>

Please post your `settings.gradle` and tell us which subproject can’t ‘see’ `buildSrc`

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 8, 2017, 7:52pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/9 "2017-07-08T19:52:14Z")

</div>

The contents of my settings.gradle file (in root project) is

> include ‘imageproc’, ‘apps’, ‘webdeployment’

It was the webdeployment projects build.gradle file that was nuable to get the resource bundle.  
Since I can get the resource bundle in the root project, I am unblocked, but it would be nice to understand why the subproject cannot get the bundle.

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 9, 2017, 6:07am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/10 "2017-07-09T06:07:34Z")

</div>

This is strange, all projects in a multi-module build should have `buildSrc` on the buildscript classpath

Where is your code? In a `Task.doLast { ... }`? Or somewhere else? (I know there’s some strangeness with the `buildscript { ... }` classloader)

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 9, 2017, 11:12am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/11 "2017-07-09T11:12:29Z")

</div>

The full project is on github if you want to look closer:  
[https://github.com/bb4/applets](https://github.com/bb4/applets)

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 9, 2017, 11:29am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/12 "2017-07-09T11:29:41Z")

</div>

You have a gradle wrapper in the root folder but then you also have a gradle wrapper in the `webdeployment` folder. How are you executing `gradlew`? From root folder (correct) or from webdeployment folder? (may have issues)

Try `./gradlew :webdeployment:build`  
from the root folder

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 9, 2017, 11:32am UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/13 "2017-07-09T11:32:21Z")

</div>

I think I do not need it in the webdeployment folder. I am going to try removing it there. I had originally added it there because I thought I needed an older version of gradle to run there in order to get the localeBundle. I would like to run from the root, but running in webdeployment should work too.

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 9, 2017, 2:26pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/14 "2017-07-09T14:26:10Z")

</div>

I created a minimal-locale-test branch in [https://github.com/bb4/applets](https://github.com/bb4/applets) hoping to create a simple reproducible case, however, it then worked as expected. So when I removed all the subprojects, it was able to read the message bundle from buildSrc, but with the subprojects present it could not. Maybe I can add things back gradually and see where it stops working.

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 9, 2017, 2:34pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/15 "2017-07-09T14:34:01Z")

</div>

I suspect it’s the extra wrapper in the `webdeployment` directory. When you run `gradlew` from that directory gradle thinks it’s the root (ie not a multi-module build)

Have you tried `./gradlew :webdeployment:build` from the root directory?

---

<div class="post-metadata">

### Author: ![Barry\_Becker1](https://avatars.discourse-cdn.com/v4/letter/b/87869e/32.png) [@Barry\_Becker1](https://discuss.gradle.org/u/Barry_Becker1)
#### Post date: [July 9, 2017, 3:07pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/16 "2017-07-09T15:07:13Z")

</div>

I removed the gradle wrapper from the webdeployment directory.  
I made a change that fixed the problem.  
Moving this line  
ext.depVersion = getDependentVersion()  
from the top of /webdeployment/build.grade to the top of /build.gradle  
seemed to allow it to work.  
The working version is now on the master branch.

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [July 9, 2017, 4:11pm UTC](https://discuss.gradle.org/t/accessing-message-bundles-in-buildsrc/23030/17 "2017-07-09T16:11:30Z")

</div>

You didn’t fix the problem, you moved the code from a place where it wasn’t working to a place where it is working. It’s still unexplained why the `buildSrc` isn’t on the subproject classpath
