简体   繁体   English

Maven不会创建可运行的jar

[英]Maven won't create runnable jars

My pom.xml looks like this 我的pom.xml看起来像这样

...
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <archive>
            <manifest>
              <mainClass>application.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  ...

but although I specified the main class, on clean package install maven only creates jars that aren't runnable. 但是尽管我指定了主类,但在clean package install maven仅创建了无法运行的jar。

How can I fix that? 我该如何解决? The manifest then looks like this: 清单如下所示:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Matthias
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_121

Instead of maven compiler plugin, you should configure the maven jar plugin to create an executable jar : 您应该配置maven jar插件来创建可执行jar,而不是maven编译器插件:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>              
      <archive>
        <manifest>          
            <mainClass>application.Main</mainClass>
        </manifest>
      </archive>
    </configuration>
</plugin>

To declare the source/target java version you could use : 要声明源/目标Java版本,可以使用:

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

instead of declaring the compiler plugin. 而不是声明编译器插件。
This is less verbose and it produces exactly the same result. 这不太冗长,并且产生的结果完全相同。

Besides, if you want to include dependency jars of your application inside your executable jar, you should favor the maven assembly plugin over the maven jar plugin : 此外,如果要在可执行jar中包含应用程序的依赖jar,则应优先使用maven程序集插件而不是maven jar插件:

<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>application.Main</mainClass>
        </manifest>
      </archive>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

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

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