Maven:使用指定主类和属性文件的阴影插件编译项目 [英] Maven: compile a project using shade-plugin specifying main class and properties file

查看:71
本文介绍了Maven:使用指定主类和属性文件的阴影插件编译项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Maven的新手,我试图使用带有Maven-shade-plugin的Maven编译项目(因为这似乎是在那里构建胖子的最佳插件).我试图指定我的主类来制作一个可运行的jar文件和一些包含翻译字符串的.properties文件.

New to Maven, I was trying to compile a project using Maven with maven-shade-plugin (as it seems the best plugin to build a fat jar out there). I tried to specify my main class to make a runnable jar file and some .properties files which contains translation strings.

根据netbeans的输出,编译和构建似乎已通过,但是我不能按以下方式运行它(假设Maven生成的jar被重命名为程序"):

Compilation and build seems passed, according to netbeans output, but I can't run it as following (assuming the jar built by Maven is renamed "program"):

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

这是我的项目文件结构:

this is my project file structure:

这是我的pom.xml文件:

and this is my pom.xml file:

<?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命令运行程序?它说我"jar文件无效或损坏",这意味着该文件不可运行.

Are resources included properly and why can't I run my program using java -jar command without specifying main class? It says me "invalid or corrupt jar file" which this should mean that is not runnable.

此外,为什么既不开始指定Main classpath,又为什么不开始?

Also, why doesn't neither start specifying Main classpath?

推荐答案

问题与您当前配置的Shade插件有关

The issue is with your configuration of the Shade Plugin, which is currently

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

<archive>参数.rel ="nofollow noreferrer"> 目标.您所使用的配置元素不存在的事实这不是错误,该配置将被忽略,这说明了为什么没有在清单中设置您的主类.

There is no <archive> parameter to the shade goal. The fact that you're using a configuration element that is non-existent is not an error, the configuration will just be ignored, and this explains why your main class isn't being set in the Manifest.

要使用Shade插件构建可执行JAR,您需要

To build an executable JAR with the Shade Plugin, you need to provide the ManifestResourceTransformer as a transformers. The correct configuration would be:

<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 设置为,这意味着阴影JAR将作为辅助工件附加到项目.它将通过其分类器launcher与主JAR进行区分,即 shadedClassifierName 参数.

Note that with this configuration, the shaded JAR will not replace the main 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 parameter.

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

After running mvn clean package on this project, you will then have 2 JARs created:

  • mavenproject1-1.0-SNAPSHOT.jar,这是主要的JAR.该JAR仅包含应用程序的已编译Java源代码.它不是可执行文件,并且其中不包含所有依赖项的类.
  • mavenproject1-1.0-SNAPSHOT-launcher.jar是由Shade插件构造的带有阴影的附加JAR.这是可执行文件,包含依赖项的类.
  • mavenproject1-1.0-SNAPSHOT.jar, which is the main JAR. This JAR only consists of the compiled Java sources of your application. It is not executable and does not contain all of the dependencies' classes in it.
  • mavenproject1-1.0-SNAPSHOT-launcher.jar is the shaded attached JAR, which was constructed by the Shade plugin. This one is executable and contains the dependencies' classes.

这意味着,如果要以可执行的JAR形式启动应用程序,则必须使用而不是另一个启动-launcher.jar.

This means that if you want to launch your application as an executable JAR, you have to launch the -launcher.jar, and not the other one, with

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


作为一个旁注,两个JAR都将包含您在<directory>src/main/java/resources</directory>中的资源,因为它们是项目本身的资源,如通过<resource>元素声明的那样.但是,最好遵守标准目录布局,然后将资源放置在src/main/resources中.


As a side-note, both JARs will contain your resources that are in <directory>src/main/java/resources</directory>, because they are resources of the project itself, as declared with the <resource> element. However, it would be preferable to respect the standard directory layout and place the resources in src/main/resources instead.

这篇关于Maven:使用指定主类和属性文件的阴影插件编译项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