Java 8 to Java 9 migration optimal way for mavenised project
Asked Answered
G

1

8

I am migrating my project from Java 8 to Java 9. My project is mavenised. Now for migrating to Java 9, I am planning on creating a separate module directory where all the required dependency of a module will go.

For doing this through maven the only way I know is to use the copy plugin of maven to copy all the required dependency in the module directory. So after running maven installs for a module, the dependent jars will be copied in repository folder(which is by default) and also copied to this module directory folder.

So there will be a copy of the jars and also hard coding in pom.xml for copying specific dependency in the module directory.

This approach doesn't seem to be clean, is there any way out were automatically maven can read my module-info.java file and copy the required dependency not in the classpath but in the specified directory

Here is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.aa.bb</groupId>
        <artifactId>cc</artifactId>
        <version>0.0.1</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <artifactId>dd</artifactId>
    <name>dd</name>
    <groupId>com.aa.cc</groupId>
    <version>1.0.0</version>
    <properties>

            <maven.compiler.source>10</maven.compiler.source>
            <maven.compiler.target>10</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

    </dependencies>
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <release>10</release>
                <compilerArgs>
                    <arg>--module-path</arg>
                    <arg>./moduledir</arg>
                </compilerArgs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>6.2</version> 
            </dependency>
        </dependencies>
    </plugin>       
    </plugins>
</build>

</project>

module-info.java

module com.some_module_name {

    requires com.fasterxml.jackson.core;
    requires com.fasterxml.jackson.databind;
    requires org.apache.commons.codec;
    requires spring.beans;
    requires spring.context;

}

After adding module-info.java to an existing java 10 module and running maven i am getting issue like "package exist is used in module a and module b".

What i believe is while running maven it is looking for module dependency in the .m2/repository and there are loads of jar there as the m2./repository is common for my multiple modules.

So what i was planning to do is create a separate module directory for each module and place the required jar for that module in it which way it even works.

Grovel answered 24/7, 2018 at 7:36 Comment(9)
First please show your pom files and your code furthermore why do like to have separate module directory ? Why do you think you need to copying specific deps in a module directory ? Why do you think Maven should read the module-info file? It sounds as you might misunderstand the concept of module-info / pom.xml file ?Pearly
why do you think you can't trust maven to do dependency management for you and put the artifacts where they belong?Geesey
Note: Java 9 is out of support since March 2018 already. Java 9 should just be "skipped"... it breaks a ton of stuff. It's easier to just go for Java 10, whose support will end in september, at which point migrating to Java 11 (which is a LTS release) shouldn't be a problem. In any case: don't consider Java 9 at all, go for Java 10.Rusel
@Geesey : so does maven makes .m2/repository as automatic module . I am not able to figure out what is the module-dir maven uses by default to resolve module required dependencyGrovel
@GiacomoAlzetta : Sure I will go with java 10 but still the same problem will presistGrovel
@GiacomoAlzetta: I disagree with your advice. Skipping Java 9 and going directly from Java 8 to Java 10 makes migration (slightly) more complicated because then you're facing Java 9's and Java 10's challenges in the same migration without knowing where any particular problem comes from. I definitely recommend to do it version by version.Trammel
@Grovel The module requirements will be resolved based on the given dependencies in the pom file. If everything is fine Ok..if something is missing you will see it...and no The cache ($HOME/.m2/repository) is not been taken as automatic modules cause it would make not sense to do so...Apart from that it requires that the given deps are correct modules...Pearly
@Nicolai Except Java 10 fixes tons of issue with modules that Java 9 has, hence making the migration easier.Rusel
@GiacomoAlzetta Really? I didn't know that. Can you list a few, preferably with links to issues or JEPs.Trammel
T
6

I assume you're making this effort to make sure Maven "does the right thing" regarding the module and class path, i.e. placing direct dependencies of your module on the former and all other dependencies on the latter. If that's so, there's nothing you need to do - from version 3.7 on, the Maven Compiler Plugin does it for you as soon as you add a module-info.java to your src/main/java directory.

You can verify that by running Maven in debug mode with mvn clean compile -X. When you carefully analyze the output, you will see which JARs end up on which path.

Trammel answered 24/7, 2018 at 15:11 Comment(3)
You can also use --show-module-resolution to figure out the modules resolved in the module path.Bohs
@Nicolai +1 was not aware of that :| that's excellent news. P.S. funny how you have no gold badges, noticed just now, even if you desire a few of themVibraculum
After checking through mvn clean compile -x option , i could see that maven by default add all the jars in the .m2/repository which qualifies as module in the module-path , that way we did not to explicitly mention the module-path . Also after some googling this is the logic maven uses to qualify a jar as a module Maven provides the jar to JDK ModuleFinder If the ModuleFinder considers the Jar as a module, Maven adds the Jar to --module-path If the ModuleFinder does not consider the Jar as a module Maven adds it to -classpathGrovel

© 2022 - 2024 — McMap. All rights reserved.