先註明:本人小碼農一隻,如有錯誤請見諒並指出,謝謝。
先貼下項目圖
輸出路徑
設置webapp路徑的原因是我需要修改其中的內容(這裏遇到一個坑,花了2天時間才爬出來)
主要說下用到的2個插件
- maven-antrun-plugin
這個插件的作用是可以在打包前修改文件,如刪除、移動、重命名等,當然功能不止這些,我再項目中暫時只用到這。
<profiles>
<profile>
<id>pat</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Using pat file packaging</echo>
<delete file="${project.build.outputDirectory}/configuation.properties" />
<move file="${project.build.outputDirectory}/configuation-pat.properties" tofile="${project.build.outputDirectory}/configuation.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
項目文件
運行圖
結果
可以看到文件已經被刪除和替換了(完整的應該還要貼項目中configuration-pat.properties內容和war包中configuration.properties內容是否一致,但是這樣又得截兩張圖,我嫌麻煩就不弄了)
這段代碼是配置pat環境,名字隨意。
說下代碼裏幾個地方:
<phase>compile</phase>
這個指的的maven的生命週期,compile指的是編譯,其他週期可以看下https://www.cnblogs.com/luotaoyeah/p/3819001.html這篇文章。<echo>Using pat file packaging</echo>
這個是輸出一段話,在日誌中能看到,這裏是爲了表示插件被執行了實際對打包沒影響。<delete>
刪除文件<move>
移動文件,同時也可以用作重命名,複製是copy,具體還有那些命令可以上網搜下。${project.build.outputDirectory}
這個路徑是target/class
這個插件就說到這。
下面說下我打包時遇到另一個困擾的問題。
打包時webapp下的內容也分生產和開發,但是打war包是默認是將src/main/webapp下的內容複製到war包中,但是在項目deploy時webapp中的內容是沒有發佈到target中,這就導致沒法修改。
解決方法
- 將webapp也配置輸出路徑(在上面的輸出路徑圖片中能看到),這樣就可以在上面pat的task中增加對webapp路徑下的文件進行修改。具體代碼爲:
<delete file="${project.build.directory}/webapp/views/error.html" />
<move file="${project.build.directory}/webapp/views/error-pat.html" tofile="${project.build.directory}/webapp/views/error.html"/>
這裏的${project.build.directory}
路徑爲target/
2.配置war包中的wabapp複製路徑
這裏就要用到另一個插件:maven-war-plugin
代碼:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>wbs</warName>
<webXml>${project.build.directory}/webapp/WEB-INF/web.xml</webXml>
<warSourceDirectory>target/webapp</warSourceDirectory>
</configuration>
</plugin>
其中最主要的是<warSourceDirectory>target/webapp</warSourceDirectory>
這個就是指定webapp的路徑
到這就打war時就可以自動實現pat文件的切換。