【问题标题】:No main manifest attribute error - IntelliJ with Maven jar plugin没有主要清单属性错误-带有 Maven jar 插件的 IntelliJ
【发布时间】:2020-12-10 10:44:03
【问题描述】:

我正在学习在此链接 (https://stackabuse.com/web-scraping-the-java-way/) 上找到的教程,但是当我需要打包项目并运行 jar 时,当我使用 @987654322 时,我在终端中收到“No main manifest attribute error” @。我的 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>org.example</groupId>
  <artifactId>ApartmentHunt</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>ApartmentHunt</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.jsoup</groupId>
      <artifactId>jsoup</artifactId>
      <version>1.13.1</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <groupId>org.apache.Maven.plugins</groupId>
          <artifactId>Maven-shade-plugin</artifactId>
          <version>3.2.0</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <transformers>
                  <transformer
                          implementation="org.apache.Maven.plugins.shade.resource.ManifestResourceTransformer">
                    <mainClass>org.example.App</mainClass>
                  </transformer>
                </transformers>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

App.java 有 main 方法,位于 src/main/java/org.exmaple 中,只包含一个打印语句。代码与教程完全相同,但出现错误。我已经从教程中克隆了 repo,它可以在我的机器上运行,所以我不确定我的项目有什么问题。这是我第一次在项目中使用 maven,我使用了 maven quickstart 原型。

我使用了其他帖子中的各种组装插件,但没有解决我的问题。

【问题讨论】:

    标签: java maven intellij-idea jar manifest


    【解决方案1】:

    检查你的代码main方法在App.java中吗?

    【讨论】:

    • 是的,我应该在原始帖子中包含它,但主要方法在 App.java 中
    【解决方案2】:

    用 maven-jar-plugin 试试

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>org.example.App</mainClass>
            </manifest>
        </archive>
    </configuration>
    </plugin>
    

    并运行 mvn 命令:

    > mvn clean
    > mvn install
    

    【讨论】:

      【解决方案3】:

      将以下插件添加到我的 pom.xml 并运行 mvn assembly:single 解决了这个问题。虽然我不明白为什么当我在插件中指定在打包阶段发生时我不能运行mvn package 来做同样的事情。另外,我必须在任何源代码更改后mvn compile。代码如下

      <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                  <archive>
                    <manifest>
                      <mainClass>org.example.App</mainClass>
                    </manifest>
                  </archive>
                  <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                  </descriptorRefs>
                  <appendAssemblyId>false</appendAssemblyId>
                </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>
      

      【讨论】:

        【解决方案4】:

        按照video 中的说明进行操作。

        当您到达工件创建点 (Ctrl + Shift + Alt + S) 时,更改清单的位置,如下所示。 (\src\main\resources)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-14
          • 2019-05-18
          • 2015-01-06
          • 2018-02-10
          • 2019-07-19
          • 2016-10-13
          • 2021-05-29
          • 2015-10-01
          相关资源
          最近更新 更多