1. 程式人生 > >maven插件詳解

maven插件詳解

prop vendor div extension embed aging 使用 目錄 sym

一、maven-jar-plugin插件詳解

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <!-- 生成的jar中,包含pom.xml和pom.properties這兩個文件 -->
                        <addMavenDescriptor>true
</addMavenDescriptor> <!-- 生成MANIFEST.MF的設置 --> <manifest> <!--這個屬性特別關鍵,如果沒有這個屬性,有時候我們引用的包maven庫 下面可能會有多個包,並且只有一個是正確的,其余的可能是帶時間戳的, 此時會在classpath下面把那個帶時間戳的給添加上去,然後我們 在依賴打包的時候,打的是正確的,所以兩頭會對不上,報錯。
--> <useUniqueVersions>false</useUniqueVersions> <!-- 為依賴包添加路徑, 這些路徑會寫在MANIFEST文件的Class-Path下 --> <addClasspath>true</addClasspath> <!-- 這個jar所依賴的jar包添加classPath的時候的前綴,如果這個 jar本身和依賴包在同一級目錄,則不需要添加 --> <classpathPrefix>lib/</classpathPrefix> <!-- jar啟動入口類 --> <mainClass>com.ht.pojo.Test</mainClass> </manifest> <manifestEntries> <!-- 在Class-Path下添加配置文件的路徑 --> <Class-Path>../config/</Class-Path> <!-- 假如這個項目可能要引入一些外部資源,但是你打包的時候並不想把 這些資源文件打進包裏面,這個時候你必須在這邊額外指定一些這些資源 文件的路徑,這個位置指定的話,要根據你預期的這些位置去設置,我這邊 所有jar都在lib下,資源文件都在config下,lib和config是同級的 同時還需要註意另外一個問題,假如你的pom文件裏面配置了
<scope>system</scope>,就是你依賴是你本地的資源,這個時候使用 這個插件,classPath裏面是不會添加,所以你得手動把這個依賴添加進 這個地方,用空格隔開就行 --> </manifestEntries> </archive> <!-- jar包的位置 --> <outputDirectory>${project.build.directory}/lib</outputDirectory> <includes> <!-- 打jar包時,打包class文件和config目錄下面的 properties文件--> <!-- 有時候可能需要一些其他文件,這邊可以配置,包括剔除的文件等等 --> <include>**/*.class</include> <include>**/*.properties</include> </includes> </configuration> </plugin>

使用clean package命令打包成功後,目錄結構如下所示:

技術分享圖片

打開meventest-0.0.1-SNAPSHOT.jar文件,查看打包成功後的項目結構

技術分享圖片

二、maven-dependency-plugin

說明:該插件可以把依賴的jar包,打包到項目的指定目錄中

 1             <plugin>
 2                 <groupId>org.apache.maven.plugins</groupId>
 3                 <artifactId>maven-dependency-plugin</artifactId>
 4                 <executions>
 5                     <execution>
 6                         <id>copy-dependencies</id>
 7                         <phase>package</phase>
 8                         <goals>
 9                             <goal>copy-dependencies</goal>
10                         </goals>
11                         <configuration>
12                             <!-- 拷貝項目依賴包到lib/目錄下 -->
13                             <outputDirectory>${project.build.directory}/lib</outputDirectory>
14                             <!-- 間接依賴也拷貝 -->
15                             <excludeTransitive>false</excludeTransitive>
16                             <!-- 帶上版本號 -->
17                             <stripVersion>false</stripVersion>
18                         </configuration>
19                     </execution>
20                 </executions>
21             </plugin>

打包後的目錄結構如下所示:

技術分享圖片

三、maven-assembly-plugin

  • 該插件可以把項目依賴的jar包編譯成class文件,以包的形式生成在項目中
  • 將Assembly插件的assembly:single加入到Maven的package階段
 1 <plugin>
 2                 <artifactId>maven-assembly-plugin</artifactId>
 3                 <version>3.1.0</version>
 4                 <configuration>
 5                     <archive>
 6                         <manifest>
 7                             <mainClass>com.ht.pojo.Test</mainClass>
 8                         </manifest>
 9                     </archive>
10                     <descriptorRefs>
11                         <descriptorRef>jar-with-dependencies</descriptorRef>
12                     </descriptorRefs>
13                 </configuration>
14                 <executions>
15                     <execution>
16                         <id>make-assembly</id>
17                         <phase>package</phase>
18                         <goals>
19                             <goal>single</goal>
20                         </goals>
21                     </execution>
22                 </executions>
23             </plugin>

打包成功後會生成meventest-0.0.1-SNAPSHOT-jar-with-dependencies.jar文件,這個jar包中包含依賴的編譯後的jar文件、meventest-0.0.1-SNAPSHOT.jar不包含。

meventest-0.0.1-SNAPSHOT-jar-with-dependencies.jar打開後的目錄如下所示:

技術分享圖片

四、maven-bundle-plugin

  • 該插件可以把項目依賴的jar包打包到項目的根目錄,前提是必須加上必須加上<Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>這個配置
 1         <groupId>com.ht.maventest</groupId>
 2         <artifactId>meventest</artifactId>
 3         <version>0.0.1-SNAPSHOT</version>
 4         <!-- 必須加上packaging這個節點 -->
 5         <packaging>bundle</packaging>
 6 
 7 
 8             <plugin>
 9                 <groupId>org.apache.felix</groupId>
10                 <artifactId>maven-bundle-plugin</artifactId>
11                 <version>3.2.0</version>
12                 <extensions>true</extensions>
13                 <configuration>
14                     <instructions>
15                         <Embed-Dependency>*;scope=compile|runtime;inline=false</Embed-Dependency>
16                     </instructions>
17                     <archive>
18                         <manifestEntries>
19                             <Bundle-ManifestVersion>2</Bundle-ManifestVersion>
20                             <Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name>
21                             <Bundle-SymbolicName>${project.groupId}.${project.artifactId}
22                             </Bundle-SymbolicName>
23                             <Bundle-Version>${project.version}</Bundle-Version>
24                             <Bundle-Vendor>${project.groupId}</Bundle-Vendor>
25                             <Bundle-Activator>com.ht.pojo.Activator</Bundle-Activator>
26                             <Export-Package>com.ht.pojo</Export-Package>
27                             <Import-Package>org.osgi.framework</Import-Package>
28                         </manifestEntries>
29                     </archive>
30                 </configuration>
31             </plugin>

打包成功後的解壓目錄如下所示:

技術分享圖片

maven插件詳解