Java Dynamic Web project with Maven and Eclipse - java

I have several questions about creating a Java Web application with Maven and Eclipse:
How do I create a Java web project with servlets, jsp, and other classes with Maven?
It creates a simple directory structure, src->main->java. Where and how do I put the web-inf folder?
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
Is there a way to test the servlets with junit?

Wow that's a lot of questions at once. I admit that setting up a webapp project with Maven and Eclipse can be tricky, so I'll try to answer them all.
Creating a Web application project with Maven
How do I create a java web project with servlets jsp and other classes with maven? It creates a simple directory structure, src->main->java.
When you are creating a Java web project, the final product should be a WAR or EAR file. WAR and EAR files are JAR files with a specific structure that can be deployed in an application server or servlet container.
As mentioned, the easiest way to set up a Maven project for web applications is to use archetypes:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
If we create a project with this archetype then a simple directory structure and pom.xml are generated. This project follows the standard Maven directory layout you mention, with /src/main/java/, /src/test/java, etc. Maven generates the WAR file from this structure with the war:war goal.
Note that this archetype is for a very simple (outdated) web application, but it's the best available starting point. You probably want to discard the web.xml file and create a new one that supports Servlet 3.0.
WEB-INF location
Where and how do I put the web-inf folder?
By default, Maven expects resources that should go in the root of the WAR file -- such as images, html pages and the WEB-INF directory -- to reside in /src/main/webapp/. So the WEB-INF folder should be located at /src/main/webapp/WEB-INF/. If you use the maven-archetype-webapp this directory is automatically created, along with a sample web.xml file.
Eclipse integration
You mentioned Eclipse in the question title, and it is indeed possible to develop Mavenized web applications in Eclipse with the m2eclipse plugin. Eclipse has good support for Web applications through WTP (Web Tools Platform).
Although many guides on the internet (wrongly) recommend it, you should not use the mvn eclipse:eclipse command to create the Eclipse project. This plugin can only generate WTP projects for very old Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described here.
Dependencies
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
There is no need to do this manually, since one of the key strengths of Maven is dependency management. If you add a dependency in the pom.xml with a scope of compile or runtime, the JAR file will be automatically included in the WEB-INF/lib/ directory of the WAR file. For example to add the Postgresql JDBC driver dependency, add this to your pom.xml:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
Since the scope is unspecified Maven will assume it is in the the default scope compile. The result is that Maven will include WEB-INF/lib/postgresql-9.1-901.jdbc4.jar in the WAR file.
Testing
Is there a way to test the servlets with junit?
This question has been asked (and answered) on Stackoverflow:
Unit testing a Java servlet
Unit testing servlets
References
Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse.

You should create a project based on the webapp Maven archetype, not the default one you're using.
I'm using SpringSource Tool Suite, which, for this exercise, is the same as Eclipse with m2e. Create a new Maven project and make sure you select the following archetype:
The Maven the command-line way of doing this is:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
This archetype will put the WEB-INF folder in the correct location (under src/main/webapp).
You can find more information at http://maven.apache.org/guides/mini/guide-webapp.html

Just create a normal "Dynamic Web Project". Then right click the project name and select Configure > Convert To Maven Project. It can take up to a minute to complete the conversion, so be patient. See the following article:
http://aykutakin.wordpress.com/2013/12/04/create-eclipse-dynamic-web-project-with-maven-2/
If that doesn't work, try this:
http://crunchify.com/how-to-create-dynamic-web-project-using-maven-in-eclipse/

Step 1: create your web app folder.
Step 2: Move to that folder in command prompt.
Step 3: use following command:
mvn archetype:generate -DgroupId=com.helloworld -DartifactId=HelloWorldDemo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
DarchetypeArtifactId=maven-archetype-webapp will create a maven web project.
Step 4: Now on command prompt go inside the project folder and use this command:
mvn eclipse:eclipse -Dwtpversion=2.0 to make your project a dynamic web project for eclipse, now import this newly created project as an "Ëxisting project"
Use m2eclipse plugin of eclipse to enable MAVEN in eclipe.
Web.xml will be at \src\main\webapp\WEB-INF

Related

How to create a Java Spring WebApp Folder Structure using Gradle

