How to add Maven to existing Java WebApplication in IntelliJ? - java

There is standard Java WebApp project. I need to use Maven to build/compile/deploy project to online server. There is an option to create new maven module module or to import project using pom.xml. Following probaby won't work because:
Project doesn't have maven project and pom.xml
Creating new maven module probably would mess up my existing project structure.
How to add Maven support to existing Java WebApplication in IntelliJ 12?
UPDATE:
I selected Add framework Support... > Maven. Generated pom.xml at project's root. Added maven-war-plugin. The following structure I have got. Is it any good / correct?

You should use maven standard project structure for maven applications. However, if you cannot change it, then you have to tell Maven where to look for specific files. Consider:
Using maven using non standard directory layout
Standard Maven structure
Also here are some interesting things to remember: http://wiki.jetbrains.net/intellij/Maven_FAQ

Related

Combining a maven project with a java project

Currently, I have a maven project that has a server running (using Jersey REST API). I also have a java project, I need to move all the contents of the java project into the maven project. The maven project is a subset of the java project. However, the maven project only displays the parts of the java project. However, I want a project that allows me to use maven and displays all of the other details from the java project.
I would've copied and pasted however I'm using git so I want to also preserve history.
I was thinking it would be easier to nest the maven project inside the java project but I don't know if that's possible.
Here's a picture of my package explorer to help explain everything.
Package explorer showing the maven project being a subset of the java project.
What I've tried is converting the java project into a maven project and then updating the pom.xml but then it doesn't link to the web.xml. Also, it tries to run the server with the name of the project name TeamProject. When infact it should run the url with the name client_server
I was considering just copying and pasting all the code into the maven project (from the Teamproject java project).
Actually nesting the java project inside the Maven project makes more sense, as it is the purpose of Maven to handle a project lifecycle. (also by default Maven will look for sources in the src/ folder which should ease the task of putting your Java project inside Maven's hands)
There are several possibilities I would see:
Copy your java project in the src/ of your client project and update maven accordingly (within the pom.xml)
Make your Java project a Maven project and aggregate the two projects in a parent pom (see Multi module maven project example)
Make your Java project a Maven project, and decide of a "Master" project between it and the client and compose one with the other (not sure that's a great solution)
Nesting your Maven project inside the Java project would not be so great because Maven could only handle the client and not your Java project, and then you'd miss on numerous functionalities offered by Maven (just look at how simple it is to get dependencies compared to downloading a jar and including it on the build path manually)

Maven GroupID and ArtifactID for J2EE Utility Project

I have a collection of 3 existing Eclipse projects that I want to make into Maven projects and I'm having a hard time understanding the relationship between the GroupID, ArtifactID and my Java Packaging, and the different directory structure that is created for Maven vs. Java projects. I'm pretty new to J2EE and completely new to Maven, so any advice is welcome! Let me describe my projects first.
The first project is IDMU_JAR. The base Java packaging is com.foo.util.merge and it contains Java classes like Template and Bookmark and the target jar file is com.foo.util.merge.jar
The second project is IDUM_WAR. The base class in this project is com.foo.util.merge.web and it references the IDMU_JAR project and contains only a few Servlets that expose the functionality in IDMU_JAR.
The third project is IDMU_EAR. Currently this is just a device for creating a deployment EAR that contains IDMU_JAR, WAR and it's dependencies.
My questions are:
Is my J2EE packaging and naming "typical" or have I already gone against best practices?
Am I better off creating the Maven Project and then converting it to a Faceted project and picking the facets, or should I be creating the Java Utility/WAR/EAR projects and then converting them to Maven projects?
This appears to have a big impact on the directory structure of the project, is this important?.
Does my Eclipse Project name have to match the Maven Artifact ID?
You can configure pretty much anything in maven from file structure to project special configuration. But If it's not absolutly necessery you should stick with maven defaults.
You can start with that project template:
javaee-essentials-pom
For the project namings:
GroupID: com.foo
ArtifactID: idmu-utils, idmu-war, idmu-ear
package: jar, war, ear
With Eclipse:
Start by using the template I provided. Importing as maven project from Eclipse should resolve all facets available automatically.

How and where does a pom.xml will be configured to be invoked by a web project

I have been working on maven for some time but never had this question before, Many times I created my maven project using a IDE wizard which helps me to create a maven project which has pom.xml pre-configured or the other way is to convert a general java project into maven project which generates a pom.xml. I have a question what exactly happens in the background when we convert this java project into a maven project. What will be configured and how does the java project detects it has a pom.xml.
Inside the .project file in the root of the Eclipse project, m2eclipse will add the org.eclipse.m2e.core.maven2Nature to it. This tells Eclipse that the m2e plugin will handle various stages of the project lifecycle, and m2e has a hybrid partially-internal and partially-external engine that applies the instructions in the pom.xml to the project. (For example, it finds the classpath placeholders that are marked as maven.pomderived and replaces them with the appropriate Maven dependencies.)

How to use Maven with EAR

If i want to convert an EAR project a maven project , do i need to add the module in the deployment assembly as maven dependency or just use the convert in m2eclipse without any further configuration.
Me personally I wouldn't attempt any kind of conversion of an existing project. I would add the poms, make sure that mvn clean install works on the command prompt and then create a new mavenized Eclipse project from the poms.
The main reason is that you current project settings are effectively wrong when you switch to Maven - the Maven poms are the truth and what feeds the Eclipse project setup, so you really do not want to make your life difficult and work against m2eclipse - let it do the project creation for you. Fresh.
You can install m2eclipse and then do the following as well.
Go to the project menu (right click on Package Explorer) > Configure > Convert to Maven Project
Open the pom.xml and right-click and choose Run As -> Maven Clean. Similarly Choose Run As -> Maven Install.
Note : Please ensure that your eclipse project settings are correct and classpath libraries are not absolute and you don't have any project specific environment variables defined in your workspace. Please take a backup of your project before you do this.This is to ensure we don't mess up the current stable project configurations. Once m2eclipse generates the pom.xml for your project, you can update and make changes to it to
fully obtain a mavenized ear build. hope this helps
You can also try creating new maven project with archetype selection of "jboss-javaee6-ear" and follow the similar structure for your project. Most probably you will need parent Pom and child poms per each module (ejb, war, jar etc). There are other few similar approach but almost all of them requires you to have mulitple POMs
maven-ear-plugin and JBoss AS 7
You can also go through all the examples for maven ear plugin to find settings suitable for you
http://maven.apache.org/plugins/maven-ear-plugin/
I ended up ditching ear for war :) single POM and even ditched the JBOss for tomcat/jetty :)
If you want to convert your existing eclipse dependencies into Maven dependencies, you can try the JBoss Tools (JBT) Maven integration feature, which contains an experimental conversion wizard, plugged into m2e's conversion process : http://docs.jboss.org/tools/whatsnew/maven/maven-news-4.0.0.Beta1.html.
So, all you have to do is, as Keerthi explained, right-click on your project and Configure > Convert to Maven...
If your dependencies already are maven artifacts, it should work easily. If not, you'll need to convert them to Maven (if they're workspace projects) or make them available in your maven enterprise repository, before converting the EAR project.
JBT (requires Eclipse JavaEE) can be installed from http://download.jboss.org/jbosstools/updates/stable/kepler/ or from the Eclipse Marketplace (See https://marketplace.eclipse.org/search/site/jboss%2520tools)

