详解如何使用maven生成可以执行的jar
不依赖任何外界包,maven如何生成可以执行的jar?
pom中不包含任何引用的情况下,只需要在pom中添加 maven-jar-plugin
即可。
解决过程
新建项目,仅包含一个main函数类:Xixi.java,输出 Xixi Say: hello
pom中配置为
<groupId>com.paxi</groupId> <artifactId>xixi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>xixi</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
执行 mvn clean install
生成jar包 xixi-1.0-SNAPSHOT.jar,
执行jar命令,执行反馈出错
java -jar xixi-1.0-SNAPSHOT.jar xixi-1.0-SNAPSHOT.jar中没有主清单属性
对jar进行解压缩jar xvf xixi-1.0-SNAPSHOT.jar查看对应文件MANIFEST.MF
cat META-INF/MANIFEST.MF Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Built-By: paxi Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_121
在pom中添加插件 maven-jar-plugin
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <mainClass>com.paxi.Xixi</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
执行成功输出
java -jar xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
再对解压缩后的MANIFEST.MF文件查看为
cat META-INF/MANIFEST.MF Manifest-Version: 1.0 Built-By: paxi Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_121 Main-Class: com.paxi.Xixi //新增
在pom中添加依赖,maven如何生成可执行jar?
需要使用插件maven-dependency-plugin 和 maven-jar-plugin。
解决过程
在pom中仅仅增加一个apache-commons包
<groupId>com.paxi</groupId> <artifactId>xixi</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>xixi</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> </dependencies>
在代码中运用common包中的一个类
public static void main(String[] args) { String words = "Xixi Say: hello"; if (StringUtils.isNotBlank(words)) { System.out.println(words); } }
打包后直接运行出错
java -jar xixi-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at com.paxi.Xixi.main(Xixi.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
在pom中增加插件
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.paxi.Xixi</mainClass> <classpathPrefix>libs/</classpathPrefix> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/libs </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
再次运行
java -jar xixi-1.0-SNAPSHOT.jar Xixi Say: hello
MANIFEST.MF文件为
cat META-INF/MANIFEST.MF Manifest-Version: 1.0 Built-By: paxi Class-Path: libs/commons-lang3-3.7.jar Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_121 Main-Class: com.paxi.Xixi
不仅使用了POM中的依赖还使用了三方的jar,maven如何生成可执行jar?
编译过程需要对编译插件 maven-compiler-plugin
做修改,运行的时候需要加上三方jar的目录,执行命令
java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jar Xixi Say: hello Papa say:hello
编译解决过程
测试代码为
public static void main(String[] args) { String words = "Xixi Say: hello"; if (StringUtils.isNotBlank(words)) { System.out.println(words); } Papa.say(); }
使用mvn命令执行报错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xixi: Compilation failure
xixi/src/main/java/com/paxi/Xixi.java:[15,9] 找不到符号
pom中添加编译插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <compilerArgs> <arg>-Djava.ext.dirs=lib/</arg> </compilerArgs> </configuration> </plugin>
打包执行
java -jar target/xixi-1.0-SNAPSHOT.jar Xixi Say: hello Exception in thread "main" java.lang.NoClassDefFoundError: com/paxi/Papa at com.paxi.Xixi.main(Xixi.java:15) Caused by: java.lang.ClassNotFoundException: com.paxi.Papa at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
加上参数后即可得出结果
java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jar Xixi Say: hello Papa say:hello
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。