maven构建可执行jar包

时间:2022-04-07 09:09:43

使用maven-compiler-plugin和maven-jar-plugin两个插件可以构建可执行jar包:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.sq.platform.dataMigration.service.main.LoadLocalDataMain</mainClass>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
        <classesDirectory>
        </classesDirectory>
    </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}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

打包完成后,通过命令行即可运行:

java -jar ${jar包名}

注意:lib包需要和jar包放在同级目录下