Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[all java] Add Java module support #7424

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apriltag/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ dependencies {
implementation project(':wpimath')
}

apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"
apply from: "${rootDir}/shared/java/opencv-module-patches.gradle"

sourceSets {
main {
resources {
Expand Down
14 changes: 14 additions & 0 deletions apriltag/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.apriltag {
requires transitive wpilib.opencv;
requires wpilib.math;
requires wpilib.util;
requires com.fasterxml.jackson.annotation;
requires com.fasterxml.jackson.databind;

exports edu.wpi.first.apriltag;
exports edu.wpi.first.apriltag.jni;
}
1 change: 1 addition & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ repositories {
}
dependencies {
implementation "edu.wpi.first:native-utils:2025.3.0"
implementation("org.gradlex:extra-java-module-info:1.9")
}
1 change: 1 addition & 0 deletions cameraserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ evaluationDependsOn(':cscore')
evaluationDependsOn(':hal')

apply from: "${rootDir}/shared/javacpp/setupBuild.gradle"
apply from: "${rootDir}/shared/java/opencv-module-patches.gradle"

dependencies {
implementation project(':wpiutil')
Expand Down
13 changes: 13 additions & 0 deletions cameraserver/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.cameraserver {
requires transitive wpilib.opencv;
requires wpilib.cscore;
requires wpilib.ntcore;
requires wpilib.util;

exports edu.wpi.first.cameraserver;
exports edu.wpi.first.vision;
}
1 change: 1 addition & 0 deletions cscore/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (OperatingSystem.current().isMacOsX()) {
}

apply from: "${rootDir}/shared/jni/setupBuild.gradle"
apply from: "${rootDir}/shared/java/opencv-module-patches.gradle"

model {
components {
Expand Down
11 changes: 11 additions & 0 deletions cscore/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.cscore {
requires wpilib.opencv;
requires wpilib.util;

exports edu.wpi.first.cscore;
exports edu.wpi.first.cscore.raw;
}
14 changes: 14 additions & 0 deletions epilogue-processor/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

import javax.annotation.processing.Processor;

module wpilib.epilogue.processor {
requires java.compiler;
requires jdk.compiler;
requires wpilib.epilogue;

provides Processor with
edu.wpi.first.epilogue.processor.AnnotationProcessor;
}
12 changes: 12 additions & 0 deletions epilogue-runtime/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.epilogue {
requires wpilib.ntcore;
requires wpilib.units;
requires wpilib.util;

exports edu.wpi.first.epilogue;
exports edu.wpi.first.epilogue.logging;
}
13 changes: 13 additions & 0 deletions hal/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.hal {
requires wpilib.util;

exports edu.wpi.first.hal;
exports edu.wpi.first.hal.can;
exports edu.wpi.first.hal.communication;
exports edu.wpi.first.hal.simulation;
exports edu.wpi.first.hal.util;
}
9 changes: 9 additions & 0 deletions ntcore/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.ntcore {
requires wpilib.util;

exports edu.wpi.first.networktables;
}
2 changes: 2 additions & 0 deletions romiVendordep/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation project(":wpilibj")
}

apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"

nativeUtils.exportsConfigs {
// Main library is just default empty. This will export everything
romiVendordep {
Expand Down
8 changes: 8 additions & 0 deletions romiVendordep/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.romi {
requires transitive wpilib.wpilibj;
requires wpilib.hal;
}
10 changes: 10 additions & 0 deletions shared/java/javacommon.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'jacoco'
apply plugin: 'org.gradlex.extra-java-module-info'

def baseArtifactId = project.baseId
def artifactGroupId = project.groupId
Expand Down Expand Up @@ -114,6 +115,15 @@ tasks.withType(JavaCompile).configureEach {
"-Xlint:-serial",
// ignore unclaimed annotation warning from annotation processing
"-Xlint:-processing",
"-Xlint:-preview",
// ignore modules requiring automatic library modules
"-Xlint:-requires-automatic",
"-Xlint:-requires-transitive-automatic",
"-Xlint:-exports",
// Java modules warn about exposing classes without an explicitly declared constructor
"-Xlint:-missing-explicit-ctor",
// Java modules warn about module names with trailing digits
"-Xlint:-module"
]
}

Expand Down
7 changes: 7 additions & 0 deletions shared/java/javastyle.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ checkstyle {
config = resources.text.fromFile(new File(configDirectory.get().getAsFile(), "checkstyle.xml"))
}

tasks.withType(Checkstyle) {
// Checkstyle falls over if module-info files are included in the check scope
// The suppressions file does not appear to work for this, so we need to configure the task
// itself
exclude("**/module-info.java")
}

apply plugin: 'pmd'

pmd {
Expand Down
6 changes: 6 additions & 0 deletions shared/java/opencv-module-patches.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Patch legacy non-modularized dependencies to have module info
extraJavaModuleInfo {
module('edu.wpi.first.thirdparty.frc2024.opencv:opencv-java', 'wpilib.opencv') {
exportAllPackages()
}
}
34 changes: 34 additions & 0 deletions shared/java/wpimath-module-patches.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Patch legacy non-modularized dependencies to have module info
extraJavaModuleInfo {
module('org.ejml:ejml-core', 'ejml.core') {
exportAllPackages()
}
module('org.ejml:ejml-simple', 'ejml.simple') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-fsparse', 'ejml.fsparse') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-dsparse', 'ejml.dsparse') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-fdense', 'ejml.fdense') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-ddense', 'ejml.ddense') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-cdense', 'ejml.cdense') {
exportAllPackages()
requiresTransitive('ejml.core')
}
module('org.ejml:ejml-zdense', 'ejml.zdense') {
exportAllPackages()
requiresTransitive('ejml.core')
}
}
2 changes: 2 additions & 0 deletions wpilibNewCommands/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies {
testImplementation 'org.mockito:mockito-core:4.1.0'
}

apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"

sourceSets.main.java.srcDir "${projectDir}/src/generated/main/java"

nativeUtils.exportsConfigs {
Expand Down
14 changes: 14 additions & 0 deletions wpilibNewCommands/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.commands2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open discussion: wpilib.commands or wpilib.commands2? The module names already differ from the package names.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commands2 makes sense if we plan on making a commands3, unless there's some way to version it separately. If we can use vendordep versioning and prevent users from installing two versions at once, we could just use commands.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commands2 makes the most sense. We'll have a commands3 in 2027, and each version is essentially a completely different library so reusing the same module name wouldn't be practical (or likely even possible)

Copy link
Member

@calcmogul calcmogul Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That argument doesn't make sense to me. WPILib 2008 and 2024 look completely different, yet we still call it wpilibj. Major version numbers exist to represent things like major API refactors, so we should be using the major version for that instead of the library name itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because wpilib was changed gradually over time, without multiple incompatible versions concurrently being available for use. Commands v3 will probably be available side by side with v2 during a transition period

Copy link
Member

@calcmogul calcmogul Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the case for most libraries though, and they don't name their libraries like that. Giving them different names would allow users to use them both simultaneously, which we should explicitly disallow. Allowing that for v1 and v2 was a mistake due to the package naming confusion that introduced.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An advantage of the numbered naming was that it made it very clear if classes were from v1 or v2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s actually quite common in Java to have versions in package names, sometimes as a subpackage name (eg commands.v1, commands.v2). The common recommendation however is to not put the version in the class name.

requires wpilib.units;
requires wpilib.wpilibj;
requires wpilib.hal;
requires wpilib.ntcore;

exports edu.wpi.first.wpilibj2.command;
exports edu.wpi.first.wpilibj2.command.button;
exports edu.wpi.first.wpilibj2.command.sysid;
}
3 changes: 3 additions & 0 deletions wpilibj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ dependencies {
devImplementation sourceSets.main.output
}

apply from: "${rootDir}/shared/java/opencv-module-patches.gradle"
apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"

apply plugin: 'cpp'
apply plugin: 'edu.wpi.first.NativeUtils'
apply plugin: ExtraTasks
Expand Down
28 changes: 28 additions & 0 deletions wpilibj/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.wpilibj {
requires ejml.core;
requires ejml.simple;
requires transitive wpilib.math;
requires transitive wpilib.units;
requires transitive wpilib.util;
requires wpilib.hal;
requires wpilib.ntcore;
requires wpilib.cscore;
requires wpilib.cameraserver;

exports edu.wpi.first.wpilibj;
exports edu.wpi.first.wpilibj.counter;
exports edu.wpi.first.wpilibj.drive;
exports edu.wpi.first.wpilibj.event;
exports edu.wpi.first.wpilibj.internal;
exports edu.wpi.first.wpilibj.livewindow;
exports edu.wpi.first.wpilibj.motorcontrol;
exports edu.wpi.first.wpilibj.shuffleboard;
exports edu.wpi.first.wpilibj.simulation;
exports edu.wpi.first.wpilibj.smartdashboard;
exports edu.wpi.first.wpilibj.sysid;
exports edu.wpi.first.wpilibj.util;
}
2 changes: 2 additions & 0 deletions wpimath/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ dependencies {
api "us.hebi.quickbuf:quickbuf-runtime:1.3.3"
}

apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"

sourceSets.main.java.srcDir "${projectDir}/src/generated/main/java"
sourceSets.main.resources.srcDir "${projectDir}/src/main/proto"

Expand Down
46 changes: 46 additions & 0 deletions wpimath/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.math {
requires com.fasterxml.jackson.annotation;
requires ejml.core;
requires ejml.ddense;
requires ejml.simple;
requires us.hebi.quickbuf.runtime;
requires wpilib.units;
requires wpilib.util;

exports edu.wpi.first.math;
exports edu.wpi.first.math.controller;
exports edu.wpi.first.math.controller.proto;
exports edu.wpi.first.math.controller.struct;
exports edu.wpi.first.math.estimator;
exports edu.wpi.first.math.filter;
exports edu.wpi.first.math.geometry;
exports edu.wpi.first.math.geometry.proto;
exports edu.wpi.first.math.geometry.struct;
exports edu.wpi.first.math.interpolation;
exports edu.wpi.first.math.jni; // Probably unnecessary
exports edu.wpi.first.math.kinematics;
exports edu.wpi.first.math.kinematics.proto;
exports edu.wpi.first.math.kinematics.struct;
exports edu.wpi.first.math.optimization;
exports edu.wpi.first.math.path;
exports edu.wpi.first.math.proto;
exports edu.wpi.first.math.spline;
exports edu.wpi.first.math.spline.proto;
exports edu.wpi.first.math.spline.struct;
exports edu.wpi.first.math.struct;
exports edu.wpi.first.math.system;
exports edu.wpi.first.math.system.plant;
exports edu.wpi.first.math.system.plant.proto;
exports edu.wpi.first.math.system.plant.struct;
exports edu.wpi.first.math.system.proto;
exports edu.wpi.first.math.system.struct;
exports edu.wpi.first.math.trajectory;
exports edu.wpi.first.math.trajectory.constraint;
exports edu.wpi.first.math.trajectory.proto;
exports edu.wpi.first.math.util;
exports edu.wpi.first.math.numbers;
}
9 changes: 9 additions & 0 deletions wpinet/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.net {
requires wpilib.util;

exports edu.wpi.first.net;
}
10 changes: 10 additions & 0 deletions wpiunits/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.units {
exports edu.wpi.first.units;
exports edu.wpi.first.units.collections;
exports edu.wpi.first.units.measure;
exports edu.wpi.first.units.mutable;
}
18 changes: 18 additions & 0 deletions wpiutil/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

@SuppressWarnings("requires-transitive-automatic")
module wpilib.util {
requires com.fasterxml.jackson.databind;
requires transitive us.hebi.quickbuf.runtime;

exports edu.wpi.first.util;
exports edu.wpi.first.util.cleanup;
exports edu.wpi.first.util.concurrent;
exports edu.wpi.first.util.datalog;
exports edu.wpi.first.util.function;
exports edu.wpi.first.util.protobuf;
exports edu.wpi.first.util.sendable;
exports edu.wpi.first.util.struct;
}
2 changes: 2 additions & 0 deletions xrpVendordep/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
implementation project(":wpilibj")
}

apply from: "${rootDir}/shared/java/wpimath-module-patches.gradle"

nativeUtils.exportsConfigs {
// Main library is just default empty. This will export everything
xrpVendordep {
Expand Down
8 changes: 8 additions & 0 deletions xrpVendordep/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

module wpilib.xrp {
requires transitive wpilib.wpilibj;
requires wpilib.hal;
}
Loading