思路介紹:css
cmd生成新的工程文件(如下簡稱目標工程);html
將當前正在開發的項目的app下面的文件copy到目標工程,並在copy過程當中將對應的文件名放到app.json和bootstrap.json裏面,將resources下面的文件所有移動到目標工程並將對應的css文件名放到app.json和bootstrap.json裏面,並將lib下面的第三方js文件放到app.json下面一遍打包後能被項目找到;java
將resources下面的圖片所有放進緩存;web
經過當前項目的app.js改變目標項目的app.js;json
修改index.html文件(由於有時候咱們會在項目中用到第三方的js包並在引入是配置相關參數,這時候咱們就不免會在index.html中加入對應的js文件,好比百度地圖的js文件須要配置key等等);bootstrap
因爲cmd打包本地應用還存在問題。咱們本版本就先忽略本地應用,主要打包類型爲production,package;緩存
用java執行cmd打包的bat命令;tomcat
源碼下載地址:http://pan.baidu.com/s/1hq1IFzu服務器
你能夠加入交流羣:377140502app
部分關鍵代碼以下:
/** * 比對和轉換app.json和bootstrap.json * @author AvenGang * */ public class App_bootstrap_JSON { /** * 單例 * @return */ public static App_bootstrap_JSON getInstance() { return abj; } /** * 替換app.json和bootstrap.json * @param tag 目標路徑 * @throws IOException */ public void replace(String tag) throws IOException { //找到目標項目的app.json文件和bootstrap.json File tarFile = new File(tag + File.separator + "app.json"); File tarBootstrapFile = new File(tag + File.separator + "bootstrap.json"); String jsStr = ""; if(js.lastIndexOf(",") > -1) { jsStr = js.substring(0, js.lastIndexOf(","));//去掉多餘的逗號 } jsStr += "\r\n],\r\n";//添加結尾 String cssStr = ""; if(css.lastIndexOf(",") > -1) { cssStr = css.substring(0, css.lastIndexOf(","));//去掉多餘的逗號 } String bootstrapCssStr = cssStr + "\r\n]\r\n"; cssStr += "\r\n],\r\n";//添加結尾 String appCacheStr = ""; if(appCache.lastIndexOf(",") > -1) { appCacheStr = appCache.substring(0, appCache.lastIndexOf(","));//去掉多餘的逗號 } //添加結尾 appCacheStr += "\r\n\t" + "],\r\n\t" + "\"network\": [\r\n\t\t" + "\"*\"\r\n\t" + "],\r\n\t" + "\"fallback\": []\r\n" + "},\r\n"; String jsonStr = FileUtil.file2Str(tarFile); String preStr = jsonStr.substring(0, jsonStr.indexOf("\"js\":")); String postStr = jsonStr.substring(jsonStr.indexOf("\"ignore\":")); preStr += jsStr; preStr += cssStr; preStr += appCacheStr; preStr += resources; PrintWriter pw = new PrintWriter(tarFile); //app.json寫數據 pw.write(preStr+postStr); pw.close(); String idStr = jsonStr.substring(jsonStr.indexOf("\"id\":"),jsonStr.length() - 1); PrintWriter pw1 = new PrintWriter(tarBootstrapFile); String str = "{"+idStr+","+jsStr+bootstrapCssStr+"}"; //bootstrap.json寫數據 pw1.write(str); pw1.close(); } public void addJs(String libJsName) { if(libJsName.contains(".svn")) { return; } if(!libJsName.endsWith(".js")) { return; } String str = "\r\n\t" + "{\r\n\t\t" + "\"path\" : \"resources/lib/%1$s\"\r\n\t" + "},"; str = String.format(str, libJsName); js.append(str); } public void addCss(String cssName) { if(cssName.contains(".svn")) { return; } if(!cssName.endsWith(".css")) { return; } String str = "\r\n\t" + "{\r\n\t\t" + "\"path\" : \"resources/css/%1$s\"\r\n\t" + "},"; str = String.format(str, cssName); css.append(str); } public void addAppCache(String cachePathName) { if(cachePathName.contains(".svn")) { return; } String str = "\"%1$s\",\r\n\t\t"; str = String.format(str, cachePathName); appCache.append(str); } private App_bootstrap_JSON() { initJs(); initCss(); initAppCache(); initResources(); } /** * 初始化,將系統不變的js文件配置到app.json裏面sencha-touch.js */ private void initJs() { js.append("\"js\": [\r\n\t" + "{\r\n\t\t" + "\"path\": \"touch/sencha-touch.js\",\r\n\t\t" + "\"x-bootstrap\": true\r\n\t" + "},\r\n\t" + "{\r\n\t\t" + "\"path\": \"bootstrap.js\",\r\n\t\t" + "\"x-bootstrap\": true\r\n\t" + "},\r\n\t" + "{\r\n\t\t" + "\"path\": \"app.js\",\r\n\t\t" + "\"bundle\": true,\r\n\t\t" + "\"update\": \"full\"\r\n\t" + "},"); } public void initCss() { css.append("\"css\": [\r\n\t"); } public void initAppCache() { appCache.append("\"appCache\": {\r\n\t" + "\"cache\": [\r\n\t\t" + "\"index.html\",\r\n\t\t"); } public void initResources() { resources.append("\"resources\": [\r\n\t"); resources.append("\"resources/images\",\r\n\t\t"); resources.append("\"resources/icons\",\r\n\t\t"); resources.append("\"resources/startup\",\r\n\t\t"); resources.append("\"resources/lib\"\r\n\t"); resources.append("],"); } private static StringBuffer js = new StringBuffer(); private static StringBuffer css = new StringBuffer(); private static StringBuffer appCache = new StringBuffer(); private static StringBuffer resources = new StringBuffer(); private static App_bootstrap_JSON abj = new App_bootstrap_JSON(); } /** * 修改app.js文件 * @author AvenGang * */ public class App_JS { public static void replace(String src, String tag) throws IOException { File tagFile = new File(tag + File.separator + "app.js"); File srcFile = new File(src + File.separator + "app.js"); String srcJsonStr = FileUtil.file2Str(srcFile); String tagJsonStr = FileUtil.file2Str(tagFile); String iconStr = tagJsonStr.substring(tagJsonStr.indexOf("icon:")) .split("}")[0] + "},"; String str = "requires:['Ext.MessageBox',"; if (srcJsonStr.indexOf("requires:[") > -1) { srcJsonStr = srcJsonStr.replace("requires:[",str); } else if (srcJsonStr.indexOf("requires :[") > -1) { srcJsonStr = srcJsonStr.replace("requires :[",str); } else if (srcJsonStr.indexOf("requires: [") > -1) { srcJsonStr = srcJsonStr.replace("requires: [",str); } else if (srcJsonStr.indexOf("requires : [") > -1) { srcJsonStr = srcJsonStr.replace("requires : [",str); } else {//沒有required屬性 } String post_preStr = tagJsonStr.substring( tagJsonStr.indexOf("isIconPrecomposed:"), tagJsonStr.indexOf("launch:")); if (srcJsonStr.indexOf("icon:") > -1) { iconStr = srcJsonStr.substring(srcJsonStr.indexOf("icon:")).split( "}")[0] + "},"; } else if (srcJsonStr.indexOf("icon :") > -1) { iconStr = srcJsonStr.substring(srcJsonStr.indexOf("icon :")).split( "}")[0] + "},"; } else { iconStr = ""; } String onUpdatedStr = tagJsonStr.substring(tagJsonStr .indexOf("onUpdated:")); String endStr = iconStr + post_preStr + onUpdatedStr; srcJsonStr.replace("});", endStr); PrintWriter pw = new PrintWriter(tagFile); pw.write(srcJsonStr); pw.close(); } } /** * 修改index.html的內容 * @author AvenGang * */ public class INDEX_HTML { public static void replace(String src, String tag) throws FileNotFoundException { File srcFile = new File(src + File.separator + "index.html"); File tagFile = new File(tag + File.separator + "index.html"); String srcJsonStr = FileUtil.file2Str(srcFile); ArrayList<String> lists = StringUtil.getValueBetweenTwoMarkers(srcJsonStr, "<!--cmd-->", "<!--/cmd-->"); String tagJsonStr = FileUtil.file2Str(tagFile); StringBuffer sb = new StringBuffer(); for(String str : lists) { sb.append(str+"\r\n"); } tagJsonStr = tagJsonStr.replace("</head>", sb.toString() + "</head>"); PrintWriter pw = new PrintWriter(tagFile); pw.write(tagJsonStr); pw.close(); } }
10.效果圖:
把這個文件夾放到tomcat等服務器下面就能在網上訪問了,怎麼樣,是否是以爲比之前方便多了呢!