Skip to main content
Entwickler Themen
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Image Creation

Erstellung von Images

Für die Weitergabe braucht man das übersetzte Programm und oft eine kurze Installationshinweis. Das Image entsteht in zwei Schritten:

  1. Alle nötigen Dateien bereitstellen: übersetzte Klassen, Ressourcen, Abhängigkeiten.
  2. Daraus eine Verzeichnisstruktur mit einem startbaren Binary bauen.

Aufruf

Profil image, Phase install, zuvor clean, damit überschriebene Dateien nicht stören:

mvnw -Pimage clean install

Beschreibung des image Profils

Das Profil

        <profile>
            <id>image</id>
            <activation>
                <property>
                    <name>image</name>
                </property>
            </activation>
            <build>
                <plugins>

                <!-- ... -->

                </plugins>
            </build>
        </profile>
  • Das Profil hat eine Id: image. Damit ist es möglich, das Profil über -Pimage auszuwählen.
  • Durch die activation Property ist es zusätzlich möglich, das Profil auch über ein Define auszuwählen: -Dimage
  • In dem Profil selbst sind dann Plugins untergebracht.

maven-dependency-plugin

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>${maven.dependency.plugin}</version>
                        <executions>
                            <!-- erstmal Abhängigkeiten für den Class-Path kopieren -->
                            <execution>
                                <id>copy-dependencies</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>copy-dependencies</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/modules</outputDirectory>
                                    <includeScope>runtime</includeScope>
                                    <overWriteReleases>false</overWriteReleases>
                                    <overWriteSnapshots>false</overWriteSnapshots>
                                    <overWriteIfNewer>true</overWriteIfNewer>
                                </configuration>
                            </execution>

                            <!-- dazu noch das Projekt-JAR -->
                            <execution>
                                <id>copy</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <configuration>
                                    <outputDirectory>${project.build.directory}/modules</outputDirectory>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>${project.groupId}</groupId>
                                            <artifactId>${project.artifactId}</artifactId>
                                            <version>${project.version}</version>
                                            <type>${project.packaging}</type>
                                            <destFileName>${project.build.finalName}.jar</destFileName>
                                        </artifactItem>
                                    </artifactItems>
                                    <overWriteIfNewer>true</overWriteIfNewer>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

Das maven-dependency-plugin kopiert zuerst die Laufzeitabhängigkeiten, dann das Projekt-JAR nach ${project.build.directory}/modules.

copy-dependencies

Läuft in der Phase package, Goal copy-dependencies. includeScope ist runtime. Releases und Snapshots werden nicht überschrieben, neuere Dateien schon (overWriteIfNewer).

copy project artifact

Läuft in der Phase install, Goal copy. Das gebaute Artefakt landet unter dem Namen ${project.build.finalName}.jar im selben modules-Verzeichnis.

jpackage-maven-plugin

                    <plugin>
                        <groupId>com.github.akman</groupId>
                        <artifactId>jpackage-maven-plugin</artifactId>
                        <version>${jpackage.maven.plugin}</version>
                        <configuration>
                            <name>${appName}</name>
                            <type>IMAGE</type>
                            <modulepath>
                                <dependencysets>
                                    <dependencyset>
                                        <includenames>
                                            <includename>javafx\..*</includename>
                                        </includenames>
                                    </dependencyset>
                                </dependencysets>
                            </modulepath>
                            <addmodules>
                                <addmodule>javafx.controls</addmodule>
                                <addmodule>javafx.graphics</addmodule>
                                <addmodule>javafx.fxml</addmodule>
                                <addmodule>javafx.web</addmodule>
                            </addmodules>
                            <mainclass>${main.class}</mainclass>
                            <input>${project.build.directory}/modules</input>
                            <mainjar>${jar.filename}.jar</mainjar>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>jpackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

Das jpackage-maven-plugin erzeugt in der Phase install ein Image (type IMAGE) inklusive Java-Runtime. input zeigt auf das modules-Verzeichnis, mainjar und mainclass sind der Einstieg. addmodules zieht die benötigten JavaFX-Module hinzu.