1

in my company, I have a Java project:

  • common: This contains classes that are used for several projects
  • GUI: This project contains a GUI for common

Without Maven, I add common to the class path, I can build the project. But how to setup the POM.xml and the Eclipse workspace when I want to build the GUI with Maven?

I also want to package the app into a JAR later, so I tried to setup the gui as .jar package

mvn_package

But then how to assign a Maven type to the common project?

Ideally, I could import "common" to the Maven project?

UPDATE:

Ok, it seems the mvn package command is able to resolve the "common" project when I add as local dependency see thisenter image description here under GUI. Still a bit confused about whether to use "pom", "maven-plugin" or "module" - anyone can add some links/clarifications, when to use what approach?

poseid
  • 6,986
  • 10
  • 48
  • 78

3 Answers3

1

Declare common as usual maven dependency in GUI. If it isn't maven project, add it to local repository as shown there How to add local jar files in maven project?

Community
  • 1
  • 1
Dmitry
  • 864
  • 7
  • 15
  • thanks, that seems to work. still, there is plugin, pom and module relationships between projects, I guess this is for other questions, but if you could provide some basic pointers when to use what, that would be nice! thanks! – poseid Apr 26 '16 at 08:51
1

I would follow this steps

  1. Create a local maven repository to store your custom jars. Which nothing but a proper directory structure with pom files in it.
  2. Create your sub projects(common, gui) and package them as jar.
  3. Install the local jars to local mvn repository.
  4. Use the jars as dependency from your project.

Example:

  1. Create a master project on ${master_project} location and your subprojects on ${master_project}/${common} and ${master_project}/${gui} location.

  2. Create mvn repository in: ${master_project}/local-maven-repo

  3. Specify your local repository location in your subprojects pom.xml located in ${master_project}/${gui}/pom.xml

    <repository>
        <id>local-maven-repo</id>
        <url>file:///${project.parent.basedir}/local-maven-repo</url>
    </repository>
    

  4. Install your jars in local mvn repository

    mvn install:install-file
    -Dfile=<path-to-file>
    -DgroupId=<group-id>
    -DartifactId=<artifact-id>
    -Dversion=<version>
    -Dpackaging=<packaging>
    -DgeneratePom=true
    
    Where: <path-to-file>  the path to the file to load
    <group-id>      the group that the file should be registered under
    <artifact-id>   the artifact name for the file
    <version>       the version of the file
    <packaging>     the packaging of the file e.g. jar
    
  5. Finally use them as regular dependency

     <dependency>
         <groupId>org.company</groupId>
         <artifactId>common</artifactId>
         <version>0.9.0-SNAPSHOT</version>
     </dependency>
    

Reference for this answer. Here's further resources for Maven multiple module projects

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • hmm.. I am bit confused by this answer. When using Eclipse, I normally create a new workspace, then I go to "new project" --> Maven project . Can you modify your answer to adapt to an Eclipse workflow? that would be great! – poseid Apr 26 '16 at 09:50
  • @poseid master project as maven project and other projects as maven modules. I have updated my answer with a reference. – mirmdasif Apr 26 '16 at 10:11
0

install the maven plugin in your eclipse using the following link. This will enable maven in your eclipse. Then right click on your project and click Configure and you will see Convert to Maven Project option. This will convert your existing project to a maven project. You can configure your project to be build as a .jar, .war etc. while converting from the wizard. The following is the tutorial on how to do it... link

Abhishek
  • 2,485
  • 2
  • 18
  • 25