0

I want my ModuleTypeA war to include ModuleBase as a dependency with 'src/main/java/typeA' package Excluded and similarly ModuleTypeB war to include ModuleBase as a dependency with 'src/main/java/typeB' package Excluded.

Here is my project structure:

ModuleBase
   |
   |----> src/main/java/base
   |----> src/main/java/typeA
   |----> src/main/java/typeB
   |----> pom.xml

ModuleTypeA
   |
   |----> src/main/java/..
   |----> pom.xml

ModuleTypeB
   |
   |----> src/main/java/..
   |----> pom.xml

I am new to Maven and not sure how to achieve that. ModuleBase cannot be a parent project as I need to build a jar for that (can't have packaging type pom) so I can create a top level parent project.

Any suggestions are welcome.

Thanks

csn
  • 376
  • 7
  • 18
  • Why do you have code for typeA in your ModuleBase? AFAIK you can only exclude packages at a project level, so ModuleBase has to exclude typeA but then it would be excluded for all projects depending on ModuleBase. But question is why have specific code in the base? – morpheus05 Jan 21 '14 at 21:04
  • The code in ModuleBase is infrastructure related code which I do not want to mix with other application level code in ModuleTypeA. Yes, I am aware of exclusions at project level but that would not serve the purpose here. – csn Jan 21 '14 at 21:08
  • You could move the code from ModuleBase/../typeA to a seperate project and reference it. – morpheus05 Jan 21 '14 at 22:50

3 Answers3

1

First, read this article about dependency exclusions: Optional Dependencies and Dependency Exclusions

If you are building a .war file from ModuleBase, you can make not a dependency but an overlay for ModuleTypeA from ModuleBase and similarly ModuleTypeB war as overlay of ModuleBase. For this, you can use Maven War Plugin: Maven War Plugin

This way, for exclusion of a package use this in you pom.xml:

<excludes>
   <exclude>src/main/java/typeA</exclude>
</excludes>

UPDATE:

I haven't try this yet, but there are two ways possible to exclude a package from .jar.

1) Use Maven Assembly Plugin. Here is an example you can refer to:

Exclude files with maven assembly does not work

2) Use Maven Jar Plugin, check this answer as an example:

maven-jar-plugin Exclusions Failing

Community
  • 1
  • 1
yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • looks like overlays only support war and zip so I have to build ModuleBase as a war file. I am wondering if something is possible is my ModuleBase is built as jar. – csn Jan 21 '14 at 21:23
  • thanks for the information and links. I guess it seems the cleanest solution would be to restructure my project tree as mentioned by @Adrian. – csn Jan 22 '14 at 16:05
1

You could create different assemblies in the ModuleBase and refer to those as system dependecies in ModuleTypeA/B.

You can set the path to a dependency with <systemPath>../module-base/target/rt.jar</systemPath>

Maybe Multiple assemblies from one maven project will help you with the assembly topic.

Community
  • 1
  • 1
Spindizzy
  • 7,244
  • 1
  • 19
  • 33
1

Normally it should look something like this:

proj
  + proj-base
    + src/main/java/   // containing base code
    + pom.xml
  + proj-mod-type-a
    + src/main/java/   // containing module a code
    + pom.xml          // dependency to proj-base
  + proj-mod-type-b
    + src/main/java/   // containing module b code
    + pom.xml          // dependency to proj-base
  + proj-a-web
    + src/main/java    // war A source
    + pom.xml          // dependency to proj-base and proj-mod-type-b
  + proj-b-web
    + src/main/java    // war B source
    + pom.xml          // dependency to proj-base and proj-mod-type-a

In brief, split your project into meaningful modules, each being a unit for dependency. Construct your WAR base on the dependency you need.

If proj-base is in fact a WAR that you want to reuse its content in other WAR, then you may look closer to behavior of WAR overlay in Maven. However, I believe the basic idea is still the same: better modularize your project and have appropriate dependencies when constructing your WAR/EAR

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131