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

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

Related

How to make M2E (Maven for eclipse) include required eclipse projects in WAR

I'm starting to use Maven (M2E) on Eclipse.
I have two projects
->Tools
->Main
The Main project requires the Tools project. I add "Tools" under the "Main"->"Deployment assembly" option, and when exporting to WAR, the WAR will automatically contain also Tools.jar. This is correct.
Now I installed M2E on the Main Project, to manage some spring dependencies. When I edit the pom.xml, Maven starts, and correctly downloads the Spring jars, but it removes the Tools project from the "Deployment assembly" option (it disappear from the org.eclipse.wst.common.component eclipse configuration file)
As a result, when I export the WAR from Eclipse->Export, the war will not contain Tools.jar, so the webapp will not work.
How do I configure pom.xml to do what Eclipse does with the "deployment assembly"?
That is, package all classes and resources and jar dependencies of the Tools project?
I think that I'm missing some understanding of Maven, but I've not been able to google about it?
thank you

Eclipse OSGi (Kura) Plugin and Maven

I have a simple basic question. If I develop plugin for Kura in eclipse with maven, can I use dependency feature of Maven? I mean, when I use Maven for my Spring projects, I simple add dependency to pom.xml file and I can use in my project. Since I run Spring project on my local machine, dependent .jar files can be used runtime. This time, I will deploy my plugin to another device (Raspberry) , I have to put everything in the bundle .jar package. Can Maven do this or should I add dependent .jar files to classpath manualy?
You can use maven shade plugin to build the so called uber-jar - one jar that will contain your project along with all the dependencies.

Netbeans Maven project to IDEA 12

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

Eclipse glassfish plugin not publishing maven dependency jars to ear lib folder

I'm using the Eclipse "Internal Glassfish 3.1.2.2" server with the Eclipse Glassfish plugin (http://download.java.net/glassfish/eclipse/juno). I'm also using Maven with the m2e Eclipse plugin. The Glassfish plugin correctly deploys my EAR modules to the Internal Glassfish, but it does not deploy my dependency jars, which I believe should end up in a folder called "lib".
I found a partial solution here, which is to add my maven target lib folder to the EAR Deployment Assembly (double-click in the Deploy Path box to edit). This works, but m2e (or m2e-wtp) undoes the config change when I do Maven > Update Project.
There ought to be a cleaner solution. The Eclipse Ear Module Assembly dialog has a field to name the "EAR library directory", so it's aware of the lib folder. Where does Eclipse EAR assembly (without Maven) look for jars to put in the lib folder? I assume the Glassfish plugin is looking in the same place.
You can use the following steps to find the source of the problem:
Take Eclipse out of the equation and build EAR on command line using Maven, does it turn out ok? If not, look for a solution in your pom.xml.
If ok so far, open Eclipse and perform Maven -> Update Project. Then export EAR using Eclipse (look for EAR export wizard under File -> Export). Did the exported EAR turn out ok? If not, file a bug report for m2e-wtp plugin. It is still pretty raw, so it wouldn't be surprising if it doesn't setup Eclipse metadata correctly in some cases.
If ok so far, then it is time to look for a problem in Glassfish plugin. I doubt that you will get this far, but if you do I will help you to follow up.

M2Eclipse and EAR projects on Weblogic

How can I import a maven EAR project into Eclipse 3.4, and be able to use the IDE (WTP) to deploy the ear successfully to Weblogic (9.2)?
The main issue is that the dependent jars are not being included in the ear (under APP-INF/lib) when it gets deployed through the IDE. When I build from command line, the ear is exactly how I want it.
I am using the APP-INF/lib configuration for the ear plugin, and have included the jarModule sections for all the required jars.
When editing the eclipse EAR project's Java EE components, all the jars are listed, but not in the APP-INF/lib. Only when I open a dependent jar project do those specific jars get set under that subfolder. All the 3rd party jars are showing they will end up in the wrong place.
If you need more info, just let me know. Thanks!
You might want to use maven to create the eclipse WTP projects for you. This might be helpful.
The Using maven-eclipse-plugin in multi-module projects with WTP of the maven website describes how to develop Multi-module projects with Maven and Eclipse efficiently using the best of both Maven 2 and Eclipse's WTP. This covers projects that have EJB and EAR modules.
An fully working example of a multi-module projects, containing some JAR-, a WAR- and an EAR-project is even available for download if necessary.