Running a multi-module Maven web app in Eclipse - java

I'm trying to run a multi-module Maven web app in Eclipse (EE Helios SR 1). In NetBeans one have just to click the run button. But here it's probably a little more complicated.
I've added Tomcat 7.0.26 as a server for the war module of the project. When I try running the app, in the Servers part of the window I see Apache Tomcat v.7.0.26 at localhost [Started, Synchronized], but browser doesn't react. Does the system browser need to show the page when the app is running?
I tried to move to localhost:8080 and localhost:8080/welcome.html (the second one should be processed by the app) when the server was started, but I got 404 error both times. I also didn't see the new folder in the apache-tomcat-7.0.26/webapps/. Should Eclipse place the project there when running the application? I'd be really grateful if someone tells me what I'm missing here.

If not specified, tomcat integration with eclipse deploy by default war to a specific folder in .metadata, in my case somethings like: ~/workspace/<my_project_workspace_name>/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/
Every war are then unzipped to a folder. If your maven pom.xml you'll find a
<build>
<finalName>myapp</finalName>
</build>
Then it'll deployed to tomcat as :
localhost:8080/myapp/welcome.html

I have had a lot of bad experience with running web servers in Eclipse. Most of the time, this was related to unreliable class/resource reloading.
My solution was to add Jetty to my app in a new module (see Embedding Jetty). This basically turns my web app into a Java application, avoiding most of the problems. It also allows me to specify a filter (written in Java) when the app should reload.
This solves all the problems with class reloading (classes and resources are never copied anywhere; they are loaded from Eclipse's bin folders), startup is much faster (we got the startup time from several minutes down to 15s) and reliable.

Maven Projects are different from the ones with Web Application Facets, You cannot directly run a Maven App as a web app because the folder structure is different. In order to run your application directly from eclipse, try to add Web Application nature to your maven project [A complex modification involving modification of .project and .classpath files along with addition of few other files]. I would recommend modifying your pom.xml file accordingly

Related

How to build, deploy and run Java application on JBoss WildFly from the command line?

My team is working on a Java application that runs on Jboss WildFly, using Maven to resolve dependencies and Primefaces.
We're using Eclipse to build, deploy and run the server. Eclipse does most of the work, building the WAR file, deploying it to server and running it.
Now we need to create an script that performs all those steps because it has to run on remote server.
.
The steps would be something like this...
1 Run Maven:
$M2_HOME/bin/mvn clean install
2 Build project to a war file
3 Deploy war file into the WildFly deployment folder
$WILDFLY_HOME/bin/jboss-cli.sh --connect --command="deploy --force [PATH_TO_WAR]"
4 Start server
$WILDFLY_HOME/bin/standalone.sh
.
I can perform every step but once the server is running it don't seem to be any changes in the application. I think this is because the WAR file has to be built optimizing it for JBoss. Eclipse has a way to do this when exporting the project. I need to know how to do that from command.
EDIT:
The real problem is that Jboss is not updating the published project when i run it from bash script. I thought it was because i had to build it on a specific way. I was wrong. The build is fine, just running Maven the deploy is done. The thing is that even if i rebuild project and redeploy it, server doesn't seem to notice at all.
I've tried deleting tmp, lib and data folders from standalone folder, and nothing happens. I also deleted standalone/deployed sub files and folders and got the same result.
The only way i achieve the result i'm expecting is getting into Eclipse, go to Servers tab, right click on JBoss Wildfly and click on 'Clean...'. This options seems to clean cache, rebuild and re publish the application in a right way that i don't know. I didn't find any answers on google.
eclipse server clean option
Give a look on Eclipse war export: optimize for a specific server runtime.
Quoting from #Konstantin Komissarchik's answer :
Eclipse itself doesn't do anything with that option. What happens is
dependent on a particular server adapter. Many of the adapters don't
do anything with this option either, but they might in the future.
If a server adapter does support export optimization, it has the
option of displaying custom options beneath that pop-up list of
runtimes, so that's a good cue to use to see if something will
actually happen.
So taking in consideration that in your provided image there is no custom options below Wildfly 9 selection, I am pretty sure that this option does not perform any optimization at your exported war so you can totally omit it in your new build-deploy process.
How can i build the war file from command, knowing it will have to run on Wildfly?
We are also deploying applications for a long time on several versions of Widlfy with the same approach as you are planning to, without the optimization thing. I can ensure you that we have not faced any performance issue.

Deploy java web application to Tomcat with Visual Studio Code

I'm trying to deploy a java web application to Tomcat using Visual Studio Code and I'm having some issues that I'd like to share.
After installing the extension Tomcat for Java, I've tried to deploy a Maven Java Web project. To do so, I've clicked over project's folder and selected "Run on Tomcat Server" option. The result is an error with this message: The folder is not a valid web app to run on Tomcat Server.
Then, I've realized that maybe I should create a war file before running it on Tomcat. Therefore, I've executed mvn clean package command and afterwards execute "Run on Tomcat Server" option over the war file. Doing this the project has been deployed correctly.
However, compared to Eclipse's way to work with Tomcat, it seems to me a bit tedious. Particularly if I'm working with static files, like css or js, because every time I modify a file I should execute mvn clean package and "Run on Tomcat Server". Whereas, in Eclipse, these files are deployed automatically on Tomcat without restarting.
I wonder if there is another way to work with VS Code and Tomcat for Java.
Thanks!
If you use Spring, there is an easy way.
The Spring Initializr extension comes with an embedded Tomcat installation, and hot reloading works out of the box.
Rely on the following guide to install it:
https://code.visualstudio.com/docs/java/java-spring-boot

New to J2EE - Hosting a Spring MVC based project using Tomcat 7 server on local dev machine (no Eclipse)

I am familiar with Java as a language but not JSP and Spring framework. I am trying to load an existing project and using vim as my IDE.
Originally, when working on it using eclipse, I installed openjdk7-jdk, downloaded the tomcat7 server from its website and extracted to a location, imported project in current workspace, set up the tomcat for server environment. I was able to see the application in browser.
I am looking for a way to use vim, installing tomcat from official repos and then either set up a new host for this application or adding a context for the same. I have tried both the ways but still not able to make it work. I get the tomcat standard 404 error page.
This answer here says that this may work if I create a .war file and place it in the webapps folder. Is this the only way to run a spring based application?
After working in ASP.Net, ROR, Django, I am assuming Java also has similar deployment structure.
One thing is that Tomcat is not required for spring based applications. But if you are implementing J2EE application on Tomcat, make sure you have Spring context defined in your webapps web.xml (inside WEB-INF directory).
The Spring Application context must be initialized through your ServletContextListener implementation.
Oups, seems there a long way to heaven ... More seriously you must pass by several steps (that a nice IDE like Eclipse or Netbeans can hide from you) :
create the source files for the application - you spoke of vim it can be used for that step
compile and build the application to a .war - this step is almost mandatory but i would not dare avoid it as I explain below - you can use low level tools like javac but you really should use ant or maven here ; if you are lucky, there is already a pom.xml (maven) or build.xml (ant) in the sources
deploy the war to a tomcat
In theory, it should be possible to individually compile the java file into .class, build a full hierarchy by hand and put that manually in webapps folder of tomcat. But never do that - or at least never say that I adviced you to do it :-)

