maven 打可执行jar包 打入所有依赖 包括本地依赖
程序员文章站
2022-04-28 18:59:10
...
稍微大点的项目都会用 maven 或者 ant 来构建,在 maven 工程中,我们也可以很方便的打包成可执行的 jar
包。默认Maven生成的JAR包只包含了编译生成的.class文件和项目资源文件,而要得到一个可以直接在命令行通过
java命令运行的JAR文件,还要满足两个条件:
- JAR包中的/META-INF/MANIFEST.MF元数据文件必须包含Main-Class信息。
- 项目所有的依赖都必须在Classpath中,其可以通过 MANIFEST.MF 指定或者隐式设置。
关于使用maven插件打jar 已经搞了两天了,终于搞定了,在此记录下。
maven-compiler-plugin 设置项目构建编译 jdk版本
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
maven-jar-plugin 设置修改jar包里/META-INF/MANIFEST.MF文件jar依赖,main class
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix></classpathPrefix>
<mainClass>com.xx.xx.xx</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
maven-dependency-plugin 将依赖的jar拷贝到编译目录 默认是target/lib
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
maven-assembly-plugin 将所有依赖的jar打入到最终生成的jar中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.zy.main.ScreenShot</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
此时便可以通过mvn clean compaile package 或 clean compile assembly:single 便会在target/目录生成相应jar
通过mvn clean compaile package 将生成两个jar 一个是没有dependence结尾的jar,这个是没有打入依赖jar的jar
一个时以dependence结尾jar,此jar是包含了项目所依赖的所有jar的
解决本地jar依赖问题:项目中如果引用了本地的jar,如:
<dependency>
<groupId>com.melloware</groupId>
<artifactId>jintellitype</artifactId>
<version>1.3.6</version>
<systemPath>${project.basedir}/lib/jintellitype-1.3.6.jar</systemPath>
</dependency>
此时使用的jar由于不在maven本地库中,使用
maven-assembly-plugin时将不能将此jar打入到可执行jar中
解决办法:使用maven-install-plugin 插件将本地引用jar安装到本地库中
<!--将项目jar打包值本地仓库-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/jintellitype-1.3.6.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.melloware</groupId>
<artifactId>jintellitype</artifactId>
<version>1.3.6</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
以下是完整的maven打jar的插件配置
<plugins>
<!--将项目jar打包值本地仓库-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/jintellitype-1.3.6.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.melloware</groupId>
<artifactId>jintellitype</artifactId>
<version>1.3.6</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 编译配置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.zy.main.ScreenShot</mainClass>
</manifest>
</archive>
<includes>
<!-- 打jar包时,只打包class文件 -->
<include>**/*</include>
</includes>
</configuration>
</plugin>
<!--拷贝依赖jar 到相target 目录-->
<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>
<!--生成有jar包依赖的可执行jar 并将全部依赖jar打入到可执行jar中-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.zy.main.ScreenShot</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
转载于:https://my.oschina.net/u/1015994/blog/617848