java - 使用 NetBeans 11.0 生成 .JAR 文件

标签 java netbeans jar executable-jar netbeans-11

我正在尝试使用 NetBeans 11.0 生成 (.JAR) 文件,我遵循了一些答案,例如 How to create a Jar file in Netbeans但我没有在 Build 下找到 Packaging,我也尝试过 Clean & Build Project 但我找不到 /dist 文件夹。

谁能帮帮我

日志:

Scanning for projects...

------------------------------------------------------------------------
Building FlickerURLMaker 1.0-SNAPSHOT
------------------------------------------------------------------------

--- maven-clean-plugin:2.5:clean (default-clean) @ FlickerURLMaker ---
Deleting C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\target

--- maven-resources-plugin:2.6:resources (default-resources) @ FlickerURLMaker ---
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\src\main\resources

--- maven-compiler-plugin:3.1:compile (default-compile) @ FlickerURLMaker ---
Changes detected - recompiling the module!
Compiling 2 source files to C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\target\classes

--- maven-resources-plugin:2.6:testResources (default-testResources) @ FlickerURLMaker ---
Using 'UTF-8' encoding to copy filtered resources.
skip non existing resourceDirectory C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\src\test\resources

--- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ FlickerURLMaker ---
Nothing to compile - all classes are up to date

--- maven-surefire-plugin:2.12.4:test (default-test) @ FlickerURLMaker ---
No tests to run.

--- maven-jar-plugin:2.4:jar (default-jar) @ FlickerURLMaker ---
Building jar: C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\target\FlickerURLMaker-1.0-SNAPSHOT.jar

--- maven-install-plugin:2.4:install (default-install) @ FlickerURLMaker ---
Installing C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\target\FlickerURLMaker-1.0-SNAPSHOT.jar to C:\Users\USER\.m2\repository\maa\FlickerURLMaker\1.0-SNAPSHOT\FlickerURLMaker-1.0-SNAPSHOT.jar
Installing C:\Users\USER\Documents\NetBeansProjects\FlickerURLMaker\pom.xml to C:\Users\USER\.m2\repository\maa\FlickerURLMaker\1.0-SNAPSHOT\FlickerURLMaker-1.0-SNAPSHOT.pom
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 2.613 s
Finished at: 2019-06-26T10:26:33+01:00
Final Memory: 16M/170M
------------------------------------------------------------------------

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>maa</groupId>
    <artifactId>FlickerURLMaker</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <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>

enter image description here

结构:

enter image description here

最佳答案

我看到这是一个 maven 项目。在 Maven 项目中,构建 jar 文件将位于 target 文件夹中。但是,如果您使用任何依赖项,它不会自动将它们附加到构建 jar 中。您需要将以下代码添加到 pom.xml 文件中。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>fully.qualified.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

通常此目标与自动执行的构建阶段相关联。因此,当您构建项目时,您的 fat.jar 文件将在您的目标文件夹中可用。

所以在你的情况下,你的 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>maa</groupId>
    <artifactId>FlickerURLMaker</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <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>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>maa.flickerurlmaker.URLMaker</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

关于java - 使用 NetBeans 11.0 生成 .JAR 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56770394/

相关文章:

java - 使用ScalaTest测试包含@Autowired和私有(private)字段的java

Java 错误 - 需要 .class

java - 尝试查看转化时出现垃圾邮件

netbeans - 如何增加 NetBeans IDE 的字体大小/字体系列? (不是文本编辑器的字体)

android - java.lang.NoClassDefFoundError : com. facebook.android.Facebook

java - 如何在库存条形图上绘制十字准线

mysql - 为什么 Hibernate saveOrUpdate 对于阿拉伯语数据失败?

java - 调用另一个类中的方法来设置标签文本(不要使用netbeans的默认内容)

java - 无法使用 Eclipse 更改 jFrame 图标/加载 .jar 的资源

java - 如何加载与当前类不在同一个 jar 存档中的类?