首先看看工程目錄結構
如圖 (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>
</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>${groupId}.${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在項目的目錄下執行 mvn clean package
然後會在項目下生成 target 文件夾,並有兩個jar包,一個包含依賴,一個不包含
方法三: mvn clean package (shade方式)
下面是 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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<!-- Do not copy the signatures in the META-INF folder.
Otherwise, this might cause SecurityExceptions when using the JAR. -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${groupId}.${mainClass}</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在項目的目錄下執行 mvn clean package
然後會在項目下生成 target 文件夾,並有兩個jar包,一個包含依賴,一個不包含
方法四: mvn clean package (shade方式)
注:推薦該方法,可以過濾簽名文件,防止報一些莫名其妙的錯誤
下面是 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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<!-- Do not copy the signatures in the META-INF folder.
Otherwise, this might cause SecurityExceptions when using the JAR. -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${groupId}.${mainClass}</mainClass>
</transformer>
</transformers>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在項目的目錄下執行 mvn clean package
然後會在項目下生成 target 文件夾,並有兩個jar包,一個包含依賴,一個不包含
方法五: mvn clean package (copy方式)
下面是 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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${groupId}.${mainClass}</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}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在項目的目錄下執行 mvn clean package
然後會在項目下生成 target 文件夾,並有一個不包含依賴包的jar,和一個lib文件夾
注:兩個在同一個目錄才能運行
方法六: Intellij idea 手動打包
注:不建議該方法
我的 Intellij idea 爲 2016.2 版,操作如下:
- 點擊 File -> Project Structure
- 點擊 左側 Artifacts -> 綠色加號 -> JAR -> From modules with dependecies…
- 選擇你的 MainClass 和 Direcory for META-INF/MANIFEST.MF:,然後點擊 OK
注:MANIFEST.MF 位置選 所在項目的根目錄
- 勾選 Build on Make,然後 OK 或者 Apply 就行。
當你每次 Build -> Make Project 的時候,就會在項目根目錄下生成 out 文件夾, out/artifacts/**_jar/ 下爲包含依賴的jar包