Gson: Move module-info.java to /src/main/java

Created on 9 May 2018  路  13Comments  路  Source: google/gson

To build gson as a JPMS module, the module-info.java file needs to be in src/main/java, not in src/main/java/com.google.gson.

I checked https://guides.gradle.org/building-java-9-modules/ for confirmation.

With that change, and with these changes in build.gradle, the JAR builds fine as a modular JAR.

sourceCompatibility = 9
targetCompatibility = 9
// sourceSets.main.java.exclude("**/module-info.java")

Most helpful comment

馃憢 Does anyone know when this fix is anticipated to be available in a release?

All 13 comments

Yeah, today I lost some time to figure out why I had to require gson instead of require com.google.gson (as specified in the module-info.java file).

As it stands right now, I don't believe the module-info.java file is actually used.

That's normal--_you_ don't use the module-info.java. But if _I_ want to use gson as a modular JAR because my app is modular, it needs to be in the right place.

@cayhorstmann you are almost right I think, except for the Gradle part, since this project seems to use both Maven and Gradle for build automation.

I made this work locally by doing the following:

  • Move module-info.java to src/main/java (in the gson module directory)
  • Set the java.version property for the gson-parent pom from 1.6 to 9.
  • Set the biz.aQute.bnd plugin's version from 3.1.0 to 4.0.0.

Then after running

mvn clean install -DskipTests=true

from the root of the project I could require the requires com.google.gson module in my project. For gson to work, you also need to open up your module, so gson's reflection magic can do its work:

module com.internetofdrums.api.web {

    requires com.google.gson;

    opens com.internetofdrums.api.web.view to com.google.gson;
}

I can now even use jlink to make a trimmed image of my app, which was my goal in the first place.

I'm gonna find out how to make a pull request for this.

I've create a pull request: https://github.com/google/gson/pull/1401

This solution targets Java 9 and up, I'll come up with a fix to support 1.6 and up later.

It's possible to target an older version of Java (e.g. 1.6) while also providing module information: https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html

Okay, another pull request, this time ensuring backwards compatibility up until Java 1.6: https://github.com/google/gson/pull/1402

Will this fix be released soon? I had to update my JavaFX projects to Java 11 because of the removal of FX out of the JDK and the old program therefore no longer executing on the newer JDK. But now I'm unable to package it using jlink because Gson 2.8.5 isn't modular yet.

馃憢 Does anyone know when this fix is anticipated to be available in a release?

So will it be a thing one day or not?
Still no modular jar on maven.

You could build it yourself... I waited for it for like 6 months, and I needed it because of jlink.

This was resolved almost 1 year ago yet there is still no release. Considering we're on a stable Java 12 release and some running Java 14 pre-builds and modularity should have been completely adopted by Java 11, can we please at least get a release addressing this issue? Even if it's a minor release, 2.8.5.1. For projects where automatic modules aren't an option (jlink for instance), it's an inconvenience.

The solution is easy. Do not use Gson, or anything else from Google.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ominfowave picture ominfowave  路  13Comments

GoogleCodeExporter picture GoogleCodeExporter  路  31Comments

GoogleCodeExporter picture GoogleCodeExporter  路  14Comments

GoogleCodeExporter picture GoogleCodeExporter  路  32Comments

GoogleCodeExporter picture GoogleCodeExporter  路  20Comments