天天看點

maven項目打成jar包執行main方法

maven常用的普通打包方式分為pom,jar,war等,如果不進行特殊配置,那麼打包出來的jar包是不可運作的。隻能當作普通依賴包使用。

下面介紹兩種打包方式:

1、内置打包法

在pom.xml中添加依賴

<plugins>
            <!-- 内置打包法 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <!-- 用這個maven打包插件 -->
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!-- 預設值為true.注意這個屬性,如果你用這個插件來deploy,或者釋出到中央倉庫,這個屬性會縮減你的pom檔案,會把你依賴的<dependency>幹掉 -->
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                            <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.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <!-- 這個是你的程式入口檔案 -->
                                    <mainClass>com.kili.lipapay.transfer.MainLauncher</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
           

上面使用maven install之後即可得到一堆檔案。

/target/目錄裡面找一下,即可找到一個<你的項目名>.jar

這個包比較大(你的依賴包+實體類=總大小),可以直接運作

運作例子:java -jar 檔案路徑/<你的項目名>.jar

java -jar 包名.jar
           

2、外部依賴發

<plugins>
			<!-- 打包jar檔案時,配置manifest檔案,加入lib包的jar依賴 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
					<classesDirectory>target/classes/</classesDirectory>
					<archive>
						<manifest>
							<mainClass>com.alibaba.dubbo.container.Main</mainClass>
							<!-- 打包時 MANIFEST.MF檔案不記錄的時間戳版本 -->
							<useUniqueVersions>false</useUniqueVersions>
							<addClasspath>true</addClasspath>
							<classpathPrefix>lib/</classpathPrefix>
						</manifest>
						<manifestEntries>
							<Class-Path>.</Class-Path>
						</manifestEntries>
					</archive>
				</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>
							<type>jar</type>
							<includeTypes>jar</includeTypes>
							<useUniqueVersions>false</useUniqueVersions>
							<outputDirectory>
								${project.build.directory}/lib
							</outputDirectory>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
           

上面使用maven install之後即可得到一堆檔案。

/target/目錄裡面找一下,和上面不一樣的是,你會發現<你的項目名>.jar沒那麼大。

而且還會有一個檔案夾叫lib,而這個lib檔案夾内全是你的依賴jar包

那麼在使用的時候,<你的項目名>.jar會去加載外部的lib裡面的jar包。

在部署項目的時候呢,你需要把lib檔案夾和<你的項目名>.jar一起拷貝去使用

注意:兩個需要是同目錄下

運作例子:java -jar 檔案路徑/<你的項目名>.jar

參考原文位址:http://alex233.blog.51cto.com/8904951/1885759