Generate war or ear - deployment

I have a spring project include java project and a web project, and I don't know which packaging to generate EAR or WAR, this choice it depend on the application server or on the project?

EAR is used to package web projects together with enterprise java beans so they can be deployed as a single bundle.
Since you use Spring I assume you don't have EJBs in your project so you can package everything as a single war file containing the jar of you java beans project and the spring libraries (with all their dependencies) in its WEB-INF/lib folder.
Most (if not all) of the application servers which accept EAR files also accept standalone WAR files.

Related

Dynamic Web project in Eclipse Neon not including jars from Ear APP-INF/lib

I have a set of projects I am working on in eclipse. An ear with a number of jars in APP_INF/lib and several projects for the wars under the ear. They are all Dynamic web projects. In Kepler and Mars the jars in APP-INF/lib show up as a library in the war projects.
Now I am moving to Neon. I have pulled in the ear and one of the wars and the classes in the war won't compile because the jars from APP-INF/lib are not in the classpath of the war.
Did something change? Is there a settings change I need to make?
I even created a new dynamic web project. I added it the the ear as part of creation. But it too does not contain a library from the ear.
I am running the web app on JBOSS EAP 6.4
Neon.2 Release (4.6.2) Build id: 20161208-0600
I am not sure why it wasn't automatically included, but in the build path, I needed to add the Ear library.

rest project building ear not war

I have a RESTful project that I've built with Maven3 as an EAR. I have created a module project (EAR, EJB),but not sure if that will deploy in JBOSS EAp
Is it possible to build Java RESTful project as EAR to make it work on JBoss EAP instead of WAR file?

Deploying an EJB .jar to jBoss/WildFly from Eclipse

I am new to Java EE. I use Eclipse, jBoss/WildFly and Maven. I have a multi module Maven project:
parent Maven project (pom)
web application (war)
EJB project (jar)
Currently, the EJB jar project is packaged inside the web application and the entire solution is deployed as one war file.
I want to change it: I need to deploy the war and the jar projects to the application server independently (as two independent applications). Then the web project could access the EJBs via their remote interfaces.
The problem:
I can deploy the war project in Eclipse with the Run As > Run On Server context menu, however when I attempt to deploy the jar project in the same way, I get an error message saying that the project cannot be deployed.
How can I deploy my projects independently from Eclipse?
Should I wrap the jar project into an ear project? If so, can I convert my jar project into an ear project, or do I have to keep my jar project and create one more project to wrap it into an ear? (I would no like to introduce one more project).
Look at your packaging for your EJB project. You should have
<packaging>ejb</packaging>
This will allow you to run the EJB project on the server.

How to create a maven project which will work in eclipse and from command line

I have any questions:
How to create maven project which will work in eclipse and from command line.
How Does use maven good developers?
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.
How Maven Builds a WAR File

ear-javaee6 archetype doesn't allow Java files

I created a new Maven Project and selected the org.codehaus.mojo.arhcetypes ear-javaee6 archetype. When I try to add a Java class to the project I get "Source folder is not a java project".
So we are not supposed to add Java classes to out .ear files now. What are these guys thinking when they create a project for a .ear but don't allow for Java classes?
What archetype should I use for creating Java EE 6 .ear files?
From Wikipedia, EAR (file format):
EAR (Enterprise Archive) is a file format used by Java EE for
packaging one or more modules into a single archive [...]
Create an EJB (ejb-javaee6 archetype) or WAR project (webapp-javaee6 archetype for example) for your java codes and refer to it from the Ear plugin configuration (and add as dependency too).