What do I need to build JDK 9 project with non-modular dependencies using Maven

对着背影说爱祢 提交于 2019-12-04 01:06:37

Including few of the recent updates, I would try and answer this.

UPDATE

  • Java 9 was released publically on 21.09.2017.
  • The minimum compatible version of maven-compiler-plugin in today's date is 3.7.0.

    As already shared by @Tunaki on how you can configure it to build compatible versions for both JDK 1.5 to 8 and JDK 9.

Assuming from the question and comments, you are already aware of automatic modules and placing all the module dependencies on the module path.

Can I tell Maven to put some artifacts on classpath if I know they are not modularized? So that I can avoid the automatic module and the need to declare them?

For artifacts specified in the maven pom <dependencies> and the ones which are as well not included in the module-info.java of the current module are ultimately left behind to be accessed from the classpath in form of an unnamed module.

If I stick with automatic module, can I tell Maven to somehow transitively allow anything my dependency needs to bring in? E.g. other parts of Weld, CDI API etc.

No, since the automatic modules do not consist of an explicitly declared module-info.java, there is no such way of defining requires transitive for any transitive dependency that might be required by the present module.

From one of my past experiences, any Maven transitive dependency as detailed in the dependency:tree that is needed at compile time by the module would have to be explicitly defined using requires in the module-info of the current project.

What is the actual reason, why I need to state, that my project requires modules, which I do not use directly? E.g. weld.environment.common**

  • You do not need to specify requires for modules that are not required at compile or runtime by your module.

  • In case there is a dependency that is not required at runtime but required at compile time by your project, you would need to make sure that you define such module using requires static in your declarations.

    I would have preferably practiced a similar approach at present as well, as stated in the answer to should I keep or remove declared dependencies that are also transitive dependencies?

I think this may help to tell maven to use java 9 :

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>
    </plugins>
</build>

If you specify a module-info.java file, you will need to declare all the java modules except java.base you need as you are now using jigsaw.

Using mvn dependency:tree can help you in populating this but you are not required to use module-info.java in Java 9.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!