Java Maven项目打jar包方法
Posted 一休Q_Q
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Maven项目打jar包方法相关的知识,希望对你有一定的参考价值。
使用assembly的方式
- 配置assemby文件,定义不打进去的相关包,依赖的包会压缩成一个大的jar包
..\\..\\..\\src\\main\\resources\\3rd-jar-zztjapp.xml
2.在pom文件添加文件配置
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--3rd-jar-agent thirdparty -->
<!--KMinerClass-agent agent -->
<descriptors>
<descriptor>src/main/resources/3rd-jar-zztjapp.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
<finalName>zztjapp</finalName>
</configuration>
<!-- 以下这段表明 maven-assembly-plugin的single goal被绑定在package phase上了,注释掉避免每次package时自动生成第三方jar包 -->
<!-- <executions> <execution> <id>make-assembly</id> <phase>package</phase>
<goals> <goal>single</goal> </goals> </execution> </executions> -->
</plugin>
- 执行打包代码
mvn clean install -DskipTests
mvn assembly:assembly
使用package方式
将下面代码拷贝到pom文件中,产出的依赖包会放入lib文件夹下
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 包含文件配置,现在只打包 com 文件夹 -->
<includes>
<include>
**/com/**
</include>
</includes>
<archive>
<manifest>
<!-- 配置加入依赖包 -->
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<useUniqueVersions>false</useUniqueVersions>
<!-- Spring Boot 启动类(自行修改) -->
<mainClass>com.server.EnterpriseApp</mainClass>
</manifest>
<manifestEntries>
<!-- 外部资源路径加入 manifest.mf 的 Class-Path -->
<Class-Path>resources/</Class-Path>
</manifestEntries>
</archive>
<!-- jar 输出目录 -->
<outputDirectory>$project.build.directory/</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- 复制依赖 -->
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 依赖包 输出目录 -->
<outputDirectory>$project.build.directory/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<!-- 复制资源 -->
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<!-- 资源文件 输出目录 -->
<outputDirectory>$project.build.directory/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
执行打包脚本
mvn clean package -Dmaven.test.skip=true
调用接口执行
java -cp "ts-test_lib/*" com.cws.test.KMAnaTest "./dic" "./性能测试" "NoProcess" "1"
注意:某些时候,由于eclipse等工具配置问题,会报
将编码加入pom的<properties>中。
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
以上是关于Java Maven项目打jar包方法的主要内容,如果未能解决你的问题,请参考以下文章