温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Maven打包时怎么指定启动类

发布时间:2023-04-08 15:38:22 来源:亿速云 阅读:188 作者:iii 栏目:开发技术

这篇“Maven打包时怎么指定启动类”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Maven打包时怎么指定启动类”文章吧。

    Maven打包时指定启动类

    使用Maven打包的时候, 有时候需要指定启动类, 可如下操作!

    测试项目(结构如下):

    Maven打包时怎么指定启动类

    代码: com.xxx.MainClass.java 

    package com.xxx;
    
    public class MainClass {
        public static void main(String[] args) {
            System.out.println("The project is to use maven to package a Jar with mainclass !");
        }
    }

    打包的时候指定启动类为 com.xxx.MainClass

    方法一

    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.xxx</groupId>
        <artifactId>maven-packaging</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <build>
            <!-- 打包后的名字 -->
            <finalName>MainClass</finalName>
            <plugins>
                <!-- 配置jdk版本为1.8 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <target>1.8</target>
                        <source>1.8</source>
                    </configuration>
                </plugin>
    
                <!-- 配置启动类 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.xxx.MainClass</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    执行 mvn clean package , 在target包下找到MainClass.jar文件 

    可直接使用命令 java -jar MainClass.jar 执行

    Maven打包时怎么指定启动类

    方法二

    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.xxx</groupId>
        <artifactId>maven-packaging</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <build>
            <!-- 打包后的名字 -->
            <finalName>MainClass</finalName>
            <plugins>
                <!-- 配置jdk版本为1.8 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <target>1.8</target>
                        <source>1.8</source>
                    </configuration>
                </plugin>
            </plugins>
            <!-- 配置启动类 -->
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.xxx.MainClass</mainClass>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib/</classpathPrefix>
                                </manifest>
                            </archive>
                            <classesDirectory>
                            </classesDirectory>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>

    执行 mvn clean package , 在target包下找到MainClass.jar文件 

    可直接使用命令 java -jar MainClass.jar 执行

    Maven打包时怎么指定启动类

    两种配置效果时一样的 !!!!!

    maven打包以某个类为启动类

    配置信息如下:

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.example.demo.TestKey</mainClass>
                              <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    Maven打包时怎么指定启动类

    定义一个java启动类

    package com.example.demo;
     
    public class TestKey {
        public static void main(String[] args) {
            System.out.println("启动了.............");
        }
    }

    以上就是关于“Maven打包时怎么指定启动类”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI