Java 9 named module

An automatic module is a named module that is defined implicitly, since it does not have a module declaration. An ordinary named module, by contrast, is defined explicitly, with a module declaration; we will henceforth refer to those as explicit modules.

There is no practical way to tell, in advance, which other modules an automatic module might depend upon. After a module graph is resolved, therefore, an automatic module is made to read every other named module, whether automatic or explicit.

Java 9 names module example

Module Structure:

module-maven-example

module-maven-example

module-info.java

Here guava is dependency that consider as automatic module. Need to add require statement in module-info.java as bellow.

Note: Modules name do not allowed – or . character as module name so maven will manage it. At time of require statement no need to specified dependency version.

/**
 * Created by JavaDeveloperZone on 22-04-2017.
 */
module module.maven.demo {
    requires guava;
}

Demo.java

package com.javadeveloperzone;
import com.google.common.base.Optional;
/**
 * Created by JavaDeveloperZone on 22-04-2017.
 */
public class Demo {
    public static void main(String... args) {
        Optional b =  Optional.of(new Integer(10));
        System.out.println(b.get());
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <groupId>module-demo</groupId>
    <artifactId>com.javadeveloperzone</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>false</showDeprecation>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
    </dependencies>
</project>

 

Build

"C:\Program Files\Java\jdk-9\bin\java" -Dmaven.multiModuleProjectDirectory=F:\extrawork\java9-demo\module-maven-thirdparty-library-demo -Xmx2g -XX:ReservedCodeCacheSize=512m "-Dmaven.home=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\lib\idea_rt.jar=49366:C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2017.1\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.1 install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building com.javadeveloperzone 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ com.javadeveloperzone ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ com.javadeveloperzone ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ com.javadeveloperzone ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory F:\extrawork\java9-demo\module-maven-thirdparty-library-demo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:testCompile (default-testCompile) @ com.javadeveloperzone ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ com.javadeveloperzone ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ com.javadeveloperzone ---
[INFO] Building jar: F:\extrawork\java9-demo\module-maven-thirdparty-library-demo\target\com.javadeveloperzone-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ com.javadeveloperzone ---
[INFO] Installing F:\extrawork\java9-demo\module-maven-thirdparty-library-demo\target\com.javadeveloperzone-1.0-SNAPSHOT.jar to C:\Users\Lenovo\.m2\repository\module-demo\com.javadeveloperzone\1.0-SNAPSHOT\com.javadeveloperzone-1.0-SNAPSHOT.jar
[INFO] Installing F:\extrawork\java9-demo\module-maven-thirdparty-library-demo\pom.xml to C:\Users\Lenovo\.m2\repository\module-demo\com.javadeveloperzone\1.0-SNAPSHOT\com.javadeveloperzone-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.010 s
[INFO] Finished at: 2017-04-22T12:01:36+05:30
[INFO] Final Memory: 12M/42M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
Output:
10

 

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *