使用 Maven 打成 jar 包,执行 main 方法

本文介绍如何使用Maven将Java项目打包成包含依赖的可执行Jar包,包括配置pom.xml,添加maven-shade-plugin插件,以及设置主类等步骤。

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

第一步:创建一个类,在类中声明一个main方法

package examples.task;

public class CollectTask {

	public static void main(String[] args) {
		System.out.println("测试执行jar包");
	}
	
}

 

第二步:在pom.xml文件中添加以下代码

<!-- java -jar main -->
<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-shade-plugin</artifactId>  
    <version>1.4</version>  
    <executions>  
        <execution>  
            <phase>package</phase>  
            <goals>  
                <goal>shade</goal>  
            </goals>
            <configuration>
            	<includeSystemScope>true</includeSystemScope>
                <transformers>  
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                        <resource>META-INF/spring.handlers</resource>  
                    </transformer> 
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <!-- 要执行的class文件目录   -->
                        <mainClass>examples.task.CollectTask</mainClass>  
                    </transformer>  
                    <transformer  
                        implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                        <resource>META-INF/spring.schemas</resource>  
                    </transformer>  
                </transformers>  
            </configuration>  
        </execution>  
    </executions>  
</plugin>
<!-- 加载jar包 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>install</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

第三步:打开命令行窗口

或者指定 jdk 目录执行

 

如果本文章对您有所帮助,您可以动一动您的金手指点个赞吧!!!!

### 使用 Maven项目JAR 文件的命令 使用 Maven项目JAR 文件的标准命令是 `mvn clean package`。该命令会清理之前的构建结果,然后根据项目的 `pom.xml` 配置文件将项目编译并打JAR 文件[^1]。 如果需要确保生成的 JAR 文件含所有依赖项,并且是一个可运行的 JAR 文件,可以在 `pom.xml` 中配置 `maven-assembly-plugin` 或 `maven-shade-plugin` 插件。以下是使用 `maven-assembly-plugin` 的示例配置: ```xml <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.MainClass</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 执行以下命令即可生成含依赖项的可运行 JAR 文件: ```bash mvn clean package ``` 若需要仅将工程下的某个特定(而非整个工程)打JAR 文件,可以结合 `Maven Assembly Plugin` 和自定义的 assembly 描述符实现[^2]。 ### 注意事项 - 确保 `pom.xml` 文件中正确指定了主类(`<mainClass>`),否则生成的 JAR 文件可能无法直接运行。 - 如果目标目录下未显示 JAR 文件,请检查构建日志,确认是否有错误或警告信息[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值