maven-jar-plugin指定mainClass

时间:2018-09-19 23:36:09

标签: java maven jigsaw

我正在尝试从使用maven创建的命令行运行Java10“ hello world”示例。

但是我收到消息了

  

错误:在模块eu.ngong.mainclass中找不到或加载主类App

它与

一起使用
mvn exec:java -Dexec.mainClass=eu.ngong.mainclass.App

但是如果我运行脚本则不会

p=~/.m2/repository/eu/ngong/mainclass/1.0.0
java -p $p -m eu.ngong.mainclass.App

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>eu.ngong</groupId>
<artifactId>mainclass</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>mainclass</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <pluginManagement>
        <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>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>eu.ngong.mainclass.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

如果我看着罐子,我可以找到App类以及MANIFEST.MF

Manifest-Version: 1.0
Created-By: Apache Maven 3.5.4
Built-By: rsc
Build-Jdk: 10.0.2
Main-Class: eu.ngong.mainclass.App

我想念什么?

我推动了项目here

1 个答案:

答案 0 :(得分:1)

尝试使用<addClasspath>true</addClasspath>和下面的configuration下的maven-jar-plugin更新pom。未设置类路径时,无法正确找到主类。

 <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <mainClass>eu.ngong.mainclass.App</mainClass>
        </manifest>
      </archive>