Modular Maven project with Java10 modular does not find the java module

2

I am adapting the exercise of the book "Java 9 - Interactive, reactive and modularized - author Rodrigo Turini" for a modular maven project with modular java 10. But when running, you are not encountering the java project modules

~/AmbienteDeDesenvolvimento/Projetos/Cursos/Java9/bookstore $ java -jar app/target/app-1.0.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: br/com/casadocodigo/http/Books
    at br.com.casadocodigo.MainMavenModulo.main(MainMavenModulo.java:17)
Caused by: java.lang.ClassNotFoundException: br.com.casadocodigo.http.Books
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    ... 1 more

parent project pom

<?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>br.com.casadocodigo</groupId>
  <artifactId>bookstore</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>



  <modules>

    <module>domain</module>
    <module>http</module>
    <module>nf</module>
    <module>app</module>

  </modules>

  <properties>
    <java.version>10</java.version>
    <start-class>br.com.casadocodigo.MainModulo</start-class>
    <maven-compiler-pluging.version>3.7.0</maven-compiler-pluging.version>
    <start-class>br.com.casadocodigo.MainMavenModulo</start-class>
  </properties>


  <build>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-pluging.version}</version>
            <configuration>
                <source>10</source>
                <target>10</target>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>

            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>6.1.1</version> 
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
              <configuration>
                <archive>
                  <manifest>
                    <mainClass>${start-class}</mainClass>
                  </manifest>
               </archive>
             </configuration>
        </plugin>

     </plugins>
  </build>

</project>

project pom domain

<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>br.com.casadocodigo</groupId>
    <version>1.0.0</version>
    <artifactId>bookstore</artifactId>
  </parent>

  <artifactId>domain</artifactId>
  <packaging>jar</packaging>

</project>

http project pom

<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>br.com.casadocodigo</groupId>
    <version>1.0.0</version>
    <artifactId>bookstore</artifactId>
  </parent>

  <artifactId>http</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
        <groupId>br.com.casadocodigo</groupId>
        <artifactId>domain</artifactId>
        <version>1.0.0</version>
    </dependency>

  </dependencies>   

</project>

nf project pom

<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>br.com.casadocodigo</groupId>
    <version>1.0.0</version>
    <artifactId>bookstore</artifactId>
  </parent>

  <artifactId>nf</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
        <groupId>br.com.casadocodigo</groupId>
        <artifactId>domain</artifactId>
        <version>1.0.0</version>
    </dependency>
  </dependencies>

</project>

app project pom

<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>br.com.casadocodigo</groupId>
    <version>1.0.0</version>
    <artifactId>bookstore</artifactId>
  </parent>

  <artifactId>app</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
        <groupId>br.com.casadocodigo</groupId>
        <artifactId>domain</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>br.com.casadocodigo</groupId>
        <artifactId>http</artifactId>
        <version>1.0.0</version>
    </dependency>

    <dependency>
        <groupId>br.com.casadocodigo</groupId>
        <artifactId>nf</artifactId>
        <version>1.0.0</version>
    </dependency>

  </dependencies>

</project>

Each project has its module-info.java See below.

module-info domain

module domain {
    exports br.com.casadocodigo.domain;
}

module-info http

module http {
    exports br.com.casadocodigo.http;

    requires domain;
    requires jdk.incubator.httpclient;
}

module-info nf

module nf {
    exports br.com.casadocodigo.service;
    exports br.com.casadocodigo.model;

    requires domain;
}

module-info app

module app {
    exports br.com.casadocodigo;

    requires domain;
    requires http;
    requires nf;
}

continues

For those who want to see the code in its entirety, the same is in my github link

    
asked by anonymous 02.06.2018 / 22:47

1 answer

1

The ideal way to run using modules (according to the java command) is to use:

ou  java [options] -m <module>[/<mainclass>] [args...]
    java [options] --module <module>[/<mainclass>] [args...]
         (para executar a classe principal em um módulo)

In this case, I put all the jars in the same directory, and executed:

java --module-path /home/marcos/diretorio-com-todas-as-jars -m app/br.com.casadocodigo.MainMavenModulo

The --module-path option defines the location of the modules:

--module-path <caminho do módulo>...
              Uma lista separada por : de diretórios, cada um
              sendo um diretório de módulos.
    
03.06.2018 / 00:12