简体   繁体   中英

No main manifest attribute error - IntelliJ with Maven jar plugin

I'm following a tutorial found at this link ( https://stackabuse.com/web-scraping-the-java-way/ ), but when it's time to package the project and run the jar, I get the "No main manifest attribute error" in the terminal when I use java -jar target/ApartmentHunt-1.0-SNAPSHOT.jar . My pom.xml looks like

<?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 has the main method and is located in src/main/java/org.exmaple and just contains a print statement. The code is exactly the same as the tutorial, but I get an error. I've cloned the repo from the tutorial and it works on my machine, so I'm not sure what is wrong with my project. This is my first time using maven for a project, and I used the maven quickstart archetype.

I've used various assembly plugins from other posts, but it didn't resolve my issue.

check your code main method is in App.java ?

Try it with 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>

and run mvn comand:

> mvn clean
> mvn install

Adding the following plugin to my pom.xml AND running mvn assembly:single fixed the issue. Although I do not understand why I cannot run mvn package to do the same thing when I specified in the plugin to occur during the packaging phase. In addition, I have to mvn compile after any source code changes. Code below

<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>

Follow the instructions in this video .

When you get to the Artifact creation point (Ctrl + Shift + Alt + S) change the location of the manifest as shown here. (\src\main\resources)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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