maven工程打包为jar
程序员文章站
2022-07-15 14:41:29
...
- 文章目的为:把一个jar类型的maven工程打包为jar其中jar包含当前工程所依赖的jar
- 使用maven创建工程(commons-libs),其类型指定为jar
- 向commons-libs工程添加所依赖的jar
- 工程pom.xml文件添加如下配置
-
<!-- <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>core.Test</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> --> <build> <plugins> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.sysware.HelloWorld</mainClass> </manifest> </archive> </configuration> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <!-- get all project dependencies --> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <!-- MainClass in mainfest make a executable jar --> <archive> <manifest> <mainClass>com.mkyong.core.utils.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- 完成pom.xml
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>commons-libs</groupId> <artifactId>commons-libs</artifactId> <version>0.0.1</version> <dependencies> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>com.commons.beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>com.commons.codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>com.commons.collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>com.commons.lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>com.commons.logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>com.httpclient</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>com.httpcore</groupId> <artifactId>httpcore</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>com.jdom</groupId> <artifactId>jdom</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>com.json.lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>com.wechat.util</groupId> <artifactId>wechat-util</artifactId> <version>1.0.2</version> </dependency> </dependencies> <!-- <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>core.Test</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> --> <build> <plugins> <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.sysware.HelloWorld</mainClass> </manifest> </archive> </configuration> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <!-- get all project dependencies --> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <!-- MainClass in mainfest make a executable jar --> <archive> <manifest> <mainClass>com.mkyong.core.utils.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- bind to the packaging phase --> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
至此:到项目pom.xml文件右击 RunAs Maven build... - 输入 clean package 命令,如下图,点击run 去目标tager目录查找目标jar,解压后会有相关依赖jar包的class文件,即可
参考地址:http://www.cnblogs.com/xinsheng/p/4109573.html
推荐阅读
-
使用IDEA的maven工程导入ojdbc14 jar包失败
-
3.eclipse中 maven打包web工程几种方式
-
在eclipse中使用Maven分模块搭建SSM框架,创建jar、war、pom工程模块教学,项目的热部署,需要的可以留下邮箱,给大家发整理过的Maven笔记
-
Spring Boot Maven 打包可执行Jar文件的实现方法
-
JavaEE:用Maven Docker插件编译上传工程为Docker镜像
-
maven工程打包为jar
-
打包的插件有maven的jar,war(可执行),还有assembly插件(zip)
-
使用maven-shade 打包apache cxf 为可运行包 出现异常Caused by: org.apache.cxf.BusException:
-
使用maven-shade 打包apache cxf 为可运行包 出现异常Caused by: org.apache.cxf.BusException:
-
Spring-boot构建多模块依赖工程时,maven打包异常:程序包xxx不存在