1 maven項目打包的插件有3種
maven-jar-plugin
maven-assembly-plugin
maven-shade-plugin
2 maven-jar-plugin
現在要新增一個Premain-Class屬性,配置如下:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <useUniqueVersions>false</useUniqueVersions> <classpathPrefix>lib/</classpathPrefix> <mainClass>cn.mymaven.test.TestMain</mainClass> </manifest>
<manifestEntries> <Premain-Class> com.xzq.test.PreAgent </Premain-Class> </manifestEntries>
</archive> </configuration> </plugin> </plugins> </build>
01.指定manfestFile位置:具體配置如下:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project> 02. 使用maven-jar-plugin 修改 MANIFEST.MF文件,具體代碼如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
所有 Maven 插件通過一個 <configuration> 元素公布了其配置,在本例中,maven-jar-plugin 修改它的 archive 屬性,特別是存檔文件的 manifest 屬性,它控制 MANIFEST.MF 文件的內容。包括 3 個元素:
addClassPath:將該元素設置為 true 告知maven-jar-plugin添加一個Class-Path元素到 MANIFEST.MF 文件,以及在Class-Path元素中包括所有依賴項。classpathPrefix:如果您計划在同一目錄下包含有您的所有依賴項,作為您將構建的 JAR,那么您可以忽略它;否則使用classpathPrefix來指定所有依賴 JAR 文件的前綴。在清單 1 中,classpathPrefix指出,相對存檔文件,所有的依賴項應該位於 “lib” 文件夾。mainClass:當用戶使用lib命令執行 JAR 文件時,使用該元素定義將要執行的類名。上述可以通過是用maven-dependency-plugin將依賴包添加進去
lib 文件夾。為此,使用
maven-dependency-plugin。代碼如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>install</phase> <goals><goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions></plugin>
maven-dependency-plugin 有一個 copy-dependencies,目標是將您的依賴項復制到您所選擇的目錄。本例中,我將依賴項復制到 build 目錄下的 lib 目錄(project-home/target/lib)。
將您的依賴項和修改的 MANIFEST.MF 放在適當的位置后,您就可以用一個簡單的命令啟動應用程序:
java -jar jarfilename.jar
spring-boot-maven-plugin插件的作用,參考:http://www.cnblogs.com/acm-bingzi/p/mavenSpringBootPlugin.html
3 maven-assembly-plugin
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <!-- 此處指定main方法入口的class --> <mainClass>com.panda521.SpringPracServerApplication</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>assembly</goal> </goals> </execution> </executions> </plugin>
使用 maven assembly:assembly進行打包操作
4 maven-shade-plugin
使用maven-shade-plugin 的注意項
maven-shade-plugin插件有個配置屬性:createDependencyReducedPom,默認值為true.
如果你用這個插件來deploy,或者發布到中央倉庫
這個屬性會縮減你的pom文件,會把你依賴的<dependency>干掉,正確的做法是把這個值改成false
<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> <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.panda521.SpringPracServerApplication</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
參考maven官方文檔:
[http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html#](http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html#)
5 maven插件使用的注意項
pluginManagement標簽
pluginManagement標簽的配置是方便子項目引用的,
但是在idea2017沒法顯示maven引入插件,所以只能注釋了,
至於它跟plugins標簽有什么區別,百度看看就一目了然了
