How can I convince com.sun.faces to scan my target/classes directory for #ManagedBeans when using tomcat7:run? - maven

JSF (com.sun.faces 2.1.12) cannot find annotated classes in target/classes when run with the tomcat7 maven plugin (v2.2).
When we debug we run the application using the tomcat7:run plugin with Maven. When the tomcat7 plugin builds the classpath for the webapp as it launches it adds target/classes to the webapp classpath as a classes directory and configures src/main/webapp as the webapp root directory. What this means is that there are no classes in WEB-INF/classes and the com.sun.faces annotation scanner does not scan target/classes for #ManagedBeans.
I tried tricking the annotation scanner into scanning target/classes by adding a META-INF/faces-config.xml to src/main/resources. This almost worked as the MET-INF/faces-config.xml was found in target/classes but com.sun.faces.config.ConfigManager.ProvideMetadataToAnnotationScanTask skipped it because it was not in a jar. I've had difficulty finding the code that scans WEB-INF/classes.
How can I convince com.sun.faces to scan my target/classes directory for #ManagedBeans?

which version of the plugin are you using?
That's normally fixed in 2.2 (see http://tomcat.apache.org/maven-plugin-2.2/tomcat7-maven-plugin/run-mojo.html#jarScanAllDirectories )
If not could you please create an issue with a sample project?
Thanks

Create in your Maven project the file src/main/tomcatconf/context.xml
with the following content:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resources
className="org.apache.naming.resources.VirtualDirContext"
extraResourcePaths="/WEB-INF/classes=target/classes"/>
</Context>
The Tomcat 7 Maven plug-in looks up this file at startup. The implementation
org.apache.naming.resources.VirtualDirContext of the Context interface allows
the mapping of directories to certain resource locations. In this case we map
the directory of the project's classes to the resource /WEB-INF/classes.

Related

Changing default filesystem in WAR file prepared by Gradle plugin

I would like to deploy a mock service developed with SoapUI. I have SoapUI project, web.xml and now I would like to add them to WAR archive. I'm using Gradle to prepare this WAR file.
My code below:
war {
archiveName 'mock.war'
webXml file ('src/main/webapp/web.xml')
}
Default filesystem of produced archive is:
META-INF
WEB-INF
lib
some libs...
classes (this folder I would like to rename to 'soapui')
some files...
web.xml
Is there any possibility, to change the name of 'classes' folder?
Thanks in advance,
Patryk

How to place context.xml in war's META-INF?

I am building a webapp through Gradle's war plugin. In order to disable Tomcat's session persistence, I need to place the file context.xml in the META-INF directory of the war's root.
I attempted the following:
Create the file src/main/webapp/META-INF/context.xml of the main project
Create the file src/main/resources/META-INF/context.xml of the main project
However, when I build the project using gradle clean war, the produced war file contains a META-INF with only a MANIFEST.MF in it. It is as if my directory gets overridden.
How do I place context.xml in the war?
src/main/webapp/META-INF/context.xml is correct and works fine for me. Chances are that you went wrong somewhere, or that there is a problem with your build script.

spring where to store xml config files

I'm developing a spring project and I've just encountered serious problems with loading XML config files within junit (but they are accessible from the web controllers) - can't load XML files.
I listed my classpath (in junit tests) and found that, among the rest, there are 2 directories included:
/var/www/Java/lyricsBase/build/web/WEB-INF/classes/
/var/www/Java/lyricsBase/build/test/classes/
There is just one file I want to include in my test:
/WEB-INF/lyricsBaseApp-servlet.xml
and it imports 3 files below:
/WEB-INF/hibernate.xml
/WEB-INF/dataSource.xml
/WEB-INF/beans.xml
I can clearly see that deploying my project on tomcat copies confg files to /var/www/Java/lyricsBase/build/web/WEB-INF/ABC.xml but this directory is not in the classpath, /var/www/Java/lyricsBase/build/web/WEB-INF/classes is included instead. I read that WEB-INF should not be in the classpath (it's just a Java EE principle). OK - so where should I put my config files to access them easily both from web controllers and junit tests? Any good practices?
Place the config files under WEB-INF/classes on the war package, and they will be loaded into the classpath. Consider deploying your code as an exploded war, so the config files can be changed easily.
Also consider using maven war project for your packaging. If you do, you can just place your config files under src/main/resources under the source code, and they will be packaged into WEB-INF/classes for you.
I had the same problem and from then on started putting them in META-INF under resources. This is where the spring template projects which don't run in Tomcat put them and seemed to solve my issues.

Spring component-scan not scanning jboss server lib directory

I have a jar file (kept in jboss-home/server/default/lib) and a war file (kept in jboss-home/server/default/deploy). The jar file contains a servlet which initializes the spring context. The servlet is initialized from the war file.
The problem is that the #Component (and #Service, etc) annotations in the jar file are not scanned. It gives NoSuchBeanDefinitionException error. I have declared the following in context xml.
<context:component-scan base-package="com.abc.mypack" />
I have also selected "Add directory entries" when building the jar using eclipse.
If I change to xml based configuration, it works. Or, if I move the jar file to WEB-INF/lib of the war file, it works.
Is there a way to scan the jboss server lib directory for components?
I am using spring 3.1 and Jboss AS 6.
Those annotations don't scan libraries. They scan packages, which means that the libraries containing those packages need to be on the classpath.
Most-likely the libraries you're referring to are on different classloaders.
I would suggest packaging the jar within the war's WEB-INF/lib directory.

Spring AppContext in Maven

I am using Maven for creating my project structure. The following is the way I am doing
Generate Archetype
Have the following modules - Ear, War and WarSource (I am deleting the src, ejb and jar folders)
So my EAR will have 2 modules - War and WarSource which inturn have src/main/java and src/main/resource folders
Question is - where should my application context reside so that I avoid the File Not Found error during runtime.
Thanks
If you're using spring mvc it will be in the war src/main/webapp/WEB-INF/ directory. Also if you're using spring you may not even need an ear file, you should read up on that to be sure you're not adding unnecessary complexity.

Resources