欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven工程打包为jar

程序员文章站 2022-06-29 14:32:02
...

 

  • 文章目的为:把一个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文件,即可
    maven工程打包为jar
            
    
    博客分类: Maven mavenbuildassemblyshade
     参考地址:http://www.cnblogs.com/xinsheng/p/4109573.html
  • maven工程打包为jar
            
    
    博客分类: Maven mavenbuildassemblyshade
  • 大小: 120.4 KB