How do I go from a Maven Dynamic web project to some executable - spring

I have a Maven Dynamic web project in Spring - I can run this in eclipse with a tomcat server and serve all my code a web page via localhost.
I am looking to create an executable which can be ran locally on a machine with no internet connection and when ran will serve the web project to a specified localhost URL.

If you have included an embedded server (like tomcat) as a dependency in you POM.xml file, then you have to Right Click on your Project -> Run as -> Maven Install. This will build a Maven Executable Jar file in the target folder of your Maven Application.
Take the Jar file and Run it from command line using
java -jar <jarfilename>.jar

After building project you will have <project_name>.war file created in target folder inside the project.
You should deploy it to a J2EE application server, like: Glassfish, JBoss Wildfly, etc.

Related

What is the use of Apache Tomcat Webserver

When building the Java web application through the maven build tool we get the war file as the artifact which is an executable file. So I have a basic doubt which is if we can execute the war file directly then why do we need an Apache tomcat webserver to host this war artifact?

Maven JBOSS application deployment

I followed this tutorial http://www.mastertheboss.com/jboss-maven/jboss-maven-example-building-a-java-ee-6-application/ in order to have a simple web application to better understand Java EE and JBOSS. I set up the example project (by archetype) and compiled it.
However, I am stuck after running mvn compile. I want to deploy my application as a war file to my JBOSS webroot directory (in my case /usr/share/jboss-as/standalone/deployments/).
I think mvn package and mvn install must be executed. Where can I specify that I want a war file and that it should be copied to my deployment location on JBOSS?
Obviously, I can use the jboss maven plugin http://docs.jboss.org/jbossas/7/plugins/maven/latest/, which is addressed via console
jboss-as:deploy
Configuration is read from the POM file.

deploying multple war files using intelliJ IDEA

I have a maven based spring mvc project. I can build that project and run it via tomcat manually like this
mvn package -Dbuild.name=App1
mvn package -Dbuild.name=App2
mvn package -Dbuild.name=App3
and then can copy those generate war files App1.war, App2.war and App3.war into my tomcat folder and start it. It works fine.
Question is how to do the same using intelliJ IDEA?
I generated war files one by one by using Maven Project window and creating custom value for package goal. It generates .war files in target folder. But when I run it in Tomcat of intelliJ it only runs the application at root "/" address. For other addresses
/App2 -> 404
/App3 -> 404
You can have multiple artifacts created in IntelliJ IDEA and configured for deployment at the same time in the application server Run/Debug configuration, Deployment tab under different contexts.
Note that Deployment tab has a configuration to perform the deployment from the External source where you can specify the location of the war produced by Maven if you don't want to use artifacts for some reason.

Deploy MavenWebApp in Eclipse Tomcat

I have the following Problem:
I have a Maven MainProject with some SubProjects.
Two of this SubProjects are WebApplications. (One WebApp and one WebService)
If I build the MainProject in the command line with mvn clean package the 2 .war files are deployable in an external Apache Tomcat 6.0.
If I build the MainProject in eclipse with the m2e plugin i can also deploy the .war files into an external Tomcat.
BUT if i want to deploy the WebProjects in an Apache Tomcat in Eclipse, it doesn't boot.
The Problem is, that the file structure of the eclipse build for the internal Tomcat is different to the Maven one.
In the Maven build (console / m2e) the path is correctly from e.g. Project/src/main/java/com/... to WEB-INF/com...
The eclipse build for the internal Tomcat creates the structure like WEB-INF/main/java/com/...
So many of the .xml files which are mandatory for the Tomcat boot are not found bacause of the wrong path.
How can i tell eclipse that it should do the similar build like m2e or maven on command line so that i can use an internal tomcat in eclipse?
I am using
Eclipse Juno 4.2 or Helios 3.6
m2e - Maven Integration for Eclipse / 1.1.0.20120530-0009 / org.eclipse.m2e.feature.feature.group
Maven Integration for WTP / 0.15.2.20120306-2040 / org.maven.ide.eclipse.wtp.feature.feature.group
I fixed the problem.
The problem was the deployment assembly options in the eclipse projects.
Eclipse default option is to deploy /src to / folder.
Because of the maven structure /src/main/java/com the deployed folder also looks like /main/java/com.
I only had to change the source folder from /src to /src/main/java (the destination still remains /). If there are other necessary files i had to add them the same way, e.g. /src/main/resources.
The /src/main/webapp folder was already set correctly by eclipse to /WEB-INF

Maven Jetty Run from Jar

Here is want I want to do. I created a maven project and configured the jetty plugin for it in eclipse...
So from Eclipse if I do run and set the maven goal there to be jetty:run it runs my project in jetty on the port specified in web.xml. Now I want to build the jar file and when I do java -jar myapp.jar it will automatically call jetty:run.
How can I do this?
If you want to package your application so that you can hand it to someone and have them run it as a standalone application without having to go through deploying a war file into a web container, then that is a different concern from doing mvn jetty:run at development time, I will call that deployment time to avoid any confusion
At deployment time, we can't assume there will be maven on the machine, thus no mvn jetty:run, and even if there was, this would not work, unless we deliver the source code to run the build as in the development environment!
A standalone web application can be packaged by bundling the jetty jars in the application war along with a Main class to start jetty programmatically, and get it to run the application war. This relies on the fact that the file and directory structure of the WAR and JAR are different, and thus there is no significant overlap between the two, which is what makes this workaround possible, and it also leaves the option of deploying the war file in a web container possible
There is a maven plugin that embeds winstone which is another lightweight servlet container
For jetty, you may start by reading Embedded Jetty 7 webapp executable with Maven

Resources