Search code examples
javamavenpom.xmlm2eclipse

How to organise my maven project


I'm sure that this has been answered elsewhere, but I'm not sure if I'm understanding fully how to organise my project hierarchy.

So I've got a project that I'm working on, it holds lots of utilities that I use in various other projects.

lets call the main part of the project MyProject.org.utils

Underneath this I have a logical separation of parts for things such as - Logging and 'custom exceptions' - Calendar / time functions - string functions.

I'm using eclipse and have created each of these as 'packages' underneath MyProject.org.utils.

I want to be able to do 2 things for this project.

I would like to be able to release a single jar for each sub part, so one jar with all the stuff for the logging and another for calendar functions.

This would mean If I don't need to import these into another project I don't need to (particularly for my logging stuff, which I use in all my other projects, but often I don't use the calendar stuff).

I'm sure that this can be done in my Maven pom.xml but I'm not sure how to set it up for this.

I've read about parent poms, but I'm not convinced I understand how to get this to work in my situation, as I have only a single pom.xml in my workspace.

I would like to keep all of these parts within the same project in my eclipse workspace.

This is the current tree setup for this project, which I think helps better helps explain what I want to do (trimmed for brevity):

├── log4j2.xml
├── pom.xml
├── README.md
├── src
│   ├── main
│   │   ├── java
│   │   │   └── myproject
│   │   │       └── org
│   │   │           └── utils
│   │   │               ├── calendar
│   │   │               ├── logging
│   │   └── test
│   │       ├── java
│   │       │   └── myproject
│   │       │       └── org
│   │       │           └── utils
│   │       │               ├── calendar
│   │       │               ├── logging
│   │       └── resources
│   └── target
│       ├── classes
│
│...

So essentially I want a jar for :
myproject.org.utils.calendar
myproject.org.utils.logging

but how to modify my pom.XML to get this.

I think I need to have a parent pom.xml, but where do I then put the child pom.xml files for the 2 jars that I want to create?

Or am I going about this in the wrong way? Short examples and links to tutorials, etc. would be perfect,
Thanks in advance for your assistance.

David


Solution

  • In general, you should create maven project (aka pom.xml) for each calendar and logging stuff. The packaging of the poms should have the default value jar. Note, that each of the projects (calendar and logging) has a separated physical location and there is no a common src folder, like in your snippet. This does not affect the name of packages or jars and you can use the common prefix for them.

    I recommend to create the parent maven project for them and include calendar and logging projects as modules into the parent pom. The parent pom packaging should be pom. There is no target for the parent pom - since jars are produces by the modules. Parent pom makes easier managing dependencies, versions, plugins etc.

    All that may be easily defined with eclipse (File->New->Maven->Maven project etc.)

    You can see working example on git. Probably it worth to read maven manual for getting background.

    Just for others who may come along, this is the better process. - create the initial maven project (using eclipse new -> maven project, or the command line). - to create the child package you should select new -> Maven module

    This will result in a file structure of the following

    ├── myproject.org.utils.calendar
    │   ├── src
    |   │   ├── main
    |   │   └── test
    │   └── target
    ├── src
    │   ├── main
    |   │   ├── main
    |   |   │   └── java
    |   |   |   │   └── myproject.org.utils.calendar_old
    

    You will notice how in the above tree the newly created maven module (myproject.org.utils.calendar) site in the root of the main project, rather than how a typical eclipse new->java->package sits under src\main\java.

    This new module (myproject.org.utils.calendar) contains its own pom.xml and the outer 'parent' project now has this as part of the a <modules> section in its pom.xml