简体   繁体   English

maven-jar-plugin 指定mainClass

[英]maven-jar-plugin to specify mainClass

I am trying to run a Java10 "hello world" example from the commandline that I have created using maven.我正在尝试从我使用 maven 创建的命令行运行 Java10“hello world”示例。

But I get the message但我收到消息

Error: Could not find or load main class App in module eu.ngong.mainclass错误:无法在模块 eu.ngong.mainclass 中找到或加载主 class 应用程序

It works with它适用于

mvn exec:java -Dexec.mainClass=eu.ngong.mainclass.App

but not if I run the script但如果我运行脚本则不会

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

The pom.xml is: 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>

If I look into the jar, I can find the App class, as well as the MANIFEST.MF如果我查看 jar,我可以找到 App class,以及 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

What did I miss?我错过了什么?

I pushed the project here我把项目推到了这里

Try updaing the pom with <addClasspath>true</addClasspath> as well in maven-jar-plugin as below under configuration . 尝试用updaing的聚甲醛<addClasspath>true</addClasspath>以及在行家-JAR-插件如下下configuration It cannot correctly find the main class when the class path is not set. 未设置类路径时,无法正确找到主类。

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

mvn clean deletes all compiled classes. mvn clean删除所有已编译的类。

mvn exec:java runs the only the goal java of the plugin exec . mvn exec:java运行插件exec的唯一目标java But it is missing the compiled classes, that you clean ed before.但是它缺少编译的类,你之前clean ed。

mvn test asks maven to run all lifecycles up to the test phase - including the compile lifecycle. mvn test要求 maven 运行所有生命周期直到测试阶段——包括compile生命周期。

mvn compile exec:java would run everything including the compile phase (which should be enough for exec:java goal) and then the exec:java goal. mvn compile exec:java将运行所有内容,包括编译阶段(对于exec:java目标应该足够了),然后是 exec:java 目标。 You can separated it also in two calls.您也可以在两次通话中将其分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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