Skip to content

gradlex-org/java-module-dependencies-maven-extension

Repository files navigation

Java Module Dependencies Maven Extension

Build Status Maven Central

A Maven Extension to make Maven use dependencies from module-info.java files automatically. If you have a project that fully uses Java Modules, you do not need to declare dependencies in the <dependencies> section anymore. Maven will use the information from your module-info.java directly.

To manage the versions of Java Modules, you can use the <dependencyManagement> section in a (parent) POM and/or importe BOMs.

This Maven Extension – which is part of the GradleX project – is maintained by me, Jendrik Johannes. I offer consulting and training for Gradle, Maven and/or the Java Module System - please reach out if you are interested. There is also my YouTube channel on Java modularity and build topics.

If you have a suggestion or a question, please open an issue.

There is a CHANGELOG.md.

Note

This Maven Extension is derived from its Gradle counterpart. It offers the same fundamental functionality, but does not have all the convenience features of the Gradle plugin. Some of these are not directly transferable to Maven. However, if you think something from the Gradle plugin could be transferred here in a useful way for your Maven use case, please open an issue.

How to use?

Working example projects to inspect:

Register the extension

Add this to one of your parent POMs:

<build>
  <extensions>
    <extension>
      <groupId>org.gradlex</groupId>
      <artifactId>java-module-dependencies-maven-extension</artifactId>
      <version>0.2</version>
    </extension>
  </extensions>
</build>

Effect of the extension

Once the extension is registered, dependencies are automatically determined based on the requires directives in your module-info.java files. For example:

module org.example.mymodule {
  requires transitive org.slf4j; // translates to:
    // <dependency>
    //   <groupId>org.slf4j</groupId>
    //   <artifactId>slf4j-api</artifactId>
    //   <scope>compile</scope>
    // </dependency>
  requires com.fasterxml.jackson.core; // translates to:
    // <dependency>
    //   <groupId>com.fasterxml.jackson.core</groupId>
    //   <artifactId>jackson-core</artifactId>
    //   <scope>compile</scope> <!-- non-transitive -->
    // </dependency>
  requires static jakarta.servlet; // translates to:
    // <dependency>
    //   <groupId>jakarta.servlet</groupId>
    //   <artifactId>jakarta.servlet-api</artifactId>
    //   <scope>provided</scope>
    // </dependency>
}

Add Module Name mapping information (if needed)

You may define additional mappings from Module Name to group:name (GA) coordinates.

The plugin already knows about Modules available on Maven Central. The information is stored in:

You define additional entries (or overwrite entries from the plugin) in a .mvn/modules.properties file in your project:

org.apache.commons.lang3=org.apache.commons:commons-lang3

Define Module versions in dependency management sections

Use <dependencyManagement> entries to define versions for the modules you depend on. In a multi-project it is recommended to do that in a parent POM. You may also manage them separately in a BOM that you import (or use an existing BOM).

<dependencyManagement>
  <dependencies>
    <!-- individual version, e.g. for module 'org.slf4j' -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.36</version>
    </dependency>
    <!-- import versions from BOM, e.g. spring-boot-dependencies -->
    <!-- has versions for many widely used modules -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.3.0.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Disclaimer

Gradle and the Gradle logo are trademarks of Gradle, Inc. The GradleX project is not endorsed by, affiliated with, or associated with Gradle or Gradle, Inc. in any way.

About

A Maven extension to use dependencies from 'module-info.java' files.

Topics

Resources

License

Stars

Watchers

Forks