Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrate Maven with existing Java Project using Eclipse

I have created this Java project using struts, hibernate in Eclipse Helios,

Now I want to integrate this project with Maven how to do it?

I have installed Maven In Eclipse already.

Tutorials, blogs, websites that I have found so far are explaining integration of some project into Maven outside Eclipse and then importing it in Eclipse or crreation of New project with Maven.None of them so far addressing my Problem.

As I mentioned I have created a project in Eclipse already Now I just want to integrate it with Maven, how to do it?

like image 540
Just_another_developer Avatar asked Jun 17 '13 10:06

Just_another_developer


2 Answers

In eclipse you can easily convert a java project in a maven one right clicking on project -> configure -> convert to maven project.

like image 148
Matteo N. Avatar answered Nov 13 '22 02:11

Matteo N.


While an IDE "importer" can sometimes be handy, it is not required to turn a project in Eclipse into a maven project. Basically all you just need is to add a pom.xml file and follow maven's conventions - or configure it.

By using the maven-eclipse-plugin it is actually possible have a maven itself generate the necessary files to integrate your maven project with eclipse:

  1. Start from the command line
  2. Go to your project's root
  3. Create a new pom.xml file from a simple template or initiate a new project folder structure (including a pom) using mvn archetype:generate
  4. Type mvn eclipse:eclipse.

Then maven has generated the necessary files to integrate with eclipse.

That said, maven by convention expects a certain folder structure of your Java project. It looks like this:

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

So unless you already have this structure, you need to move your source code to main/java (and unit test code to test/java).

Further if your project has dependencies to other projects; then you need to express these dependencies in Maven's pom.xml file. If your dependency projects are stored in the Maven Central this is particularly easy. To express a dependency to e.g. Apache Commons - you would add this to your pom.xml:

<project>
  ...
  <dependencies>
     ...
     <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-io</artifactId>
         <version>1.3.2</version>
     </dependency>
     ...       
  </dependencies>
  ...
</project>

After these initial attempts to integrate your project with maven, you can try to build with mvn compile from either the command line - or using the m2eclipse plugin for Eclipse.

like image 27
tbsalling Avatar answered Nov 13 '22 02:11

tbsalling