2021年3月23日
jar与代码分离打包
<build> <plugins> <!-- 指定启动类,将依赖打成外部jar包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 --> <addMavenDescriptor>false</addMavenDescriptor> <manifest> <!-- 是否要把第三方jar加入到类构建路径 --> <addClasspath>true</addClasspath> <!-- 外部依赖jar包的最终位置 --> <classpathPrefix>lib/</classpathPrefix> <!-- 项目启动类 --> <mainClass>com.xxx.XXApplication</mainClass> </manifest> <manifestEntries> <!--将jar写进MANIFEST.MF文件中的Class-Path--> <Class-Path>lib/commons-dbcp-1.2.1.jar</Class-Path> <Class-Path>lib/commons-pool-1.2.jar</Class-Path> <Class-Path>lib/javaparser-1.0.9.jar</Class-Path> <Class-Path>lib/xjavadoc-1.5-snapshot050611.jar</Class-Path> <Class-Path>lib/yq-estivate-2.0.jar</Class-Path> </manifestEntries> </archive> </configuration> </plugin> <!--拷贝依赖到jar外面的lib目录--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy-lib</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>target/lib</outputDirectory> <excludeTransitive>false</excludeTransitive> <stripVersion>false</stripVersion> <includeScope>test</includeScope> </configuration> </execution> </executions> </plugin> </plugins> </build>
需要注意的点: 1.manifestEntries 如果没加,单独引入的jar无法引用 2.includeScope 有几个不同的级别,默认是test,打包时候也会把src/lib下面的包放到最终的target/lib下面,如果设置成runtime之类的就不行 3.执行的时候 java -Dloader.path=./lib -jar xxxx/target/xxxx.jar 可以使用-Dloader.path=./lib指定jar包路径 上面的配置如果是在同一个目录下,可以省略-Dloader.path=./lib