1

I have a project with the following structure:

parent
 > core
 > module-1 (with dependencies: A, B)
 > module-2 (with dependencies: X, Y)
 > project  

module-1 and module-2 provide the same functionality (data access) in different environments. Module project depends on core, and either module-1 or module-2.

Problem: If I make project dependent on both module-1 and module-2, there are a lot of unnecessary transitive dependencies (e.g. I don't need X or Y when deploying to enviroment #1, because only module-1 is used there).

I would like to fail the build as early as possible if both module-1 and module-2 are specified as dependencies of project.

I wrote a test that looks for each module, and fails if both are found, but I think there should be an easier / more direct way to do that that.

Javier
  • 12,100
  • 5
  • 46
  • 57

2 Answers2

5

You could use different profiles for your different environments. For a more extensive answer take a look at Different dependencies for different build profiles in maven

Community
  • 1
  • 1
Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
  • I think that I was trying to re-invent profiles (well... actually I cannot *enforce* the exclusion of unnecessary modules through profiles, but that is not important since I can define a profile for each set of dependencies). Thank you. – Javier Feb 26 '14 at 18:39
2

This is a very odd case. The following may, or may not work, but I think it's a worthy idea, if you're willing to knock up some code, if you have to:

  • Take module-1 and module-2 out into separate projects instead of having them as <modules/>.

  • Check out the maven-enforcer-plugin and enforcer rules. You will probably have to write your own rule.

  • Add module-1 and module-2 as 'optional` dependencies.

Alternatively, you can clone project into two separate projects, remove all the code to another module and add that new module as dependency of project-1 and project-2. Frankly, this is more of a code structuring and refactoring issue, than it is an actual Maven problem.

(And, in theory, Stephen C is right).

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Thank you. I will consider the enforcer plugin for other assertions. As for this problem, I think it is overkill, since all I wanted was to be sure that I was building with a single set of dependencies. BTW, if the "modules" were separate projects I think I could enforce a rule through banned dependencies (`exclude module-*, include module-${module}`), but that doesn't work if the modules belong to the same project. – Javier Feb 26 '14 at 18:38