Where should I add Java files in web (WAR) project in Maven? - java

Where should I have my Java source folder in the Maven web application architecture which results in a WAR?
Suggestions needed.

Maven web applications typically do not include java source code. The maven approach is to create a maven project for the "logic" of your web application (this will build into a jar) and create a second maven project for the webapp portion of your web application (this will build into a war). Then in the webapp portion, you introduce a dependency on the "logic" portion.
The end result is that when you build / test your logic jar (which contains servlets, etc), you will deploy that to your local repository and then build your war (which contains jsp pages, web.xml configs, etc).

The basic structure that is standard for Maven project is the following.
src/main/java Application/Library sources
src/main/resources Application/Library resource
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
Following the Maven recommendations and normal behavior makes it easier for other people familiar with Maven to easy recognize and understand the structure.
Source/Read more

Unless you explicitly specify it differently in your pom.xml (productive) Java source files in a Maven project go to src/main/java.

Java files always go in src/main/java and its advised to keep it that way.

Related

Using maven instead of ant with SPRING, deploying and structure

I have a spring project and using ANT to compile/deploy my war to Tomcat. So basically it just creates a war file and moves it to tomcat folder.
folder structure is this:
Spring
-src
ALL MY SRC (JAVA) files
-war
-WEB-INF
-jsp
-lib
ALL MY LIBRARIES
-properties
web.xml
spring-servlet.xml
-META-INF
build.xml
pom.xml // putting my pom.xml here
With ant I download all my libraries manually. Now as it seems, maven downloads all libraries automatically, thanks to pom.xml. Questions/problems:
Must I change my folder structure with maven?
Will maven include all downloaded libs to WAR? Is that default?
When maven downloads its WARS, can it put all libraries to WEB-INF/lib? How?
Does it make sense what I am doing, if no, then why?
Have few questions, because I have never ever used maven.
Yes better change the structure, see the maven site. It saves a bit of hassle, should you start using maven-plugins with complex things like using XSLT to generate java sources. Also IDE support might be better. Like:
src/main/java, src/test/java, src/main/resources.
Yes.
Yes. Automagically.
Yes otherwise you should use Ivy with ant.
Standard Maven Web Project Structure
As millimoose commented its better to go for standard project structure.

Difference between web projects with pom.xml and web.xml

What is the difference between Java projects having pom.xml and web.xml? Can projects have both these configurations at the same time?
They're completely compatible. As a matter of fact, they perform completely unrelated tasks.
pom.xml is the configuration file for Maven projects. One of its goals is to provide assistance in the compilation and building of a project when using Maven. You can think of it as an ant build.xml file or a makefile Make file if you're not familiar to Maven (actually, it can provide a lot more functionality)
web.xml is the Java EE web application deployment descriptor, where you specify for instance servlets, servlet mappings and other aspects of a webapp.
What is Maven from the Apache Maven site.
What is web.xml file and what all things can I do with it? question on SO.
The two files have nothing to do with each other.
pom.xml - Maven configuration file. Controls the build process for
the project
web.xml - Web application configuration file. Controls the deployment
and configuration of the web application
The POM file really shouldn't be deployed with the application, its just for the build process.
web.xml is an indicator that the project is running in some kind of servlet container (possibly even a full-fledged Java EE container).
pom.xml is an indicator that the project is built using the Maven build system.
Those two things are entirely orthogonal, so any given project can have none, one or both of them.
The Pom defines any dependancy libraries, it is part of Maven. This tells maven what jar files to download and store in the lib folder of your site.
Web xml is how your web project is configured.
They can both coexist as they do different things.
POM stands for "Project Object Model". It is an XML representation of a Maven project held in a file named pom.xml. http://maven.apache.org/pom.html
yes you can have both configurations at the same time.
The pom.xml is for configure your project with Maven.
The web.xml is use in all Java EE project under Tomcat for example.
You can use both, Maven is for compile and deploy your project, Tomcat is your server.

Eclipse(STS)/Maven integration issues

