Maven:没有主要的清单属性,在target / xx.jar中,无法执行jar文件

问题描述 投票:1回答:1

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

我经历了几个stackoverflow帖子,并尝试了所有的排列和组合。我尝试添加assemblyshade插件。您可以在上面的pom.xml中看到我的设置。即使我添加了正确的maven设置,我也不确定这是怎么回事。

验证java运行正常:enter image description here

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

除了结果之外,一切看起来都不错,我现在很困惑。任何人都可以对究竟出了什么问题嗤之以鼻吗?我希望他们中的一些人仍然将问题标记为“重复”,但我花了2天时间阅读其他帖子并尝试相应地更改设置。他们都没有工作。感谢您对此特定案例的建议或输入。谢谢!

java maven command-line jar
1个回答
1
投票

质量好的问题!

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

这就是我用来构建一个包含所有依赖项的可点击JavaFX Jar的方法。这是使用maven-jar-pluginmaven-shade-plugin而不是maven-assembly-plugin; Here 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>
© www.soinside.com 2019 - 2024. All rights reserved.