Applying Maven to a project

I've been asked to apply Maven to a project. After browsing a dozen sites it appears that it's quite vast and I'm not familiar as I'd like with similar tools like Ant. Why is it used/preferred and what does it offer over a standard Eclipse project? Also, how could it be added to an existing project?
Why is it used/preferred and what does
it offer over a standard Eclipse
project?
It is a build tool which can build your project without the need for an IDE like Eclipse. It can create a jar or war or other artifacts from the source, performing a bunch of steps like compilation, running unit tests, etc.
Where maven scores over ant is in managing third-party dependencies and in convention over configuration (which mean less lines of build script if you follow convention).
Also, how could it be added to an
existing project?
You start by creating a new maven project, following the step here.
Place it in the root folder of your project
If your source and resource files do not follow maven folder convention, update maven properties suitably referring to this documentation.
Run mvn package
It will fail if it needs any third party dependencies, which you can add as specified in the doc
With some trial and error, you should have your project running with maven, possibly, much quicker than if you were to set up the same with ant.
Others are already provided sufficient resources to read more about maven.
I suggest to start reading here:
http://www.sonatype.com/books/mvnref-book/reference/public-book.html
Maven is a great tool when you know how to use it. Maven (at core) is a dependency manager.
You include in your pom.xml (similar in function to the build.xml from Ant) all the librairies your project depends on (example : apache commons) along with their version and Maven get them directly from a repository (by default, the central maven repository)
Then you do not have to manually install any jar to make your project work. All is downloaded and cached on your local machine. You can even create an enterprise repository where you put all the jars needed by your company
Maven uses the concept of artifacts which are pre-built library projects with their own dependencies
To mavenize a project, you'll have to write a pom.xml describing your project (examples are numerous), get rid of your libs directory (or whatever classpath you described under Eclipse) and add all your dependencies to your pom.xml
You could also check Mavenizer for a first-start
But Maven is a lot more what i've just said. Read the docs, read poms from librairies and you'll get used to it quickly ;-)
If you use the M2Eclipse plugin from Sonatype, it's just a matter of right clicking the project in the package explorer and choosing Enable Dependency Management in the Maven menu. You are also advised to adjust the directories that contain the sources to the Maven standard directory layout but if you absolutely can't, you can configure that later.
Apart from that: Well, look for tutorials and documentation (for example there is the free book Better builds with Maven. Maven is very complex (yes, I don't think it is simple) and very powerful.

Categories

Resources