Creating a simple Java Web project with Maven tutorial -- Part 2 Understanding war and pom.xml
Creating a simple Java Web project with Maven tutorial -- Part 1 covered developing a simple Java web application using Maven. This part is all about understanding the war and pom files.
1. The lib directory
Q. How did the servlet-api-2.5.jar get into the WEB-INF/lib folder?
A. You need to understand the pom.xml file that we used n part-1 tutorial.
There are 6 scopes, and most popular ones are
- compile: this is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
- provided: this is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
- runtime: this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
- test: this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
Q. Where do the compiled class files and web.xml need to be in the war file?
A. WEB-INF/classes and WEB-INF respectively.
Q. Where do web resources like .jsp, .html, .css, .jpg, .gif, .jsm etc go?
A. If you want it to be publicly accessible via a url like http://localhost:7002/simpleWeb/index.jsp then you need to put in the root (i.e. simpleWeb.war). If you want to protect its direct access, then in subfolders inside WEB-INF. Create folders for html, images, JavaScrip, css, etc. The protected approach is recommended.
Q. What if you want the war file to published to your local maven repository?
A. Run the "mvn install" command.
After running the command, inspect your local maven repository
Q. Where did maven get the group, artifiactId, and version info?
A. From the pom.xml file.