18

Nothing I've found has been able to help me solve this one specific case. I recently switched from a plain old java web app project (which was working) to a maven web project. I get the following runtime exception:

java.util.MissingResourceException: Can't find bundle for base name com.myapp.config, locale en

I am using Netbeans to create a JSF 2.0, Spring, and Hibernate web app. I have the following directory structure:

src\main\java\com\myapp     Contains config.properties
src\main\resources               Empty

target\myapp\WEB-INF\classes\com\myapp     Contains compiled class files without config.properties
src\main\java\com\myapp                                 Contains config.properties

Inspection of the WAR file in the target folder does not show any sign of the properties file so it's as if the Maven build plug-in is not copying over properties files. I know there is a tag you can place inside the pom but it didn't work for me. The link below mentions that the resources folder (empty for me) has its contents included during the build but if that is the case, how do you do it from Netbeans? I just want the properties file to be packaged with my war so it is accessible when it is deployed to the server.

http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<repositories>
    <repository>
        <id>java.net</id>
        <name>Repository hosting the Java EE 6 artifacts</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-hibernate3</artifactId>
        <version>2.0.8</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.1.8</version>
    </dependency>
    <dependency>
        <groupId>net.authorize</groupId>
        <artifactId>java-anet-sdk</artifactId>
        <version>1.4.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.15</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${artifactId}</finalName>
</build>
<profiles>
    <profile>
        <id>endorsed</id>
        <activation>
            <property>
                <name>sun.boot.class.path</name>
            </property>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <!-- javaee6 contains upgrades of APIs contained within the JDK itself.
                             As such these need to be placed on the bootclasspath, rather than classpath of the
                             compiler.
                             If you don't make use of these new updated API, you can delete the profile.
                             On non-SUN jdk, you will need to create a similar profile for your jdk, with the similar property as sun.boot.class.path in Sun's JDK.-->
                        <compilerArguments>
                            <bootclasspath>${settings.localRepository}/javax/javaee-endorsed-api/6.0/javaee-endorsed-api-6.0.jar${path.separator}${sun.boot.class.path}</bootclasspath>
                        </compilerArguments>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>javax</groupId>
                            <artifactId>javaee-endorsed-api</artifactId>
                            <version>6.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
<properties>
    <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
</properties>

vkraemer
  • 9,864
  • 2
  • 30
  • 44
Nicholas22j
  • 183
  • 1
  • 1
  • 5

5 Answers5

39

Maven doesn't copy resources from the java source tree by default, but you can get it do that by adding this to your pom.xml:

<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <excludes><exclude>**/*.java</exclude></excludes>
    </resource>
  </resources>
</build>

Make sure you exclude the java source files.

From https://rogerkeays.com/how-to-change-mavens-default-resource-folder

Roger Keays
  • 3,117
  • 1
  • 31
  • 23
14

What is your project's build path configured to be in Netbeans? You might try changing it to src/main/webapp/WEB-INF/classes. This way class files compiled from your src/main/java folder and any resources you have under src/main/resources should get included in the generated WAR. You would then be able to access your config.properties file if you place it under the src/main/resources folder.

You might also review any includes sections in your pom.xml and ensure you're not accidentally excluding something (if you explicitly include some things, you're likely implicitly excluding everything else).

Dave
  • 797
  • 6
  • 10
  • I attached the pom.xml above. The classpath tag might be what I need to edit. As mentioned above, I don't want to place stuff in the resources folder since it won't show up in netbeans if I move it there. – Nicholas22j Apr 03 '11 at 03:03
  • Netbeans should be able to handle having the properties files in the resources directory. See http://netbeans.org/community/magazine/html/04/maven.html, about halfway down where it talks about resources (or just search the page for resources and read what it has to say about it). – squawknull Apr 03 '11 at 03:15
  • 2
    I think that netbeans article pointed me in the right direction. There is a folder called "Other Sources" in the Netbeans project tab. You need to add non-class files (like .properties files) to "Other Sources" > "src/main/resources" under the same desired package structure. I knew it had to be something simple! I can't tell you how much time I spent on this. Thank you so much @squawknull! You rock. – Nicholas22j Apr 03 '11 at 13:31
  • I had a similar problem and this didn't solved. I had to add the package name at the beginning of the bundle name as ResourceBundle.getBundle(packageName+"mybundle") in order to work – Christian Vielma Aug 06 '12 at 20:35
1

By default maven will include all files under resources folder. If your properties files are not in the resource folder, then you need to include the following in the pom.xml file under the build section.

<build>
/* other tags like <plugins> goes here */
<sourceDirectory>src/main/java</sourceDirectory>  
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
/* other tags like <plugins> goes here */
</build>
ABODE
  • 958
  • 2
  • 15
  • 13
0

Try putting your config.properties under src\main\resources\com\myapp. I was able to test this on a local project. I'm running Maven 3.0.2.

Created a mvn sample project with the webapp archetype:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp

Created a directory at src/main/resources/com/foo and put a foo.properties file under it.

Ran a build:

mvn clean install

Then, when looking in the resulting target directory, the foo.properties file appears:

ls -al target/my-webapp/WEB-INF/classes/com/foo/
-rw-r--r--  1 sblaes  staff    4 Apr  2 22:09 foo.properties

You might try those steps on your machine. If that works, then start trying to simplify your POM above by removing things from it to see if it starts working. Trial and error is no fun, but I just don't see anything above that should be breaking it.

squawknull
  • 5,131
  • 2
  • 17
  • 27
  • I tried this and still gives same exception. It is interesting to note src\main\webapp is what's being copied over to the target dir for inplace deployment. The webapp folder only contains facelet pages and the xml config files under web-inf. No properties file is there. Even if copying config.properties to the resources folder did work, I am no longer able to view it in the Netbeans project. Any other ideas? – Nicholas22j Apr 03 '11 at 02:23
  • Hrmm, I just tried this locally and the properties file got copied over... Can you add the contents of your pom.xml to your question above? Can you try running a build outside of Netbeans using "mvn clean install" and see if it shows up in the WEB-INF directory inside the war file that gets created? – squawknull Apr 03 '11 at 02:36
  • I ran it manually at the command line and it produced the same results as when I'm in the IDE. Build is always successful. It's just when the EL in the view tries to resolve #{config['propertyKey']} and throws the MissingResourceException. The war doesn't have any of the config files. It's strange that when switching to maven, this problem occurs but not as a normal java web project. I switched to maven to avoid dependency nightmares. – Nicholas22j Apr 03 '11 at 02:58
  • Hrmm, I don't see anything wrong with your POM. I can't imagine those other descriptors breaking this... You might try simplifying the POM a bit to see if something else is causing a bug or affecting the build process in a way not intended. I'll paste some info on my sample project above. – squawknull Apr 03 '11 at 03:06
0

Huge gotcha for this:

  • when your resources are in "test/resources" (e.g. .properties files for tests)
  • maven doesn't copy them to target, so they're not in the classpath

Check whether your "packaging" is set to "pom" in the pom.xml:

<packaging>pom</packaging>

Fix is:
change your packaging to "jar" or "war" instead

davidfrancis
  • 3,734
  • 2
  • 24
  • 21