Spring AppContext in Maven - spring

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.

Related

extract all jar dependency from maven based project and add them to tomcat lib

I have a maven based project and my war file would be bigger than 100M. because of firewalls and some network policies in our company, it takes hours to transfer my new war file to our server.
I want to exclude all jar dependencies and locate them into Tomcat lib. How can I handle it in maven? and how can I add my dependencies to tomcat classpath? thank you
You can utilize the maven dependency plugin in order to collect the dependencies in a folder: https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-project-dependencies.html
If you place the libraries in the lib folder of the tomcats they are available for the tomcat common/shared classloader. If you want to put them in a subfolder to avoid mixing up with already present jars used by tomcat you can add that in catalina.properties.
If you have to deplyoy several webapps on the tomcat with possibly different versions of the same dependency you could run in very ugly problems because the loading of the libs by the common classloader is more or less "random".

Maven simple archetype for ejb (without web app and servlets)

I am looking for a simple archetype for EJB with working example.
I want build a single EAR file with pure EJBs (Servlet and WebApp are not required)
I have used this maven-archetype-j2ee-simple archetype and deleted servlets and projects primary-source and src folders.
I am able to build, but I am not able to deploy in to GlassFish, when I run the command mvn glassfish:deploy it is always looking for .war file inside the target folder.

mvn jboss:deploy fails with NoClassDefFoundError

When deploying an EJB project to JBoss using JBoss Maven Plugin, I get a NoClassDefFoundError on my JBoss' console about a class that is in one of the dependencies of this EJB project.
This dependency is declared with a Compile scope. Is there another scope that I should use so that my dependencies are also deployed to JBoss? Or how should I solve this?
The error looks like this:
DEPLOYMENTS IN ERROR:
Deployment "vfszip:/Users/hordine/projects/SoftBudget/soft-budget-ejb/target/soft-budget-ejb.jar/" is in error due to the following reason(s): java.lang.NoClassDefFoundError: br/com/pedra/j2eepatterns/facade/IEntityService
I understand that you have packaged the EJB as a jar file. This means there is no any provided dependency as same as a stand-alone jar.
AFAIU, there may be 3 possible ways as the following: -
Package them as an ear and put those dependencies inside the ear/lib. Please see Maven EAR Plugin for further information.
Package them as an war and put those dependencies inside the WEB-INF/lib. Please see Maven WAR Plugin for further information.
Package them as an jar and put those dependencies inside the classpath or application server lib. Please refer to your application server document.
EDITED:
If you are using the JavaEE 6, then it is possible to package the EJB as a war file. The The Java EE 6 Tutorial: Packaging Enterprise Beans in WAR Modules told us as
Enterprise beans often provide the business logic of a web application. In these cases, packaging the enterprise bean within the web application’s WAR module simplifies deployment and application organization. Enterprise beans may be packaged within a WAR module as Java programming language class files or within a JAR file that is bundled within the WAR module.
To include enterprise bean class files in a WAR module, the class files should be in the WEB-INF/classes directory.
To include a JAR file that contains enterprise beans in a WAR module, add the JAR to the WEB-INF/lib directory of the WAR module.
WAR modules that contain enterprise beans do not require an ejb-jar.xml deployment descriptor. If the application uses ejb-jar.xml, it must be located in the WAR module’s WEB-INF directory.
I hope this may help.

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.

Resources