常用MAVEN打包方式總結
幾種常用的maven打包外掛總結:
一、自帶外掛:
maven自帶的核心外掛為Build plugins和Reporting plugins。
mvn compile編譯原始碼實際上就利用到了maven-compiler-plugin,其他phase也類似用到了相應的外掛
關於maven自帶的核心外掛見:http://maven.apache.org/plugins/index.html
核心外掛 maven-compiler-plugin
參考地址 http://maven.apache.org/plugins/maven-compiler-plugin/
從3.0版本開始,編譯工具預設使用的是 javax.tools.JavaCompiler(從JDK 1.6開始) 如果要強制使用javac來進行編譯,需要新增引數forceJavacCompilerUse
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<encoding>utf-8</encoding>
<source>1.7</source>
<!-- 預設1.5 -->
<target>1.7</target>
<!-- 預設1.5 -->
<compilerArgs>
<!-- 傳遞編譯引數 -->
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
<fork>true</fork>
<!-- 配置編譯記憶體引數 -->
<meminitial>128m</meminitial>
<!-- 初始記憶體 -->
<maxmem>512m</maxmem>
<!-- 最大記憶體 -->
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<!-- 指定編譯的java庫 -->
<compilerVersion>1.3</compilerVersion>
<!-- 指定編譯版本 -->
</configuration>
</plugin>
配置source 和target時,也可以這樣配置 實際上這是javac指令可以接受的引數
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
二、直接打成可執行的jar包。
這種打包方式比較粗暴,將應用的依賴包和程式包打成一個全量包。包會相對較大,但是好處也很明顯,不用管依賴包。所以這種方式只適用於比較小的專案,比如搭建微服務這種方式可以適用。
附上關鍵的伺服器執行程式碼
java -jar dataCleaner.jar 1>/home/credit/app/tracefile 2>/home/credit/app/errorfile &
說明: 最後面的& 表明後臺執行。
1> 將執行日誌輸出到tracefile
2> 將錯誤日誌輸出到errorfile
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
還一種方式,可以使用spring-boot的打包外掛進行打包。一般是跟spring boot一起使用的,但是也可以單獨利用出來打成可執行的整包。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
<configuration>
<mainClass>com.ftoul.dataCleaner.DataCleanServiceProvider</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
補充一下,還有一個shade外掛也是比較常用的打fat jar包的方式
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>org.sonatype.haven.ExodusCli</Main-Class>
<Build-Number>123</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
三 將依賴包與程式碼包分開打包
這種打包方式更普遍適用。畢竟依賴的jar包在專案發展中變動會相對較小。一般可配合maven-dependency-plugin 和 maven-jar-plugin 兩個外掛打包。前者負責依賴包,後者負責程式包。
另外,附上伺服器可用的執行指令碼。
more runapp.sh
#!/bin/sh
#執行jar包
RUN_LIBS=""
#依賴jar包
SUPPORT_LIBS=""
RUN_LIB_PATH="$HOME/app/lib"
SUPPORT_LIB_PATH="$HOME/app/support"
#載入程式包
for i in ${RUN_LIB_PATH}/* ; do
RUN_LIBS=${RUN_LIBS}:$i
done
#載入依賴包
for i in ${SUPPORT_LIB_PATH}/* ; do
SUPPORT_LIBS=${SUPPORT_LIBS}:$i
done
#整合classpath
CLASSPATH=${RUN_LIBS}:${SUPPORT_LIBS}
export CLASSPATH
#呼叫java指令執行。-D輸入引數 java中可以用 System.getProperties讀取。同時指定執行入口類 SpringBootApplication 這是一個典型的Springboot的執行方式。
java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=27899,suspend=n -cp $CLASSPATH -Dspring.profiles.active=prod com.app.SpringBootApplication -D
user.timezone=GMT+08 1>/home/credit/ftoulcloud/bin/tracefile 2>/home/credit/ftoulcloud/bin/errorfile &
echo Start App Success!
assembly官網的其他幾個示例使用專案的依賴包進行打包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>src-dependencies</id>
<phase>package</phase>
<goals>
<!-- use copy-dependencies instead if you don't want to explode the sources -->
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<classifier>sources</classifier>
<failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
<outputDirectory>${project.build.directory}/sources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
將指定的包打入依賴包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
</execution>
</executions>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</plugin>
</plugins>
</build>
一個可用的示例
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<!-- <version>2.10</version> -->
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>export</outputDirectory> <!-- 將依賴包放入export資料夾 -->
<excludeTransitive>false</excludeTransitive>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
四、maven-jar-plugin 將指定的一些檔案打包成jar包
這個比較簡單。就將指定的檔案打成jar包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration> <!-- manifest配置資訊 主要是可以配置主執行類。有主執行類,可以用java-jar直接執行。沒有的話就需要指定執行類 -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>support/</classpathPrefix>
<mainClass>com.myapp.MyAppApplication</mainClass>
<!-- 可以按上面的方式自己配置,也可以指定MF檔案打包。 -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>myapp1-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>myapp</classifier>
<includes>
<include>com/myapp/**</include>
<include>mybatis/**</include>
<include>templates/**</include>
<include>*.properties</include>
<include>dubbo.xml</include>
</includes>
</configuration>
</execution>
<execution>
<id>myapp2-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>myapp2</classifier>
<includes>
<include>com/myapp2/crawler/*</include>
<include>com/myapp2/crawler/*</include>
<include>com/myapp2/utils/**</include>
<include>log4j.properties</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
五、其他豐富的三方外掛
PMD打包外掛。 PMD一個很好用的靜態程式碼檢查外掛, eclipse可以直接安裝外掛使用
生成PMD報告
http://maven.apache.org/plugins/maven-pmd-plugin/
只能用於3.3.3以後的maven版本
分析JSP頁面
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<language>jsp</language>
<rulesets>
<ruleset>jsp-basic</ruleset>
</rulesets>
<includes>
<include>**/*.jsp</include>
</includes>
<compileSourceRoots>
<compileSourceRoot>${basedir}/src/main/webapp</compileSourceRoot>
</compileSourceRoots>
</configuration>
</plugin>
</plugins>
</reporting>
分析JS
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<language>javascript</language>
<rulesets>
<ruleset>ecmascript-basic</ruleset>
<ruleset>ecmascript-braces</ruleset>
<ruleset>ecmascript-unnecessary</ruleset>
</rulesets>
<includes>
<include>**/*.js</include>
</includes>
<compileSourceRoots>
<compileSourceRoot>${basedir}/src/main/javascript</compileSourceRoot>
</compileSourceRoots>
</configuration>
</plugin>
</plugins>
</reporting>
程式碼非法檢查
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
制定JDK
<reporting>
<plugins>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<configuration>
<targetJdk>1.6</targetJdk>
</configuration>
</plugin>
</plugins>
</reporting>
刪除PMD報告
<reporting>
<plugins>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.8</version>
<reportSets>
<reportSet>
<reports>
<report>pmd</report>
<report>cpd</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
沒事可以經常去官網轉轉,時不時有些新的打包外掛出來。 比如PDF外掛 GPG簽名外掛 TOMCAT外掛 JETTY外掛 等。 好多好多。用時慢慢去了解。