種常用的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來進行編譯,需要添加參數(shù)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>
<!-- 傳遞編譯參數(shù) -->
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
<fork>true</fork>
<!-- 配置編譯內(nèi)存參數(shù) -->
<meminitial>128m</meminitial>
<!-- 初始內(nèi)存 -->
<maxmem>512m</maxmem>
<!-- 最大內(nèi)存 -->
<executable>${JAVA_1_4_HOME}/bin/javac</executable>
<!-- 指定編譯的java庫 -->
<compilerVersion>1.3</compilerVersion>
<!-- 指定編譯版本 -->
</configuration>
</plugin>
配置source 和target時,也可以這樣配置 實際上這是javac指令可以接受的參數(shù)
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
二、直接打成可執(zhí)行的jar包。
這種打包方式比較粗暴,將應用的依賴包和程序包打成一個全量包。包會相對較大,但是好處也很明顯,不用管依賴包。所以這種方式只適用于比較小的項目,比如搭建微服務這種方式可以適用。
附上關鍵的服務器執(zhí)行代碼
java -jar dataCleaner.jar 1>/home/credit/app/tracefile 2>/home/credit/app/errorfile &
說明: 最后面的& 表明后臺執(zhí)行。
1> 將運行日志輸出到tracefile
2> 將錯誤日志輸出到errorfile
具體參數(shù)很多,參考官網(wǎng)頁面
官網(wǎng)頁面 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
還一種方式,可以使用spring-boot的打包插件進行打包。一般是跟spring boot一起使用的,但是也可以單獨利用出來打成可執(zhí)行的整包。
<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包在項目發(fā)展中變動會相對較小。一般可配合maven-dependency-plugin 和 maven-jar-plugin 兩個插件打包。前者負責依賴包,后者負責程序包。
另外,附上服務器可用的執(zhí)行腳本。
more runapp.sh
#!/bin/sh
#執(zhí)行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
#調(diào)用java指令執(zhí)行。-D輸入?yún)?shù) java中可以用 System.getProperties讀取。同時指定執(zhí)行入口類 SpringBootApplication 這是一個典型的Springboot的執(zhí)行方式。
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官網(wǎng)的其他幾個示例使用項目的依賴包進行打包
<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配置信息 主要是可以配置主執(zhí)行類。有主執(zhí)行類,可以用java-jar直接執(zhí)行。沒有的話就需要指定執(zhí)行類 -->
<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一個很好用的靜態(tài)代碼檢查插件, 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>
沒事可以經(jīng)常去官網(wǎng)轉(zhuǎn)轉(zhuǎn),時不時有些新的打包插件出來。 比如PDF插件 GPG簽名插件 TOMCAT插件 JETTY插件 等。 好多好多。用時慢慢去了解。
言:
其他資源可以有兩種解讀:
這里以圖標為例:
阿里圖標官網(wǎng): https://www.iconfont.cn/
在阿里圖標官網(wǎng)下載你想要的字體圖標
<span class="iconfont icon-weixin"></span>
<span class="iconfont icon-ziyuan"></span>
<span class="iconfont icon-wodejianying"></span>
<span class="iconfont icon-shouyejianying"></span>
<span class="iconfont icon-sirendingzhi"></span>
說明:
代碼如下:
今年國慶假期終于可以憋在家里了不用出門了,不用出去看后腦了,真的是一種享受。這么好的光陰怎么浪費,睡覺、吃飯、打豆豆這怎么可能(耍多了也煩),完全不符合我們程序員的作風,趕緊起來把文章寫完。
這篇文章比較基礎,在國慶期間的業(yè)余時間寫的,這幾天又完善了下,力求把更多的前端所涉及到的關于文件上傳的各種場景和應用都涵蓋了,若有疏漏和問題還請留言斧正和補充。
以下是本文所涉及到的知識點,break or continue ?
原理很簡單,就是根據(jù) http 協(xié)議的規(guī)范和定義,完成請求消息體的封裝和消息體的解析,然后將二進制內(nèi)容保存到文件。
我們都知道如果要上傳一個文件,需要把 form 標簽的enctype設置為multipart/form-data,同時method必須為post方法。
那么multipart/form-data表示什么呢?
multipart互聯(lián)網(wǎng)上的混合資源,就是資源由多種元素組成,form-data表示可以使用HTML Forms 和 POST 方法上傳文件,具體的定義可以參考RFC 7578。
multipart/form-data 結構
看下 http 請求的消息體
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryDCntfiXcSkPhS4PN 表示本次請求要上傳文件,其中boundary表示分隔符,如果要上傳多個表單項,就要使用boundary分割,每個表單項由———XXX開始,以———XXX結尾。
每一個表單項又由Content-Type和Content-Disposition組成。
Content-Disposition: form-data 為固定值,表示一個表單元素,name 表示表單元素的 名稱,回車換行后面就是name的值,如果是上傳文件就是文件的二進制內(nèi)容。
Content-Type:表示當前的內(nèi)容的 MIME 類型,是圖片還是文本還是二進制數(shù)據(jù)。
解析
客戶端發(fā)送請求到服務器后,服務器會收到請求的消息體,然后對消息體進行解析,解析出哪是普通表單哪些是附件。
可能大家馬上能想到通過正則或者字符串處理分割出內(nèi)容,不過這樣是行不通的,二進制buffer轉(zhuǎn)化為string,對字符串進行截取后,其索引和字符串是不一致的,所以結果就不會正確,除非上傳的就是字符串。
不過一般情況下不需要自行解析,目前已經(jīng)有很成熟的三方庫可以使用。
至于如何解析,這個也會占用很大篇幅,后面的文章在詳細說。
使用 form 表單上傳文件
在 ie時代,如果實現(xiàn)一個無刷新的文件上傳那可是費老勁了,大部分都是用 iframe 來實現(xiàn)局部刷新或者使用 flash 插件來搞定,在那個時代 ie 就是最好用的瀏覽器(別無選擇)。
DEMO
這種方式上傳文件,不需要 js ,而且沒有兼容問題,所有瀏覽器都支持,就是體驗很差,導致頁面刷新,頁面其他數(shù)據(jù)丟失。
HTML
<form method="post" action="http://localhost:8100" enctype="multipart/form-data">
選擇文件:
<input type="file" name="f1"/> input 必須設置 name 屬性,否則數(shù)據(jù)無法發(fā)送<br/>
<br/>
標題:<input type="text" name="title"/><br/><br/><br/>
<button type="submit" id="btn-0">上 傳</button>
</form>
復制代碼
服務端文件的保存基于現(xiàn)有的庫koa-body結合 koa2實現(xiàn)服務端文件的保存和數(shù)據(jù)的返回。
在項目開發(fā)中,文件上傳本身和業(yè)務無關,代碼基本上都可通用。
在這里我們使用koa-body庫來實現(xiàn)解析和文件的保存。
koa-body 會自動保存文件到系統(tǒng)臨時目錄下,也可以指定保存的文件路徑。
然后在后續(xù)中間件內(nèi)得到已保存的文件的信息,再做二次處理。
NODE
/**
* 服務入口
*/
var http = require('http');
var koaStatic = require('koa-static');
var path = require('path');
var koaBody = require('koa-body');//文件保存庫
var fs = require('fs');
var Koa = require('koa2');
var app = new Koa();
var port = process.env.PORT || '8100';
var uploadHost= `http://localhost:${port}/uploads/`;
app.use(koaBody({
formidable: {
//設置文件的默認保存目錄,不設置則保存在系統(tǒng)臨時目錄下 os
uploadDir: path.resolve(__dirname, '../static/uploads')
},
multipart: true // 開啟文件上傳,默認是關閉
}));
//開啟靜態(tài)文件訪問
app.use(koaStatic(
path.resolve(__dirname, '../static')
));
//文件二次處理,修改名稱
app.use((ctx) => {
var file = ctx.request.files.f1;//得道文件對象
var path = file.path;
var fname = file.name;//原文件名稱
var nextPath = path+fname;
if(file.size>0 && path){
//得到擴展名
var extArr = fname.split('.');
var ext = extArr[extArr.length-1];
var nextPath = path+'.'+ext;
//重命名文件
fs.renameSync(path, nextPath);
}
//以 json 形式輸出上傳文件地址
ctx.body = `{
"fileUrl":"${uploadHost}${nextPath.slice(nextPath.lastIndexOf('/')+1)}"
}`;
});
/**
* http server
*/
var server = http.createServer(app.callback());
server.listen(port);
console.log('demo1 server start ...... ');
復制代碼
CODE
https://github.com/Bigerfe/fe-learn-code/
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。