«

Maven打包时怎么指定启动类

时间:2024-7-15 09:03     作者:韩俊     分类: Java


这篇“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 执行

    方法二

    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>

    定义一个java启动类

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

    标签: java maven

    热门推荐