Maven將代碼及依賴打成一個Jar包的方式

Maven可使用mvn package指令對項目進行打包,若是使用java -jar xxx.jar執行運行jar文件,會出現"no main manifest attribute, in xxx.jar"(沒有設置Main-Class)、ClassNotFoundException(找不到依賴包)等錯誤。 html

要想jar包能直接經過java -jar xxx.jar運行,須要知足: java

一、在jar包中的META-INF/MANIFEST.MF中指定Main-Class,這樣才能肯定程序的入口在哪裏; git

二、要能加載到依賴包。 github

使用Maven有如下幾種方法能夠生成能直接運行的jar包而且是打成一個jar包,能夠根據須要選擇一種合適的方法。spring

方法一:使用maven-assembly-plugin插件打包

在pom.xml中配置:

<build>
	<plugins>
 
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-assembly-plugin</artifactId>
			<version>2.5.5</version>
			<configuration>
				<archive>
					<manifest>
						<mainClass>com.xxg.Main</mainClass>
					</manifest>
				</archive>
				<descriptorRefs>
					<descriptorRef>jar-with-dependencies</descriptorRef>
				</descriptorRefs>
			</configuration>
		</plugin>
 
	</plugins>
</build>

打包方式: mvn package assembly:single(在eclipse中執行maven build…… ,而後在Goals中輸入 assembly:singleexpress

打包後會在target目錄下生成一個xxx-jar-with-dependencies.jar文件,這個文件不但包含了本身項目中的代碼和資源,還包含了全部依賴包的內容。因此能夠直接經過java -jar來運行。 apache

此外還能夠直接經過mvn package來打包(在eclipse中直接執行maven install),無需assembly:single,不過須要加上一些配置:app

<build>
	<plugins>
 
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-assembly-plugin</artifactId>
			<version>2.5.5</version>
			<configuration>
				<archive>
					<manifest>
						<mainClass>com.xxg.Main</mainClass>
					</manifest>
				</archive>

<descriptorRefs>eclipse

<!-- 這個jar-with-dependencies是assembly預先寫好的一個,組裝描述引用 -->maven

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

<!--工程名-->

<finalName>${project.name}</finalName>

			</configuration>
			<executions>
				<execution>
					<id>make-assembly</id>
					<phase>package</phase>
					<goals>
						<goal>single</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
 
	</plugins>
</build>

其中<phase>package</phase>、<goal>single</goal>即表示在執行package打包時,執行assembly:single,因此能夠直接使用mvn package打包。

不過,若是項目中用到Spring Framework,用這種方式打出來的包運行時會出錯,使用下面的方法二能夠處理。

方法二:使用maven-shade-plugin插件打包

在pom.xml中配置:

<build>
	<plugins>
 
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version>2.4.1</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
					<configuration>
						<transformers>
							<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								<mainClass>com.xxg.Main</mainClass>
							</transformer>
						</transformers>
					</configuration>
				</execution>
			</executions>
		</plugin>
 
	</plugins>
</build>

配置完成後,執行mvn package便可打包。在target目錄下會生成兩個jar包,注意不是original-xxx.jar文件,而是另一個。和maven-assembly-plugin同樣,生成的jar文件包含了全部依賴,因此能夠直接運行。

若是項目中用到了Spring Framework,將依賴打到一個jar包中,運行時會出現讀取XML schema文件出錯。緣由是Spring Framework的多個jar包中包含相同的文件spring.handlers和spring.schemas,若是生成一個jar包會互相覆蓋。爲了不互相影響,

可使用AppendingTransformer來對文件內容追加合併:

<build>
	<plugins>
 
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version>2.4.1</version>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>shade</goal>
					</goals>
					<configuration>
						<transformers>
							<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
								<mainClass>com.xxg.Main</mainClass>
							</transformer>
							<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>
						</transformers>
					</configuration>
				</execution>
			</executions>
		</plugin>
 
	</plugins>
</build>

方法三:使用maven-jar-plugin和maven-dependency-plugin插件打包

原理是經過修改maven 打jar包的maven-jar-plugin插件的配置信息來生成咱們須要的指定依賴的可執行jar包。

在項目的pom.xml文件中修改默認的jar插件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <!--運行jar包時運行的主類,要求類全名-->
                        <mainClass>com.hafiz.Runner</mainClass>
                        <!-- 是否指定項目classpath下的依賴 -->
                        <addClasspath>true</addClasspath>
                        <!-- 指定依賴的時候聲明前綴 -->
                        <classpathPrefix>./lib/</classpathPrefix>
                        <!--依賴是否使用帶有時間戳的惟一版本號,如:xxx-1.3.0-20121225.012733.jar-->
                        <useUniqueVersions>false</useUniqueVersions>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
<!--接着咱們還要配置maven的maven-dependency-plugin插件把當前項目的全部依賴放到target目錄下的lib文件夾下-->
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
        <id>copy</id>
        <phase>package</phase>
        <goals>
           <goal>copy-dependencies</goal>
        </goals>
        <configuration>
           <outputDirectory>${project.build.directory}/lib</outputDirectory>
        </configuration>
     </execution>
   </executions>
</plugin>

其中,${project.build.directory}表示默認的target文件夾。

咱們經過上文的修改便完成了適用maven生成指定依賴的可執行jar包。

咱們發現生成的manifest文件中已經設置好了Main-Class以及Class-Path,以下:

若是<addClasspath>設置爲false,則生成的manifest文件中不會聲明依賴(即不會有Class-Path聲明)

程序主類以下:

package com.hafiz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Desc:主類
 * Created by hafiz.zhang on 2018/4/07.
 */
public class Runner {
    public static void main(String[] args) {
        ApplicationContext context 
        = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        System.out.println("context:" + context.getClass());
        System.out.println("The Main Class Is Running....");
    }
}

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>com.hafiz</groupId>
    <artifactId>assembly-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>assembly-demo</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.2.6.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <!--運行jar包時運行的主類,要求類全名-->
                            <mainClass>com.hafiz.Runner</mainClass>
                            <!-- 是否指定項目classpath下的依賴 -->
                            <addClasspath>true</addClasspath>
                            <!-- 指定依賴的時候聲明前綴 -->
                            <classpathPrefix>./lib/</classpathPrefix>
                            <!--依賴是否使用帶有時間戳的惟一版本號,如:xxx-1.3.0-20121225.012733.jar-->
                            <useUniqueVersions>false</useUniqueVersions>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!--把當前項目全部的依賴打包到target目錄下的lib文件夾下-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!--已存在的Release版本不重複copy-->
                            <overWriteReleases>false</overWriteReleases>
                            <!--已存在的SnapShot版本不重複copy-->
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <!--不存在或者有更新版本的依賴才copy-->
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

咱們如今進入生成的jar包所在的文件夾下,使用 java -jar xxx.jar來執行生成的jar包

到此咱們就完成了如何使用maven的jar包生成插件來進行生成指定依賴的可執行jar包。

代碼Github地址:https://github.com/hafizzhang/assembly-demo.git


參考網址:

http://www.javashuo.com/article/p-usxjyaer-mw.html

http://www.cnblogs.com/hafiz/p/6538107.html

http://www.javashuo.com/article/p-qszbvohu-dx.html

相關文章
相關標籤/搜索