Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 to Java 9 migration optimal way for mavenised project

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.

like image 892
Programmer Avatar asked Jul 24 '18 07:07

Programmer


1 Answers

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.

like image 84
Nicolai Parlog Avatar answered Sep 28 '22 06:09

Nicolai Parlog