Can `mvn compile` be expected to work with multi-module Maven reactor builds?

561 Views Asked by At

Can mvn compile be expected to work with multi-module Maven reactor builds?

I am trying to work on a project that uses a reactor/multi-module Maven build. When mvn compile failed*, I was told by persons familiar with the project that mvn install is needed, and not mvn compile, because it is a reactor build.

While mvn install indeed works because Maven can then pull the build dependency out of the local repository, it seems wrong to me that I must publish to the repository since doing so can create problems, especially in CI environments.

However, nearly every example I can find surrounding a multi-module project uses mvn clean install, including Maven by Example. I have found no examples of mvn compile that I can attribute to multi-module projects, making me inclined to think there is a Maven limitation/requirement here.

I note that mvn compile did work for me with the example provided in Maven by Example, but that is a relatively simple project so I know I cannot take that to mean it should work in general.

* It failed because it could not find one of the dependencies that was built successfully earlier in the reactor build.

4

There are 4 best solutions below

0
On BEST ANSWER

As indicated in @khmarbaise's comment and demonstrated by the fact it works with the Maven By Example example. mvn compile and mvn test can work with reactor builds.

However, not all plugins work well with this approach. For example, making test-jar's work can require changing the phase.

Using mvn package as suggested by @khmarbaise is likely to give far fewer of these issues while also avoiding the problems caused by using mvn install or mvn deploy.

4
On

If your multi-module project has interdependencies on each other it is actually quite logical that you need to at least install these dependencies to your local repository before others are compile-able.

5
On

You can use --also-make-dependents option. It configures Maven to build the project and its dependents. Actually, the command examines all of the projects in reactor to find projects that depend on a particular project. It will automatically build those projects and nothing else.

$ mvn --projects your-sample-project --also-make-dependents install

Resource Link: Maven Tips and Tricks: Advanced Reactor Options

0
On

I have run into this issue for multi-module projects. For example, I'll have a module called base that every other module depends on, for both normal scope as well as test scope. The test scope dependency is there so I can share common unit test classes and utilities.

The mvn compile will fail with on the test-scoped deps with a message like Could not resolve dependencies for project com.example:zydeco:jar:0.0.1: Failure to find com.example:base:jar:tests:0.0.1

However using mvn test -Dmaven.test.skip.exec=true, which is in spirit the same as compile, and it will find resolve all intra-project test dependencies just fine.

If I run something like mvn dependency:tree, it will fail for regular dependencies with a message like Could not resolve dependencies for project com.example:zydeco:jar:0.0.1: Failure to find com.example:base:jar:0.0.1

I agree with other folks that avoiding package and install is desirable - to avoid having stale code lying around. In the past, I've actually written scripts which create empty/fake jar files so that commands like mvn dependency:build-classpath can work.

I'm not sure there is away around this with maven. It's on my list to look at sbt and gradle.