首先看看工程目录结构
如图 (ReadLogByThread 为 MainClass):
方法一: mvn clean assembly:assembly
下面是 pom.xml 内容:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.test</groupId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>testkafka</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 在这里定义你的入口类 -->
<mainClass>ReadLogByThread</mainClass>
</properties>
<artifactId>${mainClass}</artifactId>
<dependencies>
<!-- 在这里添加你的依赖 -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.10.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 你也可以在这里自己写MainClass -->
<mainClass>${groupId}.${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
在项目的目录下执行 mvn clean assembly:assembly
然后会在项目下生成 target 文件夹,并有两个jar包,一个包含依赖,一个不包含
方法二: mvn clean package (assembly方式)
下面是 pom.xml 内容:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.test</groupId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>testkafka</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>ReadLogByThread</mainClass>
&l