Is there any guide showing how to build effectively an application with appengine and maven in eclipse?

I'm trying to develop an application using Google App-Engine and Maven using Eclipse. I am facing a problem where by whenever you change a CSS or JSP file, a restart of the application is required. This makes working quite difficult, since a reload takes a lot of time.
For example when you develop on tomcat no need for restart is required when such a change is made, only Java or xml file require restart of the server.
Is there a solution that mimics the tomcat behavior on GAE SDK when developing on localhost?
If you've built using the standard app engine maven archetype, when you run mvn appengine:devserver from you -ear directory, the exploded war working directory is located here:
<yourapp>/<yourapp>-ear/target/<yourapp>-ear-1.0-SNAPSHOT/<yourapp>-war-1.0-SNAPSHOT.war
If you change any JSPs in this directory, they will get reloaded without needing to restart the devserver.
Thus, you can set up a simple script or command that copies all your JSPs and static files from /-war/src/main/webapp to that deploy directory whenever you want your files to be refreshed. I saw on some threads where people set up their IDE (in this case IntelliJ) to automatically do that copy whenever the IDE loses focuses, which makes it trivial to get pages reloaded.
Building application with appengine in eclipse using maven is not very effective and time consuming because on every change on jsp/css/js with a traditional approach there is a need to restart appengine or mvn appengine:devserver.
#adevine make me thought about a solution, and I found this link: http://andrei.gmxhome.de/filesync/index.html wich installs a plugin in eclipse, then I activate the project to sync files from myapp/src/main/webapp to a target folder generated by maven myapp/target/myapp-snapshot-0.0.1.
It's working!!!
Whenever a change is done in webapp the files are transfered automtically(no need to run anything else, just save the file) to the target folder and are reloaded successfully.
Hope this could help people because its frustrating working with appengine(restarts takes a lot).

