首页 > 解决方案 > maven:没有主要清单属性

问题描述

我正在开发我的 web java 项目。当我尝试运行由 maven 构建的 java jar 文件时,出现错误:

no main manifest attribute, in "project name".

我认为原因是 maven 找不到我的主要课程。我创建了具有此层次结构的测试项目:

/test
    /src
        /main
             Main.java
    pom.xml

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>FirstProject</groupId>
<artifactId>FirstProject</artifactId>
<version>1.0</version>

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugin</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
        </plugin>
        <plugin>

            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                    <outputDirectory>${basedir}</outputDirectory>
                    <finalName>server</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                 <archive>
                        <manifest>
                            <mainClass>main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
    </plugin>
    </plugins>
</build>

在测试项目中也会出现此错误。我试图用谷歌搜索它,每个人都建议添加 pom.xml,但我已经这样做了......(在我的情况下,这不是一个解决方案)。我怎么解决这个问题?

标签: javamavenpom.xmlmanifest

解决方案


阴影插件更容易使用:

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

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>main.Main</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

推荐阅读