46
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | Introduction to Modular Development CON5118 Alan Bateman Java Platform Group, Oracle October 2015

Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Introduction to Modular DevelopmentCON5118

Alan BatemanJava Platform Group, OracleOctober 2015

Page 2: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Sessions

Prepare for JDK 9

Introduction to Modular Development

Advanced Modular Development

Project Jigsaw: Under the Hood

Project Jigsaw Hack Session

2

2

3

1

4

5

Page 3: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

What is a module?

3

com.foo.bar

Page 4: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

What is a module?

4

com.foo.bar

com.foo.bar.alpha.Alphacom.foo.bar.alpha.AlphaFactorycom.foo.bar.beta.Betacom.foo.bar.beta.BetaBlocker:

Page 5: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |5

module com.foo.bar { }

module-info.javacom/foo/bar/alpha/Alpha.javacom/foo/bar/alpha/AlphaFactory.javacom/foo/bar/beta/Beta.javacom/foo/bar/beta/BetaBlocker.java:

Page 6: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |6

module com.foo.bar { requires com.foo.baz;} com.foo.bar

com.foo.baz

Page 7: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |7

module com.foo.app { requires com.foo.bar; requires java.sql}

com.foo.app

com.foo.bar java.sql

Page 8: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |8

com.foo.app

com.foo.bar

java.sql

com.foo.baz

java.base

Page 9: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |9

com.foo.app

com.foo.bar

java.sql

com.foo.bazjava.loggingjava.xml

java.base

Page 10: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |10

com.foo.app

java.logging

java.sql

Page 11: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |11

com.foo.app

java.logging

java.sql

package java.sql;import java.util.logging.Logger;

public class Driver { public Logger getParentLogger() { .. } :}

Driver d = …d.getParentLogger().log(…)

Page 12: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |12

com.foo.app

java.logging

java.sql

Page 13: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |13

com.foo.app

java.logging

java.sqlmodule java.sql { requires public java.logging; :}

Page 14: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |14

com.foo.app

com.foo.bar

java.sql

com.foo.bazjava.loggingjava.xml

java.base

Page 15: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |15

module com.foo.bar { exports com.foo.bar.alpha; exports com.foo.bar.beta;}

com.foo.bar.alphacom.foo.bar.beta

com.foo.bar.internal

com.foo.bar

Page 16: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |16

com.foo.app com.foo.bar

Accessibility

reads

Page 17: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |17

com.foo.app com.foo.bar

Accessibility

Page 18: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 18

public ⇏ accessible

Page 19: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Platform modules

19

Page 21: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 21

Compilation$ javac -d mods/com.foo.baz \ src/com.foo.baz/module-info.java \ src/com.foo.baz/com/foo/baz/Bazooka.java \ :

src/com.foo.baz/module-info.javasrc/com.foo.baz/com/foo/baz/Bazooka.java

mods/com.foo.baz/module-info.classmods/com.foo.baz/com/foo/baz/Bazooka.class

Page 22: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 22

$ java -modulepath dir1:dir2:dir3 …

module path

Page 23: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

src/com.foo.bar/module-info.javasrc/com.foo.bar/com/foo/bar/alpha/Alpha.java:

23

Compilation$ javac -modulepath mods -d mods/com.foo.bar \ src/com.foo.bar/module-info.java \ src/com.foo.bar/com/foo/bar/alpha/Alpha.class \ :

mods/com.foo.bar/module-info.classmods/com.foo.bar/com/foo/bar/bar/Alpha.class:

Page 24: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 24

$ java -modulepath mods -m com.foo.app/com.foo.app.MainGreetings from module com.foo.app!

module name main class

Page 25: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |25

com.foo.app

com.foo.bar

java.sql

java.base

Page 26: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |26

com.foo.app

com.foo.bar

java.sql

com.foo.bazjava.loggingjava.xml

java.base

Page 27: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27

