简体   繁体   中英

Java Modules, Gradle project - Problems with external libraries

I have a multi module project in IntelliJ , a java library project, using Gradle with quite old versions of libraries I must say, and now switching to Java 11 (using right now OpenJDK 11.0.2 from https://jdk.java.net/archive/ ).

I want to modularize that library project, adding to all modules a module-info.java . I keep getting an error in one of the modules with one of the dependencies, Saxon-HE .

I isolated that module in a separate project (using Gradle 7.6 ), and modified the build.gradle dependencies step by step as IntelliJ discovered import errors, while using the latest versions of the dependencies.

The build.gradle of the project looks like this:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

The build.gradle of the module looks like this up to the point with the error with Saxon-HE .

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
    implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6.1'
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
    implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.10.0'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

test {
    useJUnitPlatform()
}

The module-info.java looks like this, I used the recommendations of IntelliJ so it added automatically the requires statements:

module mymodule1 {
    requires org.apache.commons.lang3;
    requires org.apache.commons.io;
    requires org.slf4j;
    requires okhttp3;
    requires okio;
    requires java.xml;
    requires org.jdom2;
}

The next import error IntelliJ discovers while building it results because one of my classes has the import statement:

import net.sf.saxon.xpath.XPathFactoryImpl;

Building the project results in the error:

error: package net.sf.saxon.xpath does not exist
import net.sf.saxon.xpath.XPathFactoryImpl;

Looking at https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/11.4 I added to build.gradle of the module:

implementation group: 'net.sf.saxon', name: 'Saxon-HE', version: '11.4'

In IntelliJ I can see in the Project navigator view, in External Libraries that there is

Gradle: net.sf.saxon.Saxon-HE:11.4 
Saxon-HE-11.4.jar

IntelliJ recommends

Add 'requires Saxon.HE' directive to module-info.java

The module-info.java looks now like this:

module mymodule1 {
    requires org.apache.commons.lang3;
    requires org.apache.commons.io;
    requires org.slf4j;
    requires okhttp3;
    requires okio;
    requires java.xml;
    requires org.jdom2;
    requires Saxon.HE;
}

After that the error in the particular class using that import statement is gone, IntelliJ doesn't complain.

But when then building the project I get the error

C:\Users\ME\PROJECTS\myproject\mymodule1\src\main\java\module-info.java:9: error: module not found: Saxon.HE
        requires Saxon.HE;
                      ^

Removing the requires Saxon.HE and building the project results in the error:

error: package net.sf.saxon.xpath is not visible
import net.sf.saxon.xpath.XPathFactoryImpl;
                   ^
  (package net.sf.saxon.xpath is declared in the unnamed module, but module net.sf.saxon.xpath does not read it)

I find this error message weired, because it says but module net.sf.saxon.xpath does not read it , I would rather expect but module mymodule1 does not read it.

I don't know what's going wrong, other external dependencies are not problematic but Saxon-HE is.

I found here Gradle build - add module path a snippet which might solved it, but maybe only partially, so not sure if this answer can be marked as the solution.

I added to the project build.gradle

subprojects {
    apply plugin: 'java'

    sourceCompatibility = 19
    targetCompatibility = 19

    compileJava {
        doFirst {
            options.compilerArgs += [
                    '--module-path', classpath.asPath,
                    '-Xmaxerrs', 1000
            ]
            classpath = files()
        }
    }
}

Now trying to build it (with Gradle 7.6 and OpenJDK 19 ), it doesn't complain to not finding modules, so far at least, but now I have the next problem which I desribed here Java Modules, Gradle, external dependencies - Modules reading from more then one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM