Impose build order for a multi-project in Maven
Asked Answered
G

2

14

I have a multi project in maven like this:

paren-project
  -> plugin-project
  -> testbed-project

The plugin project generates a JAR, which is manually copied to a specific subdirectory of testbed (using whatever embedded script like or ). The important point: I don't want the plugin JAR to be in the classpath of testbed.

But I cannot found the solution to force the plugin project to be build BEFORE the testbed project. If I set the plugin project as dependency of the testbed project, it is added in the classpath.

Any solution, or do I have to switch to a build system like , or ?

Gyroscope answered 6/7, 2011 at 8:26 Comment(0)
V
23

As it is mentioned at the http://maven.apache.org/guides/mini/guide-multiple-modules.html

Reactor Sorting

Because modules within a multi-module build can depend on each other, 
it is important that The reactor sorts all the projects in a way that 
guarantees any project is built before it is required.

The following relationships are honoured when sorting projects:
  1. project dependency on another module in the build
  2. plugin declaration where the plugin is another modules in the build
  3. plugin dependency on another module in the build
  4. build extension declaration on another module in the build
  5. the order declared in the modules element (if no other rule applies)

Note that only "instantiated" references are used - dependencyManagement and pluginManagement elements will not cause a change to the reactor sort order

Veracity answered 6/7, 2011 at 9:6 Comment(2)
Thank you, I declare no relationship and succeed to build the plugins before the testbed by declaring them before the module testbed.Gyroscope
This deserves some clarification. We've found that rule #5 only applies if the given module order is totally compatible with rules #1-#4. So you cannot use it to say "A before B" unless you also establish a correct global module build order that doesn't violate any dependencies.Aerospace
F
7

Maven is a versatile system. No need to switch.

You can add the dependency like this:

<dependency>
  <groupId>group</groupId> 
  <artifactId>artifact</artifactId> 
  <optional>true</optional> 
</dependency>

This way, the dependency will not be included in the classpath.

Read more about Optional Dependency

Fred answered 6/7, 2011 at 8:43 Comment(4)
Optional is about transitive relationship, no use for this problem.Gyroscope
This worked for me with a slightly different problem. i had Integration tests in one project that output resources to be used in tests by another. i scoped the dependency to test only so that i would include the war in the build. For this purpose this worked great.Microcopy
Kartoch, according to the documentation, the optional dependency does not included in the classpath, but changes the classification of the reactor. then the solution is valid.Juridical
This may have undesirable side-effects, like including the module in a shaded artifact even though it is not a true dependency, although the comment suggesting setting <scope>test</scope> is clever.Aerospace

© 2022 - 2024 — McMap. All rights reserved.