Maven基础-pom.xml中配置打包的入口程序和第三方依赖包

1.说明

本文的打包操作适用于 普通的java项目
本文介绍的 maven 的打包方式是把java项目的第三方依赖包与主程序独立出来,即目标jar包与依赖包目录同级。

2.具体的操作

2.1 项目结构介绍

一个标准的maven项目: 目录结构完全符合maven规范

helloworld-maven-java
    | -- src
    	| -- main
    		| -- java
    			| -- com.northcastle
    				| -- App.java : 主类
    		| -- resources
    	| -- test
    		| -- java
    			| -- com.northcastle
    				| -- AppTest.java
    		| -- resources
    |-- pom.xml

App.java 的文件内容:

package com.northcastle;

import com.alibaba.fastjson.JSONObject;

/**
 * Hello world!
 *
 */
public class App {
    public static void main( String[] args ){
        System.out.println("=== App run begin ===");
        System.out.println( "Hello World!" );
        System.out.println( "欢迎使用maven进行打包工作" );
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","northcastle");
        System.out.println("jsonObject is  : "+jsonObject);
        System.out.println("=== App run end ===");
    }
}

2.2 pom.xml文件内容(核心)

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>com.northcastle</groupId>
<artifactId>helloworld-maven-java</artifactId>
<version>1.0-SNAPSHOT</version>

  <!--设置打包的类型为 jar 包-->
  <packaging>jar</packaging>

<!--  自定义的属性设置,可以自己进行配置和修改-->
<properties>
  <!--  maven 编译代码使用的jdk版本  -->
  <maven.compiler.source>1.8</maven.compiler.source>
  <!--  maven 执行代码使用的jdk版本  -->
  <maven.compiler.target>1.8</maven.compiler.target>
  <!--  maven 编译使用的编码  -->
  <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  <!--  maven 进行项目构建使用的编码,避免中文乱码  -->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <!--  maven 生成项目报告使用的编码  -->
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

</properties>

<!--  依赖中默认带有一个 junit 单元测试-->
<dependencies>

  <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>

</dependencies>

<build>
  <!-- 指定最后构建打包成功的压缩包的名字 -->
  <finalName>helloworld-maven-java</finalName>

  <plugins>
    <!-- 1.maven 打包时跳过测试 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId> <!-- 测试使用到的插件 -->
      <configuration>
        <skip>true</skip><!-- 声明跳过测试 -->
      </configuration>
    </plugin>

    <!-- 2.1 maven 打包时指定main方法 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <classesDirectory>target/classes</classesDirectory>
        <archive>
          <manifest>
            <mainClass>com.northcastle.App</mainClass>
            <useUniqueVersions>false</useUniqueVersions>
            <addClasspath>true</addClasspath>
            <classpathPrefix>libs/</classpathPrefix>
          </manifest>
          <manifestEntries>
            <Class-Path>.</Class-Path>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>
    <!--  2.2 maven 打包时处理依赖的第三方jar包,解决 运行时 ClassNotFoundException 的异常  -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <type>jar</type>
            <includeTypes>jar</includeTypes>
            <outputDirectory>
              ${project.build.directory}/libs
            </outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>


</project>

2.2.1 pom.xml 中表示指定程序入口类的配置

这一块是配置的核心

  <!-- 2.1 maven 打包时指定main方法 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <classesDirectory>target/classes</classesDirectory>
        <archive>
         <!-- manifest 标签表示 jar包内的 MANIFEST.MF 清单配置文件的内容 -->
          <manifest>
            <!-- 1.mainClass 标签指定程序的入口类 -->
            <mainClass>com.northcastle.App</mainClass>
            <useUniqueVersions>false</useUniqueVersions>
            <addClasspath>true</addClasspath>
             <!-- 2.classpathPerfix 标签 表示指定第三方依赖的目录名,和目标jar包同级 -->
            <classpathPrefix>libs/</classpathPrefix>
          </manifest>
          <manifestEntries>
            <Class-Path>.</Class-Path>
          </manifestEntries>
        </archive>
      </configuration>
    </plugin>

2.2.2 pom.xml 中表示第三方依赖的配置

这一块是配置的核心 : 如果不理解,直接抄上就行

 <!--  2.2 maven 打包时处理依赖的第三方jar包,解决 运行时 ClassNotFoundException 的异常  -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <type>jar</type>
            <includeTypes>jar</includeTypes>
            <outputDirectory>
              ${project.build.directory}/libs
            </outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>

2.3 执行打包命令

在 pom.xml 文件所在的目录下执行打包命令 : mvn clean package

在这里插入图片描述
在这里插入图片描述

2.4 查看打包后target目录

根据我们的pom.xml 中的配置,打包成功后:
1.会在 target目录 下生成一个 libs的目录来存放我们的第三方的依赖包;
2.会在 target目录 下生成我们的目标jar包

在这里插入图片描述
在这里插入图片描述

2.5 运行jar包,检查效果

使用java -jar xxx.jar 命令,运行程序

在这里插入图片描述

3.完成

Congratulations!
You are one step closer to success!

### 配置 `maven-shade-plugin` 打包包含第三方依赖的 JAR 文件 为了确保生成的 JAR 文件能够正常工作并包含所有的第三方依赖,可以通过配置 `pom.xml` 中的 `maven-shade-plugin` 插件来完成此操作。以下是详细的配置说明: #### 添加插件到 POM 文件 在项目的 `pom.xml` 文件中添加如下配置以引入 `maven-shade-plugin` 并设置其参数[^1]。 ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <!-- 主类名 --> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.example.MainApp</mainClass> </transformer> </transformers> <!-- 排除不需要被打入jar中的资源文件 --> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这段 XML 片段定义了一个名为 `maven-shade-plugin` 的插件实例,并指定了版本号以及执行阶段为 package。通过 `<transformers>` 节点指定主程序入口类;而 `<filters>` 则用于排除某些不必要的 META-INF 下面的安全签名文件,防止它们引起冲突[^2]。 对于那些无法从远程仓库获取或者路径特殊的本地依赖项,则建议将其安装至本地 Maven 仓库后再作为普通依赖加入项目中,而不是采用 scope=system 方式直接引用,因为后者可能会导致打包工具处理不当的问题[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值