0

I've run into some problems when practicing with Maven in IntelliJ and somehow the resource files in my class path never gets included in the built JAR.

My module structure:

src/main/java/com/abc/Main.class                
src/main/java/com/abc/Messages.class            
src/main/java/com/abc/messages_en.properties    the not included resource files.
src/main/java/com/abc/messages_es.properties    
src/main/resources/                             misc resources

Project builds successfully, but when I run the JAR file, I would receive a error message as below. And when I unpacked the JAR file, the messages_xx.properties files are missing.

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name com.abc.messages, locale en
        at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
        at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
        at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
        at org.jis.Messages.<init>(Messages.java:42)
        at org.jis.Main.main(Main.java:205)

My pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.6</version>
      <configuration>
        <includes>
          <include>${basedir}/src/main/java/com/abc/*.properties</include>
        </includes>
        <archive>
          <manifest>
            <mainClass>com.abc.Main</mainClass>
            <addClasspath>true</addClasspath>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

Came across many similar threads on SO but the solutions don't seem to fit my case. Any suggestions what's happening here? I've been trying to figure out why this happened for a while now but haven't found an answer yet. It would be great if someone could give me some hints. Thanks a lot.

Known Zeta
  • 101
  • 1

1 Answers1

0

Maven expects to find

src/main/java/com/abc/messages_en.properties
src/main/java/com/abc/messages_es.properties    

in

src/main/resources/com/abc/messages_en.properties
src/main/resources/com/abc/messages_es.properties    

Resource files in src/main/java will not finish up in the resulting jar file because

  1. the maven-compiler-plugin compiles the java sources and outputs them to target/classes

  2. the maven-resources-plugin copies (and perhaps filters) files (including their directory structure) from 'src/main/resourcesintotarget/classes`

  3. the maven-jar-plugin then builds the jar from the content of target/classes

So just move your properties files and stop wrestling with the maven way of doing things.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • Thanks! Is there really no (easy) way around except placing properties files in src/main/resources and change the code? Not that I'm not willing to change the code, just would love to know... – Known Zeta Apr 28 '17 at 03:21
  • Code changes are not required as long as you maintain the com/abc package structure in the resources directory – Steve C Apr 28 '17 at 03:23