11

I want to use maven project 2 classes in my maven project 1 so that I can use it as a parent class. Can you please tell me how to do this using eclipse and JAVA. Please write in step by step navigation as I am new in maven.

Thanks in advance

Lavish Karankar
  • 318
  • 2
  • 3
  • 13
  • why don't you just add a maven artifact of project 2 as a dependency to the project 1? – S. Pauk Oct 19 '15 at 07:25
  • could you please tell me how to do this? both the project are on my local. please write sample POM.xml for me, it would be very helpful for me – Lavish Karankar Oct 19 '15 at 07:28
  • Possible duplicate of [Maven 2 Projects](http://stackoverflow.com/questions/30934027/maven-2-projects/30935138#30935138). – abarisone Oct 19 '15 at 07:29
  • I am looking for different project not for different module therefore it cant be considered as Duplicate. – Lavish Karankar Oct 19 '15 at 07:45
  • does any of the answers helps you? – Patrick Oct 19 '15 at 09:22
  • I have been trying all, but I got to know that I have to create jar file of maven project 2 and then in pom.xml of project 1 I have to add dependency of project 2. But when I am creating jar file of project 2 then it doesnt have all the classes in the jar file, that I am figuring it out – Lavish Karankar Oct 19 '15 at 09:24

5 Answers5

7

I know its too late But this could help others who are searching for same problem.

suppose you have 2 projects p1 and p2 and you want to use p2 project's classes in p1 project then,

A) Right Click on your p2 maven project(If not maven project then convert it to maven) then
-->Choose Run As-->Click on Maven Install. (It will make p2 to avaliable for other projects in your local maven repository)

B) Now you can simply add p2 project as dependency in p1 project's pom.xml file like this

  <dependency>
    <groupId>com.project.p2</groupId>
    <artifactId>p2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <scope>compile</scope>
  </dependency>

For more details see this

kaushik_pm
  • 295
  • 3
  • 10
sohal
  • 189
  • 1
  • 6
2

maven install is a hack. the correct thing to do is create an aggregator pom that includes both project directories something like this:

   <packaging>pom</packaging>
   <modules>
         <module>pathTo/P1</module>
         <module>pathTo/P2</module>
   </modules>

and then from this new pom directory run

mvn -am compile 

the "-am" here is what is important. It will ensure both projects are added to the reactor and are discoverable.

Import this new project into eclipse and everything will work as expected.

Jason
  • 526
  • 7
  • 9
0

You can add your local project 1 what is in yor eclipse workspace to your local maven repository.

With this command (command line) yor are able to build this project to a pom.

mvn install:install-file -Dfile=c:\whereYoureFileIs-{version}.jar -DgroupId=com.companyName.projectName 
-DartifactId=projectName -Dversion={Your version} -Dpackaging=jar

and after you build your jar of Project 1 to your local maven repository you are able to use this jar in your second project as a dependency. After including this jar you can extend the class you need.

If you need more explanation, please let me know.

Patrick
  • 12,336
  • 15
  • 73
  • 115
  • how to create jar file of project 2 using maven? – Lavish Karankar Oct 19 '15 at 09:26
  • I am using TestNG therefore I dont have Main class – Lavish Karankar Oct 19 '15 at 09:26
  • you just have to build your project. This command will build your project `mvn install` . Go to your Project1 folder where your pom.xml is located. With the command line type then `mvn clean install` and you jar should be build. You can do it also with eclipse. Right click on your project1 -> run as -> maven install. The jar is then in your `target`folder – Patrick Oct 19 '15 at 09:31
  • I tried the same but it has only manifest file, pom file and properties file only but it doesnt have the dependencies and class files – Lavish Karankar Oct 19 '15 at 09:36
  • can you explain what is happened after you build the project? Any logs. And can you provide the pom.xml of both of the projects. – Patrick Oct 19 '15 at 09:40
  • Project 1 pom.xml ' maven-assembly-plugin testPack.NewTest.java jar-with-dependencies ' – Lavish Karankar Oct 19 '15 at 10:43
  • project 2 pom.xml ' com.org.mav org.mav system 0.0.1-SNAPSHOT C:\workspaceMaven1\org.mav\target\org.mav-0.0.1-SNAPSHOT-jar-with-dependencies.jar ' – Lavish Karankar Oct 19 '15 at 10:43
  • please share your full pom's in your question – Patrick Oct 19 '15 at 11:35
  • Thanks @Patrick, I have solved this actually I was keeping files in Test folder not in Main folder therefore they didnt come up. Thanks a lot again – Lavish Karankar Oct 19 '15 at 11:47
0

add a maven dependency like this :

    <dependency>
        <groupId>groupId of project2</groupId>
        <artifactId>artifactId of project2</artifactId>
        <version>version  of project2</version>
    </dependency>
nohup
  • 39
  • 1
0

First Create parent java project Simple java project

  1. Click on project (right-click) -> Configure -> Convert to maven project

  2. Convert to Maven Convert Java to Maven

  3. Right click on parent project -> new -> Porject... select maven module

  4. Create another Maven Module child project depend on base project ex : called (webapp)

  5. Open webapp/pom.xml -> dependencies tap

  6. Click add -> search about your base group id and select -> save