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.
Related
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
I have a multi-module Maven project that makes use of WAR overlays (stack of them 3 deep). Everything works great in Maven, but I am unable to launch my WAR from within Eclipse because it complains of not finding various files.
How can I configure Eclipse (and a launcher) so that I can run my WAR from Eclipse without having to do a Maven Install first? I should add that my Eclipse WAR projects are not dynamic web projects...is that a part of what I need to do?
NOTE: The following question touches on the subject, but the answer indicates you must always do a Maven install first and I know that can't be right because there are folks that use Eclipse without Maven.
Maven overlays and Eclipse
I've also found this which makes me think it is not possible via vanilla M2E:
http://jira.codehaus.org/browse/MNGECLIPSE-599
I was able to accomplish this and everything works great. Here's what to do:
First of all you need the M2E Eclipse plugin which is needed to have Eclipse automatically recognize and update Maven dependencies. It also provides a nice graphical POM editor. As of late 2011, the M2Eclipse project was donated to the Eclipse foundation by Sonatype and is now called simply M2E. However, it is still not shipped with the Java EE edition of Eclipse Indigo and must be installed separately via their update site
http://download.eclipse.org/technology/m2e/releases
In the past, the integration between Eclipse and Maven has been somewhat troublesome. However, this new Eclipse based Maven integration is now aware of the fact that there are many Maven plugins used within POM files and that only certain goals of certain plug-ins make sense within an IDE environment. Furthermore, those goals that do make sense when running inside an IDE may need special configuration. To properly handle this M2E now supports the notion of a "connector"
M2E has a long list of special connectors that allow it to do the "Right Thing" when invoking various mojos (Maven plugin goals). To see the list of available connectors within Eclipse go to the Preferences dialog and select Maven->Dicovery->Open Catalog. These connectors are supposed to be installed automatically when detected within a {{pom.xml}} file during project import. However, this requires the correct version of the M2E Maven import wizard (which Indigo does not have until SR2).
In addition, sometimes these connectors are buggy and we need to use the nightly or beta updates sites to get the latest fixes. If you have an older or buggy version, then you need to delete and re-import the projects after upgrading. For these reasons, in my opinion, all M2E connectors should be installed by hand prior to importing the your project into Eclipse.
The M2E-WTP connector is the one you need to make WAR overlays work in Eclipse launchers for Eclipse "Servers". This connector is used to integrate Maven with WTP...and thus handles WAR overlays, servlet container setup, Eclipse WTP configuration, root context setting, and other things that all go towards making Eclipse web tooling "Just Work" when a Maven WAR module is imported into Eclipse as a project.
The update site for the m2e-wtp connector is: http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/
Add this M2E-WTP site and install the connector. Note however, that neither M2E nor the M2E-WTP connector are going to put dependencies of the overlay WAR into the final WAR. You must do this by adding required dependencies to the final WAR pom.xml. I don't know why the tools don't do this, but they don't -- so just get over it. ;-)
At this point, I would also recommend doing a full update via {{Help->Check for Updates}}. Once this is done, you should be able to simply add an Eclipse server, click on your project (not the overlay one, but they regular one) and choose "Run on server...".
Changes made to either the WAR or the overlay WAR will be automatically published to your server. Javascript changes are published without restarting the server...all you have to do is hit refresh in the browser to see the new code running.
Second Update: There is a bug (https://issues.sonatype.org/browse/MECLIPSEWTP-174) in M2E which can result in the WEB-INF/lib directory accidently losing all its JAR files. When this happens, your servlet application will obviously not start. The workaround is to clean the server by clicking right on the server and selecting "clean". Note that cleaning the work directory alone will not restore these missing JARs.
I've run non-Maven applications on an Eclipse embedded Tomcat server before and I found the instantaneous deployment upon saving invaluable. However after starting a Maven project I didn't find any easy way to continue doing this.
Using WTP and the Dynamic Web Module Project Facet seems to wreck the project's Maven nature. Using JRebel is not possible in our company. Googling didn't provide any results that would work without again removing the project's Maven support. Is there any other way or am I doing it wrong?
Install m2elipse with WTP extension. Import your Maven WAR project. m2eclipse will configure Eclipse for and your can continue using your Tomcat in WTP.
I have a maven war module which run eclipse WTP tomcat server like normal. Now i want to reuse that module artifact by using maven war overlay: i just create a new war project and include that module in project dependencies. The problem is now i cannot use eclipse "Run on server" anymore because i don't have any resource or config file (like web.xml) in the new project, everything is in the war dependencies but eclipse cannot resolve it.
Is there anyway to use a maven war overlay project run with eclipse ?
Thank you.
If you're trying to have a war dependency on a war project, in other words doing a "war overlay", then it's not currently supported by m2eclipse
Jer developped a plugin to implement this feature, you can give it a try : http://code.google.com/p/m2eclipse-wtp-ext/
I've test this(to debug my first JASIG CAS server), and found it works in eclipse:
Using Jetty Maven Plugin to run/debug war overlayed web apps.
Hope helpful~
I am having the same issue here. I agree with you that it's not supported yet (and has not been for almost 2 years!). There seems to be an alternative using q4e version 0.11.0+: http://wiki.eclipse.org/IAM_WTP_support which you can download from https://code.google.com/p/q4e/ or using the update site http://q4e.googlecode.com/svn/trunk/updatesite-iam/ . I'll give it a try and see whether and how it works (with m2eclipse at the same time active).
I have Maven2 war project built, I'm using the cargo start plugin, and it works great for deploying the web app. To run the maven command, I use a .bat file in my workspace, and I have en external run config to run the the bat file. I can't seem to stop the server from the Eclipse console, or re-deploy.
Does anyone have any advice on how quickly start/stop/re-deploy to Tomcat locally while developing.
Did you try JavaEE tools and m2eclipse? As far as I can see it should work well in most common situations. What m2eclipse does for you is to exactly map the maven configuration to an eclipse projects with the necessary facets set. Also, if the Dynamic Web Application facet is set for your project, you will be able to deploy it to a Tomcat server that you set up in the Server view of JavaEE tools. This configuration allows hot redeploy.
More information on JavaEE tools:
http://eclipse.org/home/categories/index.php?category=enterprise
There is a full Eclipse distribution with the EE tools available on their download site:
http://www.eclipse.org/downloads/
m2eclipse is freely available from Sonatype:
http://m2eclipse.sonatype.org/
At work we use the mentioned software as well. Additionally we use the JBoss tools that provide a feature called Project Archives that let you individually build your web application archive (ear/war/...).
To get startet you should perhaps start a fresh workspace and import the existing maven project (via the import existing maven project wizard). If everything went fine you can set up your tomcat in the server view. Maybe switch to the Java EE perspective. Right click on the newly added server and select Add to add the dynamic web project you just imported. If it doesn't show up in the list try to update the project configuration via the project's maven context menu.
Well, you could use cargo:redeploy to Undeploy and deploy again a deployable (that's a shortcut to cargo:deployer-redeploy). But I personally don't use Cargo this way, I use it mostly for integration testing (i.e. during the build) and use Eclipse WTP during development (this works whether you're using the maven eclipse plugin or m2eclipse).