如何使用 Maven 和 Netbeans 创建可执行 jar 文件

How to create a executable jar file using Maven and Netbeans

我正在尝试创建一个可运行的 jar 文件,但不知何故我的 Maven/Netbeans 无法创建。这是我的 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>de.ksw</groupId>
    <artifactId>KBSE-CDI-Testprogram</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <version>1.8.1</version>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>KBSE-CDI</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>maven</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>1.8.1</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

如您所见,我已经尝试添加主 class。我还添加了依赖maven-jar-plugins,但是当我尝试编译项目时仍然收到错误消息:

Plugin org.apache.maven.plugins:maven-jar-plugin:1.8.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-jar-plugin:jar:1.8.1: Failure to find org.apache.maven.plugins:maven-jar-plugin:pom:1.8.1 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

我在这里做错了什么?如何解决这个问题?

没有 maven-jar-plugin 的版本 1.8.1

此处列出了 maven-jar-plugin 的所有可用版本:https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-jar-plugin

您必须将 version 更新到更早的版本。

最新版本示例:

<version>3.0.2</version>

我在一个项目中使用的是maven-jar-plugin和maven-assembly-plugin

结果是一个可执行的 fat-jar。

<artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>de.ksw.kbse.cdi.testprogram.App</mainClass>
                    </manifest>
                    <manifestEntries>                      
                        <!--this one is to add the version information in the manifest. This is something that I need but you can ignore it-->  
                        <version>${project.version}</version>
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>${project.artifactId}</finalName>
                <appendAssemblyId>false</appendAssemblyId> 
                <classifier>DEV</classifier>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- to inheritance merges -->
                    <phase>package</phase> <!-- just to use in the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <!--This plugin is used to have just one jar instead of 2-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.1</version>
            <configuration>
                <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                <finalName>${project.artifactId}</finalName>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <verify>false</verify>
            </configuration>
        </plugin>

在此之后只需点击 netbeans 中的清理和构建按钮并搜索内容

with assembly file: PATH_TO_JAR/KBSE-CDI-Testprogram.jar