Maven:使用指定 main class 和属性文件的 shade-plugin 编译项目

Maven: compile a project using shade-plugin specifying main class and properties file

Maven 的新手,我试图使用 Maven 和 maven-shade-plugin 编译一个项目(因为它似乎是构建 fat jar 的最佳插件)。我试图指定我的主要 class 来制作一个 运行nable jar 文件和一些包含翻译字符串的 .properties 文件。

编译和构建似乎通过了,根据 netbeans 输出,但我不能运行如下(假设 Maven 构建的 jar 已重命名 "program"):

/usr/bin/java -cp program.jar bot.Main
> could not find or load main class bot.Main

这是我的项目文件结构:

这是我的 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>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
    <resources>
    <resource>
        <directory>src/main/java/resources</directory>
        <includes>
            <include>**/*.properties</include>
        </includes>
    </resource>
</resources>
<plugins>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <configuration>
      <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>bot.Main</mainClass>
      </manifest>
    </archive>
    <shadedArtifactAttached>true</shadedArtifactAttached>
    <shadedClassifierName>launcher</shadedClassifierName>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.2.1</version>
      <executions>
          <execution>
              <goals>
                  <goal>java</goal>
              </goals>
          </execution>
      </executions>
      <configuration>
          <mainClass>bot.Main</mainClass>
      </configuration>
  </plugin>

</plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots</artifactId>
        <version>2.4.0</version>
        <classifier>jar-with-dependencies</classifier>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.5</version>
    </dependency>
</dependencies>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

是否正确包含了资源?为什么我不能 运行 我的程序使用 java -jar 命令而不指定 main class?它说我 "invalid or corrupt jar file" 这应该意味着那不是 运行nable。

另外,为什么不开始指定主class路径?

问题出在您的 Shade 插件配置上,目前

<configuration>
  <archive>
    <manifest>
      <addClasspath>true</addClasspath>
      <classpathPrefix>lib/</classpathPrefix>
      <mainClass>bot.Main</mainClass>
    </manifest>
  </archive>
  <shadedArtifactAttached>true</shadedArtifactAttached>
  <shadedClassifierName>launcher</shadedClassifierName>
</configuration>

shade goal. The fact that you're using a configuration element that is non-existent 没有 <archive> 参数,配置将被忽略,这解释了为什么您的主要 class 没有在清单中设置。

要使用 Shade 插件构建可执行 JAR,您需要 to provide the ManifestResourceTransformer as a transformers。正确的配置是:

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
      <mainClass>bot.Main</mainClass>
    </transformer>
  </transformers>
  <shadedArtifactAttached>true</shadedArtifactAttached>
  <shadedClassifierName>launcher</shadedClassifierName>
</configuration>

请注意,使用此配置,阴影 JAR 不会替换主 JAR。 shadedArtifactAttached is set to true, which means that the shaded JAR will be attached to the project as a secondary artifact. It will be distinguished from the main JAR with its classifier of launcher, i.e. the shadedClassifierName参数。

在此项目上 运行 mvn clean package 之后,您将创建 2 个 JAR:

  • mavenproject1-1.0-SNAPSHOT.jar,这是主要的 JAR。此 JAR 仅包含应用程序的已编译 Java 源代码。它不可执行,并且不包含所有依赖项的 class。
  • mavenproject1-1.0-SNAPSHOT-launcher.jar是shade附加的JAR,由Shade插件构建。这个是可执行的并且包含依赖项的 classes.

这意味着如果您想将应用程序作为可执行 JAR 启动,则必须启动 -launcher.jar,而不是另一个

java -jar mavenproject1-1.0-SNAPSHOT-launcher.jar

作为旁注,两个 JAR 都将包含您在 <directory>src/main/java/resources</directory> 中的资源,因为它们是项目本身的资源,如使用 <resource> 元素声明的那样。但是,最好尊重 standard directory layout 并将资源放在 src/main/resources 中。