What happens behind the scenes of deploying a Spring MVC application to tomcat outside of eclipse?

I guess a drawback of using such an awesome IDE like eclipse is that you miss the point for what happens behind the scenes of an application. I'm a ruby developer so not a java veteran. So I've been coding a project in java and using the spring framework for IOC and MVC. Can someone explain to me what is going on when I select run on server in eclipse? Because eventually I will be deploying this masterpiece of an application to a Linux server. Here is my setup. I am using Spring MVC 3 and the maven plugin in eclipse. In the pom.xml file, I have stuff like latest spring release version, log4j, spring mvc, spring context etc.
I have been testing my application on localhost using the handy option of run on server in the eclipse IDE. The server configuration in eclipse is pointing to the tomcat directory location for where I have installed tomcat 7. Please demystify what happens behind the scenes and what I will need to do if I want to deploy this application on a production server. The more detail the better. Thanks a ton in advance.
Deploying a web application to Tomcat is as simple as this (assuming Tomcat is installed)
Bundle your application in a .war with the correct format.
Move the generated .war file to the /webapps directory of your Tomcat installation folder.
Run the /bin/startup.[sh|bat] script in the Tomcat installation folder.
Note that there are intermediate steps you can do to configure the deployment, like changing your context path. Go through the Tomcat documentation for details.
In step 3, Tomcat will extract the .war contents to a directory in the /webapps folder with the same name as your .war file. It will use this as the context path. The script itself launches a java process by putting the WEB-INF/[class|lib|...] onto the classpath along with some Tomcat libraries.
So Eclipse basically does all the steps above for you.
Ultimately you are deploying an web application that means you are deploying a war file to the server. Regardless of using frameworks like spring, struts anything.
SO a web application request starts from web.xml file. SO for spring mvc application, you are mapping all request coming from browser to DispatcherServlet and then this guy is responsible to manage whole life cycle of your application.
For more details of how MVC works please see
http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html
So in order to deploy your application (a war) on server first of all you have to create a war from your source code. You can go to traditional approach to use java given utility like using jar from command prompt or you can use ANT, GRADLE, MAVEN and such build tool that creates war for you in automated way.
Spring is not doing anything extra for you. I believe you to research a bit more on how these tools works.
Once a war is ready for you, you can simply go to tomcat UI and there you will find options to deploy your war.
I hope it helps you.
All the majic happens in two places.
The first is your 'Servers' directory in the root of your Eclipse Package Explorer. These are your server configuration files that Eclipse will use (mostly) when it creates a new server instance.
The second is in the ./metadata/.plugins/org.eclipse.wst.server.core/ file system directory in your Eclipse workspace. This is where the tomcat application is actually deployed by eclipse.
The Tomcat Documentation is pretty good actually and helps explain how to do deployments. FYI, I do not know many people that use the Manager, from my experience most people deploy their applications by hand.

Categories