0

I have a single java project that contains 3 different classes that each has a main(). I have a single pom.xml file for the entire project.

I would like Maven to make a jar for each of those classes with their dependencies.

How can I do that??

Shvalb
  • 1,835
  • 2
  • 30
  • 60

3 Answers3

2

If you have three main classes that are each a separate application, then in all likelihood, you should have four jars: one for each main class and one for the code that's shared between them. Maven is very tightly bound to the idea of "one artifact per module", and if you try to go against that, you're heading down a dark and dangerous path. The natural way to express this structure in Maven is in five or six modules:

  1. The root module of your project, which aggregates all other modules. This wouldn't be a jar-producing module. Its packaging would be "pom".
  2. (Optional) A "parent" submodule, which all artifact-producing modules inherit from. You can use this module to establish common dependency versions, set repositories that are needed by all modules, etc. This would also have packaging set to "pom". It's optional because the aggregator module at the root of the project can also fill this role, but like in code, separation of concerns is good. This topic is covered by "POM Best Practices" in the Sonatype book.
  3. A submodule containing all of the code which is shared between your three main classes.

And 4-6: a submodule for each of the main classes along with supporting code and configuration specific to that main class/application.

On the file system, this solution would look like:

<project folder>
  - pom.xml
  - parent-module
    - pom.xml
  - common
    - pom.xml
  - app1
    - pom.xml
  - app2
    - pom.xml
  - app3
    - pom.xml

Then you'd use something like the assembly plugin or appassembler to create distributions with appropriate configurations, libraries, and startup scripts all packaged together.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
0

You could do this with profiles. Here You have example (click).

tostao
  • 2,803
  • 4
  • 38
  • 61
0

You really shouldn't do that. Please read this, which also links to another SO post.

Community
  • 1
  • 1
Larry Shatzer
  • 3,579
  • 8
  • 29
  • 36
  • Thank you for the warning - I will try to see if I can split the project into 3 small projects. – Shvalb Feb 18 '13 at 06:25