简体   繁体   English

胖 JAR 不起作用。 “没有主要清单属性”。 尝试了 POM 文件,仍然失败

[英]Fat JAR not working. "No Main Manifest Attribute". Tried POM file, still failing

I have a Netbeans project with Maven that I'm trying to compile into an executable JAR file.我有一个带有 Maven 的 Netbeans 项目,我试图将它编译成一个可执行的 JAR 文件。 I think so far everything works fine inside Netbeans however when I package it was a 20kb file-SNAPSHOT and couldnt get it to run.我认为到目前为止在 Netbeans 中一切正常,但是当我打包它是一个 20kb 的文件 SNAPSHOT 并且无法运行时。
Someone pointed me to some MAVEN code to package all dependencies and make it into a "FAT JAR" file.有人向我指出了一些 MAVEN 代码来打包所有依赖项并将其放入“FAT JAR”文件中。 I did that and started getting an error "No Main Manifest Attribute" I copied some more MAVEN snippets to add the main manifest however I still get the same error "No Main Manifest" so I assume I did something wrong.我这样做并开始收到错误“没有主清单属性”我复制了更多的 MAVEN 片段来添加主清单但是我仍然收到相同的错误“没有主清单”所以我认为我做错了什么。 Here's my POM.XML file:这是我的 POM.XML 文件:

<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>com.MyCompany</groupId>
    <artifactId>Client_BoxTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
    <dependency>
   <groupId>com.fazecast</groupId>
   <artifactId>jSerialComm</artifactId>
   <version>[2.0.0,3.0.0)</version>
</dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
        <build>
        <finalName>Client-Boxtest</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
            <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <classpathPrefix>lib/</classpathPrefix>
                      <mainClass>my.client_boxtest.BoxTestUI</mainClass>
                    </manifest>
                  </archive>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>

                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>

                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
    
            </plugin>
        </plugins>
    </build>
</project>

Now I am not sure about the location of my main class: it's a fairly simple program and my tree in NetBeans looks like this, with only one file:现在我不确定我的主类的位置:这是一个相当简单的程序,我在 NetBeans 中的树看起来像这样,只有一个文件:

Netbeans 中的文件树

The maven-shade-plugin is much more powerful for create an Uber jar or fat jar and is the preferred plugin for this task for over ten years. maven-shade-plugin 对于创建 Uber jar 或 fat jar 来说功能更强大,并且是该任务十多年来的首选插件。 https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html describes how you should specify the mainClass. https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html描述了您应该如何指定 mainClass。

Try to add a reference to the main class in your POM as described below, it works for me:尝试在 POM 中添加对主类的引用,如下所述,它对我有用:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          [...]
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        [...]
      </plugin>
      [...]
</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM