Resources directory not found on classpath - java

I'm using Intellij 13, and I've marked a directory as a resources directory (thinking that this would make all sub-files/folders available on the classpath), however when I run the project, an exception is thrown saying that a file called database.properties (inside the resources folder) can't be found at runtime.
Any ideas?
'database' folder is the folder marked as a resources folder.

I do not know intellij-idea, but I know that maven can deal with multiple resources directory provided they are declared in pom.xml. From Maven doc on Mave Resources plugin - Specifying resources directories :
...
<resources>
<resource>
<directory>resource1</directory>
</resource>
<resource>
<directory>resource2</directory>
</resource>
<resource>
<directory>resource3</directory>
</resource>
</resources>
...
If IntelliJ-IDEA has not declared your database directory such way in the pom, maven has no chance to find it.

Related

Maven resource filtering of json file

In my current project I have a submodule which is using the maven exec plugin to run a test service which pulls configuration files from a location outside of the resources/testResources folders.
I need to use filtering to inject an environment variable into a few of the configuration files. This is working for one of the files, a .properties file, but not for another file which is a .json. In the latter case it simply leaves the variable in the json file. The two files are right next to each other in the filtered directory.
Here is the filtering snippet from the submodule:
<build>
<finalName>${artifactId}</finalName>
<testResources>
<testResource>
<filtering>true</filtering>
<directory>../etc</directory>
</testResource>
</testResources>
json file:
{ "customMappings" :
{ "tag:example.com:/vagrant/" : "file:${VAGRANT_CWD}" }
}
Abbreviated project structure:
project
etc
config.properties
config.json
submodule
pom.xml
The submodule is definitely loading both files, but only filtering the .properties file.
Is there something special about it being a json file that would prevent filtering from happening to it? Anything that can be done about this?
For what its worth, I did eventually get this to work. I found that I had to directly list the file for inclusion in order to get it to be processed (its been a long time so hopefully this is the correct solution):
<build>
<finalName>${artifactId}</finalName>
<testResources>
<testResource>
<filtering>true</filtering>
<directory>../etc</directory>
<includes>
<include>config.json</include>
<include>config.properties</include>
</includes>
</testResource>
</testResources>
...

Maven resource filter not working [duplicate]

This question already has an answer here:
Maven Resource Filtering with Spring Boot: Could not resolve placeholder
(1 answer)
Closed 6 years ago.
I'm trying to make use of Maven's (v3) resource filtering capabilities to replace some variables in my application.properties file, with a property in my pom.xml.
The properties file resides in src/main/resources and contains:
application.version=${project.version}
My pom file contains:
<project ...>
...
<version>1.0.0</version>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
</build>
When I run goal (via eclipse run config) clean process-resources package and take a look at the properties file in target/classes, nothing has changed... the value remains the same.
Found the solution. Since I am using the spring-boot-starter-parent, there is a different required syntax for the property variables. Instead of ${...}, you must used #...#. Here is the reference that I used: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3-Release-Notes#maven-resources-filtering

Internationalization Maven application

