参考链接:https://www.cnblogs.com/GarfieldEr007/p/6995525.html
打包一共有三种方式
第一种方法,把所有的jar包都打进一个jar包里面
第二种方法,把jar包和所依赖的jar包分开打包
第三种方法,把所依赖的jar包独立生成到target目录下,运行时手动指定target目录
1.把所有的jar包都打进一个jar包里面
用maven创建一个新工程
主类
package com.ai.oidd;
import org.apache.commons.lang3.ClassUtils;
public class test {
public static void main(String[] args) {
System.out.println(ClassUtils.isPrimitiveOrWrapper(Integer.class)); //true
}
}
Pom配置文件
<?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.ctc</groupId>
<artifactId>mavenTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ai.oidd.test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
接下来使用mvn package进行打包,用java -jar mavenTest-1.0-SNAPSHOT.jar进行运行
2.分开打包
把所有外部依赖的包都打一个包
用maven创建一个新工程
工程名为JarWithDependency
(1)创建一个main类
package com.ai.oidd;
import org.apache.commons.lang3.ClassUtils;
public class Test {
public static void main(String[] args) {
System.out.println(ClassUtils.isPrimitiveOrWrapper(Integer.class)); //true
System.out.println("hello");
}
}
(2)pom配置文件
<?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</groupId>
<artifactId>JarWithDependency</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ai.oidd.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
(3)打包,会有两个jar包
cd进入target目录
java -cp JarWithDependency-1.0-SNAPSHOT-jar-with-dependencies.jar com.ai.oidd.Test
第一个为所依赖的jar包打成的一个jar包,第二个为主类地址
输出结果
3.把引用的jar包引入到classpath里面,以java -cp的方式允许
(1)先创建一个main类
package com.ai.oidd;
import org.apache.commons.lang3.ClassUtils;
public class Test {
public static void main(String[] args) {
System.out.println(ClassUtils.isPrimitiveOrWrapper(Integer.class)); //true
System.out.println("hello");
}
}
(2)pom文件
<?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</groupId>
<artifactId>JarWithDependency</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.ai.oidd.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
外部依赖的包分开来,不会一起打成jar包
cd到指定目录
java -cp JarWithDependency-1.0-SNAPSHOT.jar:commons-lang3-3.9.jar com.ai.oidd.Test