有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Maven和NetBeans将应用程序打包到JPMS模块中?

我的目标是使用jlink将一个应用程序打包成一个模块化的运行时映像,并与一个定制的JRE绑定。我的应用程序是一个简单的“hello world”Java标准版应用程序,依赖于Guava。我使用JDK 11

基本上,我试图重现this tutorial by Baeldung,但使用NetBeans、Maven来管理依赖项,以及模块系统构建的Maven Compiler Plugin版本3.8.1

目录结构:

enter image description here

模块信息。java文件:

module TestwithJLink {
    requires guava;
    exports net.clementlevallois.testwithjlink;
}

控制员。爪哇:

package net.clementlevallois.testwithjlink;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class Controller {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Multiset<String> test = HashMultiset.create();
       test.add("hello");
       test.add("world");
        System.out.println("test: "+ test.toString());
    }

}

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>net.clementlevallois</groupId>
    <artifactId>TestwithJLink</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <id>compile</id>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>

但它创建的是编译类,没有JAR或模块。所以我不能再深入了(先用jdep分析jar的模块,然后再使用jlink)。我肯定错过了一些明显的东西,但什么


共 (1) 个答案

  1. # 1 楼答案

    如果要创建JAR文件,请转到包含pom的根文件夹。在终端中输入xml并键入:

    mvn package
    

    这将在目标文件夹中创建一个JAR。 现在,在terminal to target文件夹中更改路径,并使用以下命令运行JAR文件:

    java -jar {file-name-version}.jar