温馨提示×

maven怎么打包第三方jar包

小亿
98
2024-02-27 10:38:03
栏目: 编程语言

要打包第三方jar包,可以通过以下几种方式:

  1. 将第三方jar包手动放入项目的lib目录中,然后在pom.xml文件中使用system指定该jar包的路径,这样在打包时会将该jar包一起打包到最终的输出文件中。
<dependency>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/example.jar</systemPath>
</dependency>
  1. 使用Maven插件将第三方jar包安装到本地仓库中,然后在pom.xml文件中正常引入该依赖即可。
mvn install:install-file -Dfile=path/to/example.jar -DgroupId=com.example -DartifactId=example -Dversion=1.0 -Dpackaging=jar
<dependency>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
</dependency>
  1. 使用Maven的assembly插件将项目及第三方jar包一起打包成一个可执行的jar包。

在pom.xml文件中配置assembly插件:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.example.MainClass</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

执行mvn clean package命令即可打包生成含有第三方jar包的可执行jar文件。

0