[Resolve] Root module com.foo.app located[Resolve] (file:///d/mods/com.foo.app/)[Resolve] Module com.foo.bar located, required by com.foo.app[Resolve] (file:///d/mods/com.foo.bar/)[Resolve] Module java.base located, required by com.foo.app[Resolve] (jrt:/java.base)[Resolve] Module java.sql located, required by com.foo.app[Resolve] (jrt:/java.sql)[Resolve] Module com.foo.baz located, required by com.foo.bar[Resolve] (file:///d/mods/com.foo.baz/)[Resolve] Module java.logging located, required by java.sql[Resolve] (jrt:/java.logging)[Resolve] Module java.xml located, required by java.sql[Resolve] (jrt:/java.xml)[Resolve] Resolve completed[Resolve] com.foo.app[Resolve] com.foo.bar[Resolve] com.foo.baz[Resolve] java.base[Resolve] java.logging[Resolve] java.sql[Resolve] java.xmlGreetings from module com.foo.app!

$ java -Xdiag:resolver -mp mods -m com.foo.app/com.foo.app.Main

Page 28: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 28

Packaging as modular JAR

$ jar --create --file mlib/app.jar \ --main-class com.foo.app.Main \ -C mods/com.foo.app .

mods/com.foo.app/module-info.classmods/com.foo.app/com/foo/app/Main.class:

module-info.classcom/foo/app/Main.class:

app.jar

Page 29: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 29

$ jar --file mlib/app.jar -p

Name: com.foo.appRequires: com.foo.bar java.base [ MANDATED ] java.sqlMain class: com.foo.app.Main

Page 30: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 30

$ java -mp mlib:mods -m com.foo.appGreetings from module com.foo.app!

Page 31: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 31

classpath

$ java -cp mlib/baz.jar:app.jar \ com.foo.app.Main

module-info.classcom/foo/baz/Bazooka.class:

com.foo.baz (mlib/baz.jar)

Page 32: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 32

module path

$ java -mp mlib -m myappmodule-info.classcom/foo/baz/Bazooka.class:

com.foo.baz (mlib/baz.jar)

Page 33: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 33

classpath and module path

$ java -mp mlib -addmods com.foo.baz \ -cp app.jar com.foo.app.Main

Page 34: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 34

Linking

jlink

modular run-timeimage

bin conf …

Page 35: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 35

Linking

$ jlink --modulepath $JDKMODS --addmods java.base --output myimage

$ myimage/bin/java [email protected]

Page 36: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 36

Linking

$ jlink --modulepath $JDKMODS:$MYMODS --addmods com.foo.app --output myimage

$ myimage/bin/java [email protected]@[email protected]@9.0com.foo.appcom.foo.barcom.foo.baz

Page 37: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Summary

• Introduced basic concepts• Introduced basic command lines to compile, run, package and link

37

Page 38: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Other sessions, this room

• Advanced Modular Development @ 2.30pm• Project Jigsaw: Under the Hood @ 5.30pm• Project Jigsaw Hack Session @ Tuesday 8.30am

38

Page 39: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 39

More Information

OpenJDK Project Jigsaw page, this has links to all the JEPs

http://openjdk.java.net/projects/jigsaw/

mailto:[email protected]

Early Access Builds

https://jdk9.java.net/jigsaw/

Page 40: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

Safe Harbor StatementThe preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

40

Page 41: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]
Page 42: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |

src/com.foo.app/module-info.javasrc/com.foo.app/com/foo/app/Main/javasrc/com.foo.bar/module-info.javasrc/com.foo.bar/com/foo/bar/alpha/Alpha.javasrc/com.foo.baz/module-info.javasrc/com.foo.baz/com/foo/baz/Bazooka.java

:

42

Multi-module compilation

$ javac -modulesourcepath src -d mods $(find src -name “*.java”)

mods/com.foo.app/module-info.classmods/com.foo.app/com/foo/app/Main.classmods/com.foo.baz/module-info.classmods/com.foo.baz/com/foo/baz/Bazooka.classmods/com.foo.bar/module-info.classmods/com.foo.bar/com/foo/bar/bar/Alpha.class:

Page 43: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 43

Testing

$ javac -cp lib/junit.jar -mp mods -d testclasses \ test/com/foo/bar/alpha/test/AlphaTest.java

$ java -cp lib/junit.jar:testclasses -mp mods -addmods com.foo.bar \ org.junit.runner.JUnitCore com.foo.bar.alpha.test.AlphaTest

Page 44: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 44

Testingcom.foo.bar

exportedpackages

notexported

testclasses

testclasses

Page 45: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 45

Testing

com.foo.bar

test classes

junit.jar

org.junit.Assertorg.junit.Test:

-XaddReads

-Xmodule/-Xpatch

Page 46: Introduction to Modular Developmentopenjdk.java.net/projects/jigsaw/talks/intro-modular-dev-j1-2015.pdfCopyright © 2015, Oracle and/or its affiliates. All rights reserved. | 27 [Resolve]

Copyright © 2015, Oracle and/or its affiliates. All rights reserved. | 46

Testing

$ javac -cp lib/junit.jar -mp mods -d testmods/com.foo.bar -Xmodule:com.foo.bar \ test/com.foo.bar/com/foo/bar/alpha/AlphaTest.java

$ java -cp lib/junit.jar -mp mods -addmods com.foo.bar \ -Xpatch:testmods -XaddReads:com.foo.bar=ALL-UNNAMED \ org.junit.runner.JUnitCore com.foo.bar.alpha.AlphaTest