Maven-常用插件


羅列筆者認為比較有用的一些maven打包插件,方便后續查閱

spring-boot-maven-plugin

springboot自帶的maven插件,可用於簡單的JAR/WAR方式打包,官方地址為https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

簡單的應用如下

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<!--當使用springboot構建服務,則該配置可省略-->
	<version>2.1.4.RELEASE</version>
</plugin>

當執行mvn clean package命令的時候,其會默認執行該插件的repackage任務。其會在target目錄下生成以下兩個文件

> ls target/*.jar
> target/demo-springboot-web-0.0.1-SNAPSHOT.jar target/demo-springboot-web-0.0.1-SNAPSHOT.jar.original

其中demo-springboot-web-0.0.1-SNAPSHOT.jar.original文件,用戶可將.original后綴去掉便可得到用戶自己編寫的項目包。

demo-springboot-web-0.0.1-SNAPSHOT.jar文件往往比上面那個文件要大的多,其實其內部已經將用戶編寫的項目所相關的依賴都打進去了,百聞不如一見。
boot-maven

由圖中可知,相應的依賴均被打入至\BOOT-INF\lib目錄,而相應的源碼則被編譯放置到\BOOT-INF\classes目錄。

上述打出的JAR包,由\META-INF\MANIFEST.MF文件的屬性Main-Class可知具體的啟動由springboot官方編寫的org.springframework.boot.loader.JarLauncher類來啟動,且其會去加載Start-Class屬性指定的類來作為真正的啟動類。而用戶層的調用則可執行命令java -jar demo-springboot-web-0.0.1-SNAPSHOT.jar便可


而用戶如果想個別指定相應的main-class便可使用以下配置

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<!--當使用springboot構建服務,則該配置可省略-->
	<version>2.1.4.RELEASE</version>
	<configuration>
		<archive>
			<manifest>
				<mainClass>com.example.demo.DemoSpringApplication</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>

maven-assembly-plugin

apache開發的maven插件,可用於復雜方式打包,比如支持ZIP、TAR等方式的輸出,有助於集成。官方地址為http://maven.apache.org/plugins/maven-assembly-plugin/plugin-info.html

英文單詞assembly帶有組裝的意思,其功能比較強大,一般用戶都比較青睞的工具。

簡單的應用如下

<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
</plugin>

倘若我們直接使用mvn clean package命令,你會發現毫無有用的包打出來,看來是需要添加一些配置才能。


1.官方提供了默認的裝配方式,比如jar-with-dependencies

<plugin>
	    <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
	        <descriptorRefs>
		        <descriptorRef>jar-with-dependencies</descriptorRef>
	        <descriptorRefs>
        </configuration>
        <!--bind goal to package-->
        <executions>
	        <execution>
		        <id>make-assembly</id>
		        <phase>package</phase>
		        <goals>
			        <goal>single</goal>
		        <goals>
	        </execution>
        </executions>
</plugin>

這時候可使用mvn clean package進行打包了,還是以上述的項目為例,打包后的文件出現在target目錄

> ls target\*.jar
> target/demo-springboot-web-0.0.1-SNAPSHOT.jar target/demo-springboot-web-0.0.1-SNAPSHOT-jar-with-denpendencies.jar

spring-boot-maven插件打包類似,前者為項目源碼,后者為相應的依賴包。簡單看下相應的依賴包情況
maven-assembly

由上圖可知相應的源碼也被打進包中,所有的依賴包的代碼都會打進了該包中。當然用戶如果想指定相應的啟動類,則采取archive配置即可。

2.使用最多的還是用 指定相應的文件來擴展我們的需求(與spring-boot-maven插件搭配使用)。下面貼一個示例,具體的用戶可查閱文檔


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    ***

    <build>
        <finalName>demoWeb</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--如果指定為true,則表示打出來的jar包為可執行jar包,直接可以在*nix服務器上執行./${name}.jar便可運行。-->
                    <!--官方注釋不建議使用java -jar等方式調用打出來的包,當此配置為true時-->
                    <executable>true</executable>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                </configuration>
                <!--bind to package phase-->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>


descriptor.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <!-- 可自定義,這里指定的是項目環境 -->
    <id>release</id>

    <!-- 打包的類型,如果有N個,將會打N個類型的包 -->
    <formats>
        <format>tar.gz</format>
        <!--<format>zip</format>-->
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>

    <fileSets>
        <!--
            0755->即用戶具有讀/寫/執行權限,組用戶和其它用戶具有讀寫權限;
            0644->即用戶具有讀寫權限,組用戶和其它用戶具有只讀權限;
        -->

        <!-- 將src/main/assembly/bin目錄下的所有文件輸出到打包后的bin目錄中 -->
        <fileSet>
            <directory>${basedir}/src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>**.sh</include>
                <include>**.bat</include>
            </includes>
        </fileSet>

        <!-- 指定輸出resource中的配置文件到conf目錄中 -->
        <fileSet>
            <directory>${basedir}/src/main/resource</directory>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
            <includes>
                <include>*.*</include>
                <include>**/**</include>
            </includes>
        </fileSet>

        <!-- 將項目啟動jar打包到./目錄中 -->
        <fileSet>
            <directory>${basedir}/target</directory>
            <outputDirectory>.\</outputDirectory>
            <fileMode>0755</fileMode>
            <includes>
                <include>${project.name}-${project.version}.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

經過上述的配置便可指定打包成相應的.tar.gz壓縮包,默認打包出來的格式為${project.name}-${project.version}-${assembly.id}.tar.gz,也可通過以下配置進行更改

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
						<configuration>
							<finalName>${project.artifactId}-${project.version}</finalName>
							<appendAssemblyId>false</appendAssemblyId>
							<descriptors>
								<descriptor>src/main/assembly/descriptor.xml</descriptor>
							</descriptors>
						</configuration>
					</execution>
				</executions>
			</plugin>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM