Maven打包时如何指定启动类

2023-05-14 20:05:20 指定 启动 打包

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 执行

方法二

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打包以某个类为启动类

配置信息如下:

<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>

up-f1ed6a8ed6b3a6d04c6b9d07df5a0012482.png

定义一个java启动类

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

相关文章