I am making a Java Web App project using Gradle. Now, for building a Gradle web app plenty of resources are available over the net.
But, my issue is a bit different. Suppose, I want to initiate a Gradle based project and make a web app suitable project folder structure just like we do in maven. Is there any specific way to create so in Gradle. I have found something about templates. Is there any way to do it or do I have to create a folder structure manually and then attach to Gradle so that it can build a war out it properly.
You can use maven to create the project and then create gradle build from it:
mvn archetype:generate -DgroupId=com.example -DartifactId=java-web-project -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Then:
gradle init --type pom
You didn't mention your IDE, but I will answer for IntelliJ IDEA and for Eclipse the steps are similar.
Go to File --> New --> Project, choose Gradle from the left menu and choose both Java and Web options (see image below).

Merging JSP code and Maven code

I have an Apache Maven Project for JIRA REST API. Now I am creating a JSP project to send some data to java file in my Maven Project. I want to know how can I merge them in single project. Maven Project has a pom.xml file and the java file for Maven are located in src/main/java whereas for JSP it is in different folder. (Java Resources/src)
Can anyone please tell what folder should I copy and merge?
EDIT: Found the answer. Right click the Maven project and go to Project Facets and check Dynamic Web Module option.
you can create a Maven Webapp using following archetype
-DarchetypeArtifactId=maven-archetype-webapp
which will automatically creates the desired folder structure
in your case its apart from src\main\java
src\main\resources
src\main\webapp
src\main\webapp\WEB-INF --THIS IS WHERE JSP's will go
Standard Maven Directory Layouts
Maven Web Application
Found the answer. Right click the Maven project and go to Project Facets and check Dynamic Web Module option.

how to build an eclipse dynamic web project in hudson

I have been using maven projects in hudson quite successfully for some time.
However this time I need to build an eclipse dynamic web project that is usually built within eclipse and then the war is exported to production. How can I build this project in hudson.
Thanks in advance!
If you're familiar with Maven, I suggest you use the m2e-wtp plugin -- that way you can use both Maven and WTP for your web app. It should be somehwhere in the m2e catalogue (Window > Preferences > Maven) now that m2e moved to the Eclipse Foundation, check Maven/Tomcat Projects In Eclipse Indigo/3.7 for more.
Hudson/Jenkins will just build it as a regular Maven project, no extra config needed.
Eclipse will mostly continue to use its own tools with some Maven inbetween, but most importantly it will get any dependenices from Maven and have them in the build path
If you already have a Dynamic Web Project in Eclipse, you'll probably have to move around a bunch of folders to arrange them in the structure Maven expects:
src/
main/
java/ -- your Java source files (servlets, actions, ...)
resources/ -- your resource files (struts.xml, log4j.xml, NOT web.xml)
webapp/ -- your web root (previously WTP's WebContent/)
WEB-INF/ -- your WEB-INFt (web.xml)
test/
java/ -- your Java test cases
resources/ -- your test resource files
pom.xml
Set the <packaging> to war so Maven knows that it should put your dependencies into WEB-INF/lib/ and build a WAR.
As for dependenices, you'll probably need the Servlet API:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version><!-- change version as needed -->
<scope>provided</scope><!-- Mind the scope!-->
</dependency>
Pay attentation to the provided scope: The Servlet spec prohibits web apps to bring their own Servlet API JAR along in WEB-INF/lib and conforming web containers will refuse to load your web app in that case (they will provide the JAR themselves in the version they support).
It's probably best to start by right-clicking on the Dynamic Web Project and go Maven > Convert to Maven project, then move the folders as shown above and then go through all the warnings the m2e and m2e-wtp plugins throw at you (most offer a quickfix).

Eclipse Maven project to Dynamic Web Project layout

I am used to the layout provided by the Dynamic Web project in eclipse where you have all your Java code under the src folder, your JSP files, etc under the WebContent folder and the WEB-INF resides right under the WebContent folder. Also, the Output folder is hidden from the project tree. Maven has its own format which I find somewhat confusing, it creates its now target folder and source/main/java folder and some test folder under target...
Is there a way to create a Maven project and have it use the Dynamic Web project project tree layout?
I tried changing the Deployment Assembly entries to match that of the Dynamic web project and have also update the Java Build Path src and output to match the Dynamic Web project one but I get strange, cryptic unfriendly errors when I do a Run/Install. Can you provide me with details on how to go about doing this cleanly?
What you need to get used to using is the concept of archetypes in Maven.
The concept is like project templates ready for you to use that are automatically understood by Maven.
mvn archetype:generate
will get you an interactive listing of all the available archetypes
What you want to look for is maven-archetype-webapp this will give you a basic skeleton framework of what Maven is expecting a webapp to look like.
There are others that include support for different frameworks and what not.
mvn archetype:generate | grep webapp
will filter out all the choices that are webapp archetypes
Then it is a simple mvn clean package and you get an exploded dir and a .war in the target directory, you can even enable the Tomcat plugin to automatically deploy to Tomcat. Other J2EE servers are supported as well.
Then you use the Maven plugin for eclipse to load the project based on the pom.xml file.
You could create your own non standard layout and get it working by manually configuring everything thing and create an archetype out of it but that kind of defeats the purpose of a single way of doing things with a tool like Maven.
Using m2e 1.3+ and m2e-wtp 0.16.0+, you can just :
create a new Dynamic Web project
right-click on the project > Configure > Convert to Maven...
Fill in the Maven coordinates of your project and finish
It'll mavenize your project, keeping the standard Eclipse project layout.
See http://wiki.eclipse.org/M2E-WTP/New_and_Noteworthy/0.16#New_project_conversion_participants for more information

GWT with Maven under Eclipse. What's the development cycle?

I am writing an application that uses GWT, some Spring MVC and Spring Security. Maven is be used to manage the dependencies and Eclipse is my IDE. I've created my application as follows:
webAppCreator -noant -maven -XnoEclipse -out MyApp com.example.MyApp
Then I've imported it into Eclipse as follows:
Imported as Maven project into Eclipse workspace.
In the project settings the "Use Google Web Toolkit" checkbox is ticked.
This project has a WAR directory" is checked. The WAR directory is set to src/main/webapp. The "Launch and deploy from this directory" is unchecked.
Then I test it's all OK:
Click run/debug, choose Web Application (the Google choice), and select target/www as the WAR directory.
So far, so good. Now I want to know how to control the development cycle. For example, I now want to add Spring to my application so I add this block to pom.xml and save it. The Maven Dependencies in Eclipse are expanded to show many new jars (Spring and its dependencies).
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
I also edit the web.xml to configure the Spring DispatcherServlet...
Once I've edited pom.xml and web.xml, clicking the debug/run icon on the toolbar will simply run from target/www again without any other steps taking place. This means the old web.xml is used, and target/www/WEB-INF/lib doesn't contain the Spring jars (and Spring's dependent jars). Do I need to drop to the command-line and issue Maven commands whenever particular changes are made?
The ideal answer will describe the development cycle for the above scenario.
My configuration:
GWT: 2.1.
Eclipse: Helios (3.6).
Maven: 2.2.1.
JRE/JDK: JDK 1.6.0.21.
Google Plugin for Eclipse 3.6 version 1.4.0.v201010280102.
Maven Integration for Eclipse (m2eclipse) version 0.10.2.20100623-1649 (this has been configured to point to Maven 2.2.1 environment and NOT to use the embedded Maven3 "instance").
I didn't understand you question completely but I use to create a separated gwt maven project and use apache web server to serve gwt files and then config my main webapp to load gwt files at client/browser. That save me from mixing gwt codes with my main webapp.
Edit regarding your comment:
Forget about gwt for start and read on ...
First you need to make sure that eclipse and maven are playing together correctly so that when you add dependency to you pom eclipse will recognize them too, and your eclipse project setup and structure match with maven.
Second you need to set up a web development configuration so that you can run/test your webapp. You can use maven jetty or tomcat plugin or eclipse tomcat server. IMHO using maven jetty plugin is better.
And Finally you need to package (using maven) and deploy (using maven or not) webapp to your deployment environment.
If you have been successful till now, then you can add gwt in using maven gwt plugin.
If you have installed eclipse maven plugin you can run maven commands from inside eclipse but it wouldn't hurt to run them from command prompt any way.

Categories