I am working on a fairly big project that uses maven for dependency management. As part of this we are using Maven profiles to build and replace certain properties files that differ between test/dev/production environments.
To perform a build I would execute a Maven:build using the correct profile and mavens reactor would then build the projects in the correct order and store the jars in the .m2 folder, eg the domain jar first, then the service jar (with the domain jar included in its jar as a dependency) etc. This leads to a war file eventually with all the correct libs required by the war to run.
When eclipse performs its default build that it performs everytime you save a file the jars are not built with any profile, just a regular build.
When I then push the final war file to the server and it is exploded when the server starts up (started and deployed through eclipse) I get in the lib folder all the jars that maven had packaged into the war file but also all the jars that eclipse had built.
eg
lib/
domain.jar (built by eclipse)
domain.SNAPSHOT.1.0.jar (built by maven)
etc
Is there any way to prevent this from happening? This has the end consequence of there being two of every property file and only the order in which they are loaded determines which is used. A real hassle as different properties are used in different environments.
I found a slightly hacky solution to this problem.
In the web projects properties -> Deployment Assembly I modified the path for the offending jar files eg domain.jar from
WEB-INF/lib/domain.jar
to
WEB-INF/autogen/domain.jar
This leads to the eclipse generated jar files (with the wrong properties files) to be deployed to a folder that won't be loaded when tomcat starts. Not a perfect solution but it allows all the nice things of eclipse auto-building like code completion and error messages in the web project if the interface of the domain changes etc while also providing the correct profile when deployed.
Leaving this here for anyone else in this situation.

Java classpath for dynamic web project

Hi all i have very rare problem which needs to be solved.
Problem/issue:
I have a dynamic web project which is already built and i have war file of that project.
I need to apply some customizations on top of the war file given to me.
Using maven or ant am able to compile the custom code written by me and able to add produced class files to the war file.
But the this is happening for final war file build.
when i want to test my code in eclipse. the war file build and deployed in jboss plugin contains only the class files produced out of java files written by me..........
Please help me how can i modify the .classpath file of my project so that a jboss publish can build a war file using the dependent war file which can run on eclipse-jboss to test my custom code....
Advance Thanks.....
Not a rare problem.
What you need to combine two web applications (wars) together. You have your customization war on which you need to overlay the existing web application.
It looks like you have already solved it from build perspective and looking for Eclipse support. To my knowledge, Eclipse lacks support for this. You probably need to manually do the necessary configuration to make this happen.
It looks strange to me to have two WAR files.
Perhaps you have to consider to package your customizations in a JAR and inserting that jar in the original WAR file.
Otherwise, another solution, and what I do often with open-source project to customize is to have three projects in your workspace.
PRJ-src (with your original sources/JAR/WAR)
PRJ-custom (which depends of the previous one); This project contains only the new classes or custom spring xml files (with injection of my own classes)
PRJ (the merge of the two previous projects)
I create an Ant task in the 3rd project which takes the 1st project (PRJ-src) and merge with the 2nd project (PRJ-custom). This is possible to do so with Maven as well.
Then this is the only project I deploy in my app server (tomcat / jboss).

Eclipse debug-time classpath problem: How do you include a dependent project's output into a web project's runtime classpath?

So I started with a web services project (just a dynamic web project) that builds and debugs correctly from eclipse. We've pulled a chunk of common code out that we want to put into a shared library so now those classes are going into a separate jar project that the web project references.
On the web project, I did Project->Properties->Java Build Path->Projects->Add and added the jar project. And this correctly solved all the compile-time classpath problems and everything builds fine. But at runtime, when the tomcat server fires up, spring attempts to inject some of the classes contained in the jar file and I get a NoClassDefFoundError.
My .class and properties files and the contents of my META-INF directory are showing up in the ./build directory, but my WEB-INF/lib directory seems to be referenced in-place, and the jar dependency doesn't get copied in to it to show up as part of the Web App Library.
What is the magical incantation to tell eclipse that the other jar project needs to be available to tomcat at runtime? From our ant build script, we first just build the other project into WEB-INF/lib and everything works fine, but not for eclipse debugging.
I figured this out after spending some time on it. If you are in Eclipse Helios , go to properties > deployment assembly > add > project and select the dependent project you wish to add.
Java EE module dependencies would solve this problem.
You have already done the task of extracting your common classes into its own project, possibly because other projects depend on these classes. Either way, you'll have to ensure that this is a Utility project (appears under Java EE in the project wizards), and not just a plain Java project.
One that is done, you can proceed to add the Utility project to your build path (compile-time path) as you have figured out.
The additional (final) step is to establish a Java EE module dependency between your Dynamic Web project and the shared library, which causes the utility's classes to be placed in WEB-INF\lib during deployment, and even during export of the WAR. To do so, visit the dynamic web project's properties, and browse to the Java EE module dependencies. Ensure that your utility project is selected here. Redeploy/publish your application and you should be good to go.

Categories

Resources