maven工程打成jar包的三种方式

本文介绍了使用Maven将工程打包成jar的三种方式:1) 将所有依赖合并到一个jar包中;2) 分离依赖,生成一个包含依赖的jar和一个主jar;3) 依赖jar单独存放,通过java -cp运行。详细步骤包括创建新工程、编写主类、配置Pom.xml文件以及执行打包和运行命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接: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

### 使用 IntelliJ IDEA 和 Maven 构建并打包 Java 项目 #### 配置 Maven 环境 为了确保能够顺利使用 Maven 进行构建,在 IntelliJ IDEA 中需先完 Maven 的配置。通过 `File->Settings->Build, Execution, Deployment->Build Tools->Maven` 路径进入设置界面,具体选项括: - **User settings file**: 设置为自定义的 `settings.xml` 文件路径以便于管理和维护不同的镜像源等配置。 - **Local repository**: 定义本地仓库的位置,默认情况下会读取 `settings.xml` 中的信息自动填充此字段。 - **Maven home directory**: 如果已设置了系统的 MAVEN_HOME 变量,则 IDE 应该能识别;如果没有则手动选择 Maven 的安装目录[^2]。 #### 创建 Maven Project 并添加依赖项 启动 IntelliJ IDEA 后可以通过新建工程向导创建一个基于 Maven 的项目结构。在 pom.xml 文件内声明所需 Struts 2 或其他框架的相关依赖关系以供后续开发工作调用[^1]。 ```xml <dependencies> <!-- 添加 Struts 2 依赖 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <!-- 其他必要的库... --> </dependencies> ``` #### 编写代码与测试 编写业务逻辑代码之后应该加入单元测试来验证程序的功能正确性。JUnit 是常用的 Java 测试框架之一,可以在 pom.xml 中引入 JUnit 来支持自动化测试案例执行。 ```xml <!-- 引入 JUnit 支持 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> ``` #### 执行构建过程 当所有的准备工作完后就可以准备打包了。右键点击项目的根节点或者直接打开终端窗口输入命令来进行完整的生命周期构建流程,其中括清理旧版本、编译源码、运行测试直至最后生可分发的目标产物——JAR 文件。 ```bash mvn clean install ``` 上述命令将会触发一系列的任务链路,依次清除之前的输出果(`clean`),重新编译整个应用(`compile`),随后依据 `<build>` 下面所设定好的插件规则将所有类文件连同资源一起被打包进最终制品里去(`package`)。如果一切正常结束,那么目标产物会被放置到 `${project.basedir}/target/your-artifact-id-version.jar` 目录下面等待部署发布。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值