How come my spring mvc web app works without pom.xml? - spring

I have removed pom.xml from my eclipse project folder.
And I choose run on server option and everything works fine.
I am new to Spring MVC and have been given this project as an interview round.
Spring version 4.0.6, Tomcat 7, Eclipse Juno
Any help here is appreciated.

pom.xml is the build configuration file for the Maven building tool, Using Maven is optional as you can use any building tool of choice, for example Ant, Maven, Gradle ... etc
The idea behind this build configuration file is that you can define all your dependencies of your project (Spring, Struts, Hibernate, ... etc), source code repository, test cases classes, war file generation info, any other configuration for your project. and If you want to give me your project, you could only give me that pom.xml, and I would simply run maven on this file, and it will download your source code and dependencies, run test cases, build war file and deploy it on application server.
For more information, check the POM reference here

pom.xml is a maven configuration file. You don't need it to run a Spring MVC application.
Having said this, sin maven is what you usually use to build a project, you are not going to be able to build it again if you removed the pom.xml file.

Possible reasons:
You did not reference any Spring MVC class in your Web Appliction
Your Tomcat already has the Spring MVC in its libraries
Spring MVC libraries are copied in your Lib folder of your Web project, so they are carried along within the war (or exploded by the Tomcat plug-in for Eclipse).
As Pablo pointed out, you do not need Maven to add dependencies to your projects.

Related

how to add jar creation to J2EE web project

I am a newbie with maven, I created a dynamic web project in eclipse and then converted into maven project.
Instead of deploying the project in a standard container I would use embedded jetty.
How can I create in maven the appropriate jar executable with all dependencies included?
Thanks
Roberto
Found solution by myself at
http://blog.anvard.org/articles/2013/10/09/embedded-jetty-executable-maven.html
Basically we can add as many maven plugin as we want, adding dependencies and then packaging all togheter.

How can I create web fragment project in Intellij Idea

I'm looking for creating a small web framework on top of the standard servlet-3.x specifications. Main motive is to reduce the boiler plate codes and abstractions. After I read the servlet specifications it looks without using JAX-RS or SparkJava I can create small web framework for API developments.
I'm planning to use the web-fragments method to create my framework level filters, and other beans and package it as jar. So application developer create the war project with web.xml with the metadata-complete option set to false.
I can see the web-fragment project template in eclipse but I want to develop this project using maven. With Intellij I can't see any such options for web fragment projects alone. I want to create simple maven project which ship the jar with web-fragment.xml in it, so other war projects can make use of it.
So is there any plugins or other options available to develop web fragment projects in Intellij ?
Any jar file added to the WEB-INF/lib directory of a web application is technically a "web fragment", whether it has a META-INF/web-fragment.xml file or not.
Therefore, you just need to create a maven project with "jar" packaging and import it into Intellij Idea.
Create a maven project for the fragment. Set the packaging to jar. There is no need to create a war project.
Configure an empty Artifact in the Project Structure dialog:
Using the toolbar buttons, add the WEB-INF/lib folders. Then create an Archive and add the compile output:
Add a Tomcat run configuration, and in the Deployment tab, add the Artifact you just created:
I created an example project that includes all that: https://github.com/ThomasR/web-fragment-idea. Please note that it requires TomEE+.

Maven project with maven-shade-plugin doesn't deploy to SpringSource Server correctly

We assemble a war file from several Maven based projects.
One of the projects uses maven-shade-plugin to include additional library inside its target jar file. Let's call it x.jar for the sake of this discussion.
When maven builds the war file it contains the correct x.jar file with packaged additional library inside.
We use Spring Source of the latest version (3.4.0) to deploy it to the server configured in Spring Source. Server is Sprint TC Server 2.0.3.
As soon as the project gets deployed to the server (which is Sprint TC Server 2.0), the jar file (x.jar) somehow gets replaced with incorrect small one without packaged library inside.
This smaller x.jar doesn't exist anywhere in the file system before the deployment, so it looks like Spring Source compiles it on its own.
As a result we can't deploy our system in Spring Source.
Do you, guys, the reason or a workaround for our problem?
----------- UPDATE -----------
We've conducted a test. We've broken maven pom.xml file before we deployed it and as a result Spring Source deployed the original files without recompiling it. It resulted in a correct deployment.
This test shows that Spring Source rebuilds jars silently upon deploying them to servers.
Is there a way to stop it or to make it work properly?
My suggestion, unfortunately, is to not use the shade plugin, but rather a clever combination between assembly, dependency and jar plugins, kinda like the guy in the answer here:
Invalid or corrupt JAR File built by Maven shade plugin
If you have any trouble configuring it, let me know.
And if you have the error logs of the server, it would be nice to post them as well.

Java web application modularize with spring

I'm trying to build a project structure like this:
Project
|--Web_module.war
|--Data_module.jar(Spring)
|--Util_module.jar
|--other public api...
which means, different modules should be packed into different jars, so i have to have more spring configurations(application-context.xml) for different modules (e.g. for data module and for web module).
My question, how could I organize all the configuration files to include them correctly in the web module.
Thanks in advance.
Plan to have a single eclipse project for each jar file that you anticipate.
Choose the jars files / eclipse projects as per your project functionality to be modular and self contained, as far as possible.
Use junit tests in each eclipse project to thoroughly test individual projects/modules, using spring unit test support
Each eclipse project will contain its own spring config context file eg Util_module project might contain a util-context.xml
Finally have an eclipse dynamic web project as a wrapper web application which will aggregate all your "module" projects
UI artifacts like HTML, JS, JSPs, etc plus java code which uses web application contexts like controllers, servlet filters etc should be included in the eclipse web project
In the eclipse web project's java build path, but the module "projects" as "required" projects
In the eclipse web project's deployment assembly, add module "projects" as dependencies.
now when you build-all and deploy the web app, all depending module projects will compile and deploy as well, but more importantly, all project functionality will be divided into seperate modular projects
setup dependencies between projects with care, so as not to introduce cyclic dependencies
dont be afraid to refactor project structure when needed to maintain clean and relevant modules
For your modules to publish their own configuration (and your main application to detect them automatically), you can, in your main applicationContext.xml, import other context.xml files from the classpath using a pattern with wildcards :
<import resource="classpath*:conf/moduleContext.xml" />
This tells spring to find and read files in all jars that match conf/moduleContext.xml.
Note there is a little limitation to this : you must have your context files in at least one directory (not in the root of the classpath). This is why in my example you have de "conf" directory.

2 WARs in maven EAR build

I am new to maven and as a matter of fact new to the build tools and process or should i say the whole web structure. I have a slight problem for which i need help.
I am going to make a web project which on compiling/deployment will give a war file. And i have a separate project for which the war(just the war file after bundling the project) file will be given to me.
Now my requirement is to make a EAR file comprising both my project bundle and also including the war of the other completed project. I need to use maven for this.
I know the multi-module projects can be created using maven. But i am not sure how it will handle isolated war file. I mean for my project it will have the whole structure , artifactId and groupId. What about the other war how will it be handled.
I know this may be a novice question. But can someone please help.
Thanks
The way this would typically be handled would be making the isolated WAR file a dependency. Install it in your local maven repo and just list it as a dependency in the POM for your EAR module. Also, making your WAR project a module under your EAR project would handle the WAR that you're actually building.

Resources