1 IDE:IDEA打包jar
1.0 新建Maven项目
参考博客:Maven部署JavaWeb
1.2 配置
说明:若要在输出文件夹显示使用的第三方包,则选择copy to ...
,将第三方包暴露在输出文件夹中,若只需要一个jar包(将使用的jar封装为一个jar包),选择extract to ...
。
1.3 运行
cd /home/xdq/xinPrj/java/javaTest/JavaFunctionTest/out/artifacts/JavaFunctionTest_jar
java -jar JavaFunctionTest.jar
1.4 打包scala为jar
添加pom.xml
文件,按照如上方式打包即可。
<properties>
<spark.version>2.1.1</spark.version>
<scala.version>2.11</scala.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
2 maven命令打包jar
2.1 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>java.function.test</groupId>
<artifactId>JavaFunctionTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<finalName>JavaFunctionTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>basic.datatype.datatest.OutputTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.2 打包jar
mnv package
2.3 运行jar
java -jar JavaFunctionTest.jar
3 小坑
(1) 找不到主类
建立jar的方式错误,使用Maven Package直接建立;
(2) 运行jar包出错
Error: Invalid or corrupt jarfile JavaFunctionTest.jar
META-INF路径配置错误,默认路径不对,应该放在项目根路径下.
(3) 若要在输出文件夹显示使用的第三方包,则选择copy to ...
,将第三方包暴露在输出文件夹中,若只需要一个jar包(将使用的jar封装为一个jar包),选择extract to ...
。。
(4) mvn命令打包,缺少Main-Class,需要添加插件:
<build>
<finalName>JavaFunctionTest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>basic.datatype.datatest.OutputTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
[参考文献]
[1]https://blog.csdn.net/linfeng_csdn/article/details/81396030
[2]https://blog.csdn.net/qq_31914787/article/details/88064557
[3]https://www.cnblogs.com/flyToDreamJava/p/8991201.html
[4]https://www.cnblogs.com/654wangzai321/p/9513488.html
[5]https://blog.csdn.net/h70614959/article/details/41697189/