【问题标题】:Maven : no main manifest attribute, in target/xx.jar, Unable to execute jar fileMaven:没有主要清单属性,在 target/xx.jar 中,无法执行 jar 文件
【发布时间】:2019-08-09 04:43:46
【问题描述】:

这里是 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>dl4j</groupId>
    <artifactId>dl4j</artifactId>
    <version>1.0-SNAPSHOT</version>
    <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>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>example.JavaTestExample</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-core</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>nd4j-native-platform</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-api</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <!--  You need the below dependency to use CodecRecordReader-->
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-data-codec</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <!--       <dependency>
                   <groupId>org.bytedeco.javacpp-presets</groupId>
                   <artifactId>${moduleName}-platform</artifactId>
                   <version>${moduleVersion}-1.4.4</version>
               </dependency>-->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.8.0-beta4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-beta4</version>
        </dependency>
        <!--  You need the below dependency to use LocalTransformExecutor-->
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-local</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
    </dependencies>
    <!-- Uncomment to use snapshot version -->
    <!--<repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>  &lt;!&ndash; Optional, update daily &ndash;&gt;
            </snapshots>
        </repository>
    </repositories>-->
</project>

我想运行 maven-assembly-plugin 并创建一个 jar 文件,其中添加了所有依赖项。当我运行java -jar jarfile.jar 时,出现以下错误:

no main manifest attribute, in target/jarfile.jar

我浏览了几个 Stack Overflow 帖子,并尝试了所有排列和组合。我尝试添加 assemblyshade 插件。您可以在上面的 pom.xml 中看到我的设置。即使我添加了适当的 Maven 设置,我也不确定这是怎么发生的。

我确认 Java 运行良好:

我验证了我是否正确指定了主类。

对我来说一切都很好,除了结果,我现在很困惑。

可能出了什么问题?我希望他们中的一些人仍然将问题标记为“重复”,但我一直在阅读其他帖子并尝试相应地更改设置。他们都没有工作。

【问题讨论】:

    标签: java maven command-line jar


    【解决方案1】:

    高质量的问题!

    我认为这将有助于实现您的目标。

    这是我用来构建一个包含所有依赖项的可点击 JavaFX Jar。 这是使用maven-jar-pluginmaven-shade-plugin 而不是maven-assembly-pluginHere is a good resource on the differences.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>10</release>
                </configuration>
            </plugin>
            <plugin>
                <!-- I think you don't need this one -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>${mainClass}</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>
                                ${mainClass}
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <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.ApacheLicenseResourceTransformer" />
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${mainClass}</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2018-02-10
      相关资源
      最近更新 更多