Actually, I've created two files (MessageBundle_fr_BE and MessageBundle_us_US) where I put the different text to translate to internationalize my application
My problem is when I launch the application, I've following error :
Problem accessing /MyServlet. Reason:
Can't find bundle for base name MessageBundle, locale fr_FR
Caused by:
java.util.MissingResourceException: Can't find bundle for base name MessageBundle, locale fr_FR
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:796)
at com.application.myGoogleAppEngine.Internationale.<init>(Internationale.java:13)
at com.application.myGoogleAppEngine.Internationale.getInstance(Internationale.java:18)
at com.application.myGoogleAppEngine.MyServlet.doPost(MyServlet.java:52)
at com.application.myGoogleAppEngine.MyServlet.doGet(MyServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
The project structure is below :
- src
- main
- webapp
- images
- js
- stylesheets
- resources
MessageBundle_fr_FR.properties
MessageBundle_us_US.properties
- WEB-INF
- appengine-web.xml
- logging.properties
- web.xml
- index.jsp
- java
- com.application.myGoogleApp
- test
- target
- pom.xml
- nbActions.xml
- README.md
And a part of my pom.xml to include 'src/main/webapp/resources' directory:
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
<resource>
<directory>${basedir}/src/main/webapp/resources</directory>
</resource>
</webResources>
</configuration>
Can you help me ?
Thank you
ResourceBundle.getBundle will only look in the classpath. It has no knowledge of .war files or servlets. You must place the .properties files in WEB-INF/classes.
I can see that you are using maven archetypes. So, I suggest to have template below:
src/main/java
src/main/resources
-MessageBundle_fr_FR.properties
-MessageBundle_us_US.properties
src/main/webapp
You must create src/main/resources and move your properties to this folder.

FileNotFoundException for bean definition file happens only online, not on localhost

When I published a war file for an application that works locally with Eclipse WTP, I had a FileNotFoundException for the bean.xml file with my beans definitions.
SEVERE: Exception sending context initialized event to listener instance of
class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource
[META-INF/spring/beans.xml]; nested exception is java.
io.FileNotFoundException: class path resource [META-INF/spring/beans.xml]
cannot be opened because it does not exist
at Caused by: java.io.FileNotFoundException: class path resource
[META-INF/spring/beans.xml] cannot be opened because it does not exist
...
I created the war file with mvn war:war and copied in the webapps folder of Tomcat 7.
beans.xml is located in src/main/resources/META-INF/spring/beans.xml and I've the following in my pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
</resource>
</webResources>
</configuration>
</plugin>
In the war file beans.xml gets packaged in META-INF/spring/beans.xml
In my web.xml I've:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/beans.xml</param-value>
</context-param>
However the file is not found. How to solve the problem?
UPDATE: as Matthew Farwell suggested, is bean.xml is not packaged in the right location, so it's not in the class path, I think it's specified with maven-war-plugin parameters, now I try to look at its documentation. If someone knows it would be helpful.
UPDATE 2: As explained in maven-war-plugin documentation, there is an optional parameter called targetPath. I tried and after changing maven-war-plugin configuration adding targetPath it gets packaged correctly.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
UPDATE 3: About Ryan Stewart's suggestion, I started my initial pom setup using roo, but after that I've done many changes and I'm not using roo any more. The directory src/main/resources is not mentioned in any other places in pom.xml (I've used grep), however the only setting that looks suspicious to me is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration><encoding>UTF-8</encoding></configuration>
</plugin>
I've commented out that plugin, but nothing changed, then I commented out the configuration part of maven-war-plugin, but src/main/resources was not added to the war anymore, so for now I've added it back and I'm uploading it to test it online (it's still a staging server actually, not the final one anyway).
UPDATE 4 Ryan Stewart suggested that the problem was that I was running "mvn war:war" instead of "mvn package", and that was indeed the problem. With my targetPath, the resources appeared in WEB-INF/classes, but there weren't any classes there.
I was fighting an uphill battle, while instead the simpler solution was to remove the configuration part as in update 3, and use "mvn package" to build the war file. Thank you to both of you Ryan and Matthew, not only I solved my problem, but I've also learnt something more about Maven.
I have to assume you have another part of the POM that's excluding the file in question from being processed as a classpath resource, else it should be working. Either
Stop doing that, and it'll work fine--the content of src/main/resources becomes classpath resources by default--or
remove the classpath: from your path. Without that prefix, the path given in contextConfigLocation will be resolved against the root of the WAR file, and it will correctly find your file in META-INF/spring.
If you take path 1, then you should remove the webResources section, or you'll end up with the file in two places--not problematic, but potentially confusing.
In a war, / is not part of the classpath for a webapp. The classpath includes /WEB-INF/classes and all of the jars in /lib. See Apache Tomcat 6.0 - Class Loader HOW-TO
WebappX — A class loader is created for each web application that is
deployed in a single Tomcat instance. All unpacked classes and
resources in the /WEB-INF/classes directory of your web application,
plus classes and resources in JAR files under the /WEB-INF/lib
directory of your web application, are made visible to this web
application, but not to other ones.
The other web servers will have similar rules. If you wish to reference something as part of the classpath, put it in WEB-INF/classes.

Building the WAR with m2eclipse in combination with WTP (handling webResources)

I have a situation where I have a web application that is built using maven (i.e., maven-war-plugin). For each code modification, we have had to manually launch maven and restart the application server. Now, to reduce build cycle overhead, I want to use WTP to publish the webapp.
Now, we have resource processing with Maven, and there are some additional Maven tasks defined in our POM when building the webapp. Therefore m2eclipse seems like a natural solution.
I have gotten far enough that the Maven builder is running these tasks and filtering resources correctly. However, when I choose "Run on Server", the WAR file does not look like it would if I built it in Maven.
I am guessing that this is because WTP actually builds the WAR, and not the m2eclipse builder. So even though we have configured the maven-war-plugin in our POM, those settings are not used.
Below is a snippet with our maven-war-plugin configuration. What is configured under "webResources" is not picked up, it appears:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-2</version>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<workDirectory>${project.build.directory}/work</workDirectory>
<webappDirectory>${project.build.webappDirectory}</webappDirectory>
<cacheFile>${project.build.webappDirectory}/webapp-cache.xml</cacheFile>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pdf</nonFilteredFileExtension>
<nonFilteredFileExtension>png</nonFilteredFileExtension>
<nonFilteredFileExtension>gif</nonFilteredFileExtension>
<nonFilteredFileExtension>jsp</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<webResources>
<!-- Add generated WSDL:s and XSD:s for the web service api. -->
<resource>
<directory>${project.build.directory}/jaxws/wsgen/wsdl</directory>
<targetPath>WEB-INF/wsdl</targetPath>
<filtering>false</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
Do I need to reconfigure these resources to be handled elsewhere, or is there a better solution?
To fill in an answer to my own question if someone else comes across the same problem, I ended up adding the following to my webapp project:
<resource>
<directory>${project.build.directory}/jaxws/wsgen/wsdl</directory>
<filtering>true</filtering>
<targetPath>${project.basedir}/src/main/webapp/WEB-INF/wsdl</targetPath>
<includes>
<include>**/*</include>
</includes>
</resource>
(inside the resources element under build).
It works fine since my WSDL files are generated in the generate-resources phase and places them in target/jaxws/wsgen/wsdl. Then those are moved into src/main/webapp/WEB-INF/wsdl, where the WTP builder picks them up when building the WAR file.
Note: I should mention that I get some problems with the eclipse plugin for Maven now (i.e., mvn eclipse:eclipse), because apparently you are not allowed to have absolute paths in targetPath. Not found a satisfactory workaround yet...
I'm not sure (filtered) web resources are supported yet, see MNGECLIPSE-1149. The issue has a patch (and a workaround) that could work for you. Also have a look at the hack from this thread.
WebResources are supported in m2e-wtp 0.12 and later versions (compatible with Eclipse Helios and Indigo).
For more details, see http://community.jboss.org/en/tools/blog/2011/05/03/m2eclipse-wtp-0120-new-noteworthy

Categories