Java with Maven: How to make Module B depend on jar of module A?

Kaspar Johannes Schneider :

Context:

The question is regarding a multi-module (maven, not Java 9 modules) Java project using maven for building it, where one module depends on the jar of another module.

Overview of the modules and some context:

  • A: a library compiled to a jar.
  • B: a client application, that depends on jar of A; compiled to an executable jar.
  • C: a server application, neither depending on A nor B; compiled to an executable jar.
  • I can only use Java 8 and thus no Java 9 modules.
  • The IDE used is IntelliJ IDEA Ultimate.

What works:

  • Building the modules to three separate jars (executable in case of B and C) using mvn clean package.
  • Importing the packages from module A into the classes of module B (not what I want).
  • Adding a dependency to module A into the pom.xml of B (I don't know what effect this has...).

What I would like to achieve:

  • I would like to add a dependency to the jar built from module A to the pom.xml of module B.
  • I would like to import public classes and interfaces from the jar of module A in the classes of module B (by adding it as a dependency in the pom.xml and without copying the jar manually). -> Development and debugging of B should only depend on the last stable build of A and should not break with simultaneous development on the library A.
  • The whole build process should be completed by executing one Bash script (once).
  • Building the project should do the following steps in order:
    1. Build module A to a jar (if unavoidable also build B).
    2. Build (or rebuild) module B using the jar generated in step 1. to an executable jar.
    3. Build module C at any time during the build process.

Is there an elegant solution using maven for doing this?

JF Meier :

In Maven, you have generally two choices:

  1. Define three projects A, B and C.
  2. Define one multi-module project that contains A, B and C as modules.

In either case, you describe dependencies by using Maven dependencies in the pom.xml.

Multi-module projects are usually build in one go, so compiling all the code and using the same version number for all three modules. This would violate your idea of "building against the last stable version" because you would always depend on the newest version of A when you build.

If you define three different projects, you don't have these restrictions (and C seems to be independent anyway). Two problems remain: You need to update the version of A in the dependency - which can be done with the versions plugin. And you want to construct a "one-click build". The easiest approach would probably be a pipeline in Jenkins (or whatever your build server is), but you can also write a shell script that calls Maven thrice.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134032&siteId=1