Coin163

maven打包为jar-with-dependencies无依赖jar包方法

2015-03-25by coin, 次阅读
maven打包为单独的jar,该jar包已包含所有依赖jar包,方法如下:
有两种方法:
maven-assembly-plugin和maven-shade-plugin(推荐)
第一种对spring的支持不好,见注释掉部分。

<plugins>
			<!--指定JDK为1.7 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!--指定主函数和各个依赖 ,该方法使用spring时会出现问题 -->
			<!-- <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> 
				<configuration> <archive> <manifest> <mainClass>com.stone.business.bootstrap.ProviderServer</mainClass> 
				</manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> 
				</descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> 
				<phase>package</phase> <goals> <goal>single</goal> </goals> </execution> 
				</executions> </plugin> -->

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>1.4</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<filters>
								<filter>
									<artifact>*:*</artifact>
									<excludes>
										<exclude>META-INF/*.SF</exclude>
										<exclude>META-INF/*.DSA</exclude>
										<exclude>META-INF/*.RSA</exclude>
									</excludes>
								</filter>
							</filters>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.handlers</resource>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>com.stone.business.bootstrap.ProviderServer</mainClass>
								</transformer>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
									<resource>META-INF/spring.schemas</resource>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>


------分隔线----------------------------