poseid
poseid

Reputation: 7156

How to organize local dependencies with Maven?

in my company, I have a Java project:

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?

Upvotes: 1

Views: 930

Answers (3)

mirmdasif
mirmdasif

Reputation: 6354

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

Upvotes: 1

Abhishek
Abhishek

Reputation: 2525

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

Upvotes: 0

Dmitry
Dmitry

Reputation: 884

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?

Upvotes: 1

Related Questions