Maven 打包-添加第三方包、依赖包 mvn clean package

首先看看工程目录结构

如图 (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 版,操作如下:

  1. 点击 File -> Project Structure
  2. 点击 左侧 Artifacts -> 绿色加号 -> JAR -> From modules with dependecies…

  1. 选择你的 MainClass 和 Direcory for META-INF/MANIFEST.MF:,然后点击 OK 
    注:MANIFEST.MF 位置选 所在项目的根目录

  1. 勾选 Build on Make,然后 OK 或者 Apply 就行。

当你每次 Build -> Make Project 的时候,就会在项目根目录下生成 out 文件夹, out/artifacts/**_jar/ 下为包含依赖的jar包

 

 

原文地址:https://blog.csdn.net/jiecxy/article/details/52458061

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值