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.
Related
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.
So I'm using eclipse and wrote 2 separate project. My servlet project is using the JAR file from the other project and it runs fine on the tomcat server built in to eclipse. However, if I package the servlet project into a WAR file and deploy it on the tomcat7 directly on my machine (not through eclipse), then it wouldn't run. I'm worried it might be some problem with the classpath. When I ran in eclipse, I had to manually went in RunConfiguration and add those jar files into the tomcat server classpath. How would I do that through command line?
You shouldn't mess with the tomcat classpath. Put the jar in the WEB-INF/lib directory of your war, as all the other libraries that your webapp uses.
I have a java ear maven project.
When I publish the ear to jboss7 via eclipse it generates an ear and deploys it to jboss.
But when I manually run a maven build of the same project I get a different ear in size and also the name is different.
When eclise builds the project when executing "publish to server", doesnt it use the same build as maven?
I use eclipse juno and maven3. I'm new to maven.
Eclipse uses the .classpath to package the ear that gets deployed to jboss. When you do a manual maven build it is using the pom.xml file to package the ear. If those aren't in sync you could get differences in your ears.
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
Short version: I would like to deploy Maven generated WAR to Local Tomcat server and not to build the war with internal IDEA process since the produced WAR file is not the same as the Maven generated one.
Long version: I have been using the Netbeans IDE for my Maven Java project. One of the modules is packaged as a WAR and should be deployed to tomcat server.
In Netbeans when I selected "Run" on the Web Module it asks for application server configuration then it runs the maven 'package' goal and deploys the application(generated WAR) to the Tomcat Server probably via Tomcat manager. So far so good.
We are now in the process of switching to Scala language. Scala classes extend old Java classes and the project is now "Mixed". I have configured Maven to compile the project as a mix of scala and java code. Maven produces a WAR which can then be deployed to Tomcat server without trouble - everything works as it should.
Now I would like to switch to Intellij-idea 12 IDE. But I have a problem: IDEA is refusing to deploy Maven created artifacts and insists of building one by it's own. The war is not the same and can't be used. Is there a way to tell IDEA to use Maven generated WARs and not try to make it's own ?
Just for the record I have already read these articles:
http://riddhimandas.com/blog/111
http://www.jetbrains.com/idea/webhelp/configuring-web-application-deployment.html Manually build WAR by IDEA - Want the maven generated one
IntelliJ IDEA: Maven, project compilation and deployment There is a hint here that IDEA can deploy Maven artifact from the 'target' directory but there is no information on how to do that.
With the help of CrazyCoder I figured it out.
You can make Intellij-Idea to upload your war by selecting External Source
But that's not all, even if you have a context.xml file difining your Context Root inside your WAR file: http://docs.oracle.com/javaee/1.4/tutorial/doc/WebApp3.html
You must specify one for every artifact you deploying (Unless someone will correct me)
The before launch section can be used to tell Idea what to Maven Goal to Run if any