1 場(chǎng)景
1.1 功能
通過(guò)maven名稱(chēng)生成可執(zhí)行jar。
1.2 特性
(1)jar可讀取外部配置文件
(2)生成doc、執(zhí)行腳本文件
(3)打包時(shí)生產(chǎn)環(huán)境配置文件替換開(kāi)發(fā)配置文件
1.3 依賴(lài)maven插件
(1)maven-jar-plugin
(2)maven-assembly-plugin
2 實(shí)現(xiàn)
2.1 代碼目錄結(jié)構(gòu)
項(xiàng)目名稱(chēng):mysql-doc,用于生成mysql數(shù)據(jù)庫(kù)表結(jié)構(gòu)到excel
src
-main
-config
-assembly
-package.xml //[2]
-doc
-生成mysql表結(jié)構(gòu)文檔.txt
-proConfig
-log4j.properties
-mysql.properties
-scropts
-start.bat
-start.sh
-java
-com.ext.sjckall.mysql.doc.main
-MainExec //[3]
-resources
-log4j.properties
-mysql.properties
-pom.xml //[1]
2.2 文件說(shuō)明
[1]pom.xml文件配置maven插件
<build>
<finalName>mysql-doc</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<!--生成的jar中,不要包含pom.xml和pom.properties這兩個(gè)文件-->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<!--是否要把第三方j(luò)ar放到manifest的classpath中-->
<addClasspath>true</addClasspath>
<!--生成的manifest中classpath的前綴,因?yàn)橐训谌絡(luò)ar放到lib目錄下,所以classpath的前綴是lib/-->
<classpathPrefix>lib/</classpathPrefix>
<!--運(yùn)行jar包時(shí)運(yùn)行的主類(lèi),要求類(lèi)全名-->
<mainClass>com.ext.sjckall.mysql.doc.main.MainExec</mainClass>
</manifest>
<!-- classPath下增加目錄 -->
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<!--過(guò)濾掉不希望包含在jar中的文件-->
<excludes>
<!-- 排除不需要的文件夾(路徑是jar包內(nèi)部的路徑) -->
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<!-- 對(duì)項(xiàng)目的組裝進(jìn)行配置 -->
<configuration>
<!-- 指定assembly插件的配置文件所在位置 -->
<descriptors>
<descriptor>src/main/config/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 將組裝綁定到maven生命周期的哪一階段 -->
<!--<phase>package</phase>-->
<goals>
<!-- 指定assembly插件的打包方式, -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[2]package.xml文件內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<!-- 最終打包成一個(gè)用于發(fā)布的zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- 把依賴(lài)jar包打包進(jìn)Zip壓縮文件的lib目錄下 -->
<dependencySets>
<dependencySet>
<!--不使用項(xiàng)目的artifact,第三方j(luò)ar不要解壓,打包進(jìn)zip文件的lib目錄-->
<useProjectArtifact>false</useProjectArtifact>
<!-- 第三方j(luò)ar打包進(jìn)Zip文件的lib目錄下,注意此目錄要與maven-jar-plugin中classpathPrefix指定的目錄相同,不然這些依賴(lài)的jar包加載到ClassPath的時(shí)候會(huì)找不到 -->
<outputDirectory>lib</outputDirectory>
<!-- 第三方j(luò)ar不要解壓-->
<!--<unpack>false</unpack>-->
</dependencySet>
</dependencySets>
<!-- 文件設(shè)置,你想把哪些文件包含進(jìn)去,或者把某些文件排除掉,都是在這里配置-->
<fileSets>
<!-- 把項(xiàng)目自己編譯出來(lái)的可執(zhí)行jar,打包進(jìn)zip文件的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<!-- 生產(chǎn)配置文件 -->
<fileSet>
<directory>src/main/config/proConfig</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<!-- 執(zhí)行腳本目錄 -->
<fileSet>
<directory>src/main/config/scripts</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
<!-- 說(shuō)明文檔目錄 -->
<fileSet>
<directory>src/main/config/doc</directory>
<outputDirectory>doc</outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>
</fileSets>
</assembly>
[3]程序入口文件MainExec
public class MainExec {
private static final Logger LOG=Logger.getLogger(MainExec.class);
public static void main(String[] args) {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LOG.info("[生成mysql文檔]開(kāi)始時(shí)間:"+dateFormat.format(new Date()));
ExecMysqlToDoc.mainMethod();
LOG.info("[生成mysql文檔]結(jié)束時(shí)間:"+dateFormat.format(new Date()));
}
}
2.3 打包可執(zhí)行文件
項(xiàng)目根目錄下執(zhí)行如下命令:
(1)編譯項(xiàng)目
mvn clean install
(2)打包文件
mvn assembly:single
打包后生成文件:mysql-doc-full.zip,文件目錄如下:
mysql-doc
-doc
-生成mysql表結(jié)構(gòu)文檔.txt
-lib
-*.jar
-log4j.properties
-mysql.properties
-mysql-doc.jar
-start.bat
-start.sh
整理后端bat腳本如下:
C:
echo [1、項(xiàng)目編譯]
cd C:\work\ideaWsAll\trunkPro\sjckend
call mvn clean install
pause
echo [2、打包文件]
cd C:\work\ideaWsAll\trunkPro\sjckend\mysql-doc
call mvn assembly:single
pause
echo [3、復(fù)制文件]
xcopy /y /c /h /r "C:\work\mysql-doc\target\mysql-doc-full.zip" "D:\myData"
3 獲取項(xiàng)目相對(duì)路徑
3.1 輸出文件到項(xiàng)目相對(duì)路徑
項(xiàng)目運(yùn)行期間,會(huì)輸出相關(guān)文件到執(zhí)行文件絕對(duì)路徑下,采用此種方式獲取執(zhí)行jar的絕對(duì)路徑。參數(shù)為執(zhí)行代碼的class。
/**
* 獲取執(zhí)行jar路徑
* @return
*/
private static String getJarUrl(Class<?> clazz){
String filePath=clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
try {
//轉(zhuǎn)換處理中文及空格
filePath = URLDecoder.decode(filePath, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//可執(zhí)行jar包運(yùn)行的結(jié)果里包含".jar"
if (filePath.endsWith(".jar")) {
//截取路徑中的jar包名
filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
}
return filePath;
}
如需要將項(xiàng)目結(jié)果輸出到執(zhí)行jar平級(jí)的文件夾內(nèi),可以采用以下方式獲取文件夾絕對(duì)路徑:
/**
* 獲取執(zhí)行輸出目錄
* @return
*/
public static String getJarOutUrl(Class<?> clazz){
String filePath=getJarUrl(clazz);
filePath=filePath+"out/";
File file=new File(filePath);
if(!file.exists()){
file.mkdirs();
}
return filePath;
}
3.2 輸出日志文件
如需要將日志輸出到執(zhí)行jar的平級(jí)文件夾logs中,log4j.properties采用如下配置:
log4j.appender.result=org.apache.log4j.DailyRollingFileAppender
log4j.appender.result.Threshold=INFO
log4j.appender.result.file=./logs/result.log
log4j.appender.result.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.result.layout=org.apache.log4j.PatternLayout
log4j.appender.result.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [ %l ] - [ %p ]: %m%n
log4j.appender.result.Append=true
log4j.appender.result.encoding=UTF-8
3.3 讀取外部配置文件
讀取外部properties配置文件,可采用如下方式:
Properties properties = new Properties();
properties.load(MysqlConfigContent.class.getClassLoader().getResourceAsStream("./mysql.properties"));