Maven的新手,我试图用maven-shade-plugin编译一个项目(因为它似乎是构建一个胖 jar 的最佳插件) . 我试图指定我的主类来制作一个可运行的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命令运行我的程序而不指定主类?它告诉我“无效或损坏的jar文件”,这应该意味着它不可运行 .
另外,为什么不开始指定Main classpath?
1 回答
问题在于您目前正在配置Shade插件
shade goal没有
<archive>
参数 . 事实上,您使用的配置元素不存在is not an error,配置将被忽略,这就解释了为什么您的主类没有在Manifest中设置 .要使用Shade插件构建可执行JAR,需要to provide the ManifestResourceTransformer作为transformers . 正确的配置是:
请注意,使用此配置,着色的JAR不会替换主JAR . shadedArtifactAttached设置为
true
,这意味着着色的JAR将作为辅助工件附加到项目 . 它将与主JAR区分开来,其分类为launcher
,即shadedClassifierName参数 .在此项目上运行
mvn clean package
之后,您将创建2个JAR:mavenproject1-1.0-SNAPSHOT.jar
,这是主要的JAR . 此JAR仅包含应用程序的已编译Java源 . 它不可执行,并且不包含其中的所有依赖项类 .mavenproject1-1.0-SNAPSHOT-launcher.jar
是阴影附加的JAR,由Shade插件构建 . 这个是可执行的,包含依赖项的类 .这意味着如果要将应用程序作为可执行JAR启动,则必须启动
-launcher.jar
而不是另一个,作为旁注,两个JAR都将包含
<directory>src/main/java/resources</directory>
中的资源,因为它们是项目本身的资源,如<resource>
元素所声明的那样 . 但是,最好是尊重standard directory layout并将资源放在src/main/resources
中 .