NutzBoot讀取外部資源文件功能的誕生

前因

Nutz接觸時間了,最近使用NutzBoot開發了個微服務,在準備部署的時候趕上一個問題,NutzBoot在發佈時,會將配置文件、模板文件都打包進jar包,那麼在後期作細微調整時將涉及到從新發包,本人強伯症,受不了這個,若是項目只是須要對配置作些修改,或者模板作些修改,徹底不必從新打包發佈,故而發生了以後的事情 - 改造java

過程

怎樣把配置文件打包到jar包外面?apache

修改 pom.xml <build>...</build>標籤內加入配置,過濾resources目錄app

<resources>
	<resource>
		<directory>src/main/resources</directory>
		<excludes>
			<exclude>**/*</exclude>
		</excludes>
		<filtering>true</filtering>
	</resource>
</resources>

<build><plugins>...</plugins></build>標籤內加入配置,將配置資源目錄編譯到jar包外maven

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<executions>
		<execution>
			<id>copy-resources</id>
			<phase>package</phase>
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<encoding>UTF-8</encoding>
				<outputDirectory>
					${project.build.directory}
				</outputDirectory>   <!-- 表示把配置文件拷到和jar包同一個路徑下 -->
				<resources>
					<resource>
						<directory>src/main/resources/</directory>
					</resource>
				</resources>
			</configuration>
		</execution>
	</executions>
</plugin>

此時執行mvn clean;mvn compile;mvn package;已經能夠將資源文件打包到jar包外部了。ide


那麼問題來了,項目如何讀取外部資源微服務

首先我是想辦法解決beetl模板文件的讀取,由於NutzBoot項目沒有考慮過外部讀取的狀況,因此必須通過改造,與@wendal進行討論後,採起從Setup的init方法着手,手動指定模板讀取路徑,實現以下:ui

public class MainSetup implements Setup {
    @Override
    public void init(NutConfig nc) {
        for(ViewMaker maker: nc.getViewMakers()) {
            if(maker.getClass() == BeetlViewMakerStarter.class) {
                // 獲取BeetlViewMaker對象
                BeetlViewMaker beetlViewMaker = (BeetlViewMaker)maker;
                // 生成FileResourceLoader 從文件系統取資源
                FileResourceLoader fileResourceLoader = new FileResourceLoader();
                fileResourceLoader.setCharset("UTF-8");
                // 設置jar包外部模板目錄
                fileResourceLoader.setRoot(getBasePath() + File.separator + "template");
                // 配置beetlViewMaker使用外部目錄讀取模板
                beetlViewMaker.groupTemplate.setResourceLoader(fileResourceLoader);
            }
        }
    }

    @Override
    public void destroy(NutConfig nc) {

    }
    
    // 獲取jar包當前目錄
    static String getBasePath() {
        String basePath = MainSetup.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        int lastIndex = basePath.lastIndexOf(File.separator);
        basePath = basePath.substring(0, lastIndex);
        try {
            basePath = java.net.URLDecoder.decode(basePath, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return basePath;
    }
}

外部模板讀取問題解決,就剩下讀取外部配置文件,因而跟@wendal 又進行各類溝通,最後決定修改源碼讓NutzBoot支持讀取外部配置文件,順便實現了指定配置文件目錄自動掃描,詳細步驟就不寫了,若有興趣可關注 論壇討論貼 NutzBoot 如何打包發佈.net

通過修改後,如今NutzBoot支持外部配置方式,好比將application.properties放在jar包同級目錄。 另外application.properties新增支持配置讀取其餘配置文件, 如:code

nutz.boot.configure.properties.dir=config
// 系統將自動掃描jar包同級config目錄下全部properties文件進行參數讀取

結束語

到此我外部讀取參數和模板的需求算是圓滿解決,也特別感謝@wendal給予的支持,特此記錄xml

相關文章
相關標籤/搜索