Java集成开发环境的一种配置方案(转)

先说说Tomcat应用的发布细节,这当然也是大家都比较明白的问题了。
    Tomcat是如何发现我们应用的,有三种方式!第一:你在webapps目录下新建一个发布目录,再把项目webapp文件夹下的内容全部拷贝过去,发 布完毕;第二:你把webapp文件夹的在文件系统上的路径复制好,然后在tomcat\conf\Catalina\localhost下新建一个 XML配置文件,文件命名最好和项目一致,方便查找嘛。内容如下:

Xml代码  收藏代码
  1. < Context   docBase = "F:/MyDev/workspace/OA/webapp"   path = "/oa"   reloadable = "true" >   
  2. </ Context >   
<Context docBase="F:/MyDev/workspace/OA/webapp" path="/oa" reloadable="true">
</Context>


    Context是应用上下文,docBase是项目路径,path是在服务器上的访问地址,这里就是http://localhost:8080/oa来 访问了。reloadable是指类路径下有更新是是否进行热部署,什么是热部署,google一下好吧。
    第三种方法:把上面这段复制到tomcat\conf下的server.xml中,位置是倒数第五行那对host里面即可。
    一般情况用第二种是比较方便的,开发的目录不用动,写个配置文件Tomcat就能找到了。很方便。在下面的集成开发环境中,就是用的是第二种方式
    下面开始讲解一种Java集成开发环境的配置
    大部分的Java开发者使用Eclipse作为IDE,当然这其中也有很大一部分使用MyEclipse,因为它方便,集成了开发所需的一些组件,但是问 题是启动速度慢,插件太多占用内存太大。基于以上状况,需要根据个人使用状况来定制一套开发环境,满足够用,最小占用的原则,下面先展示一下我的个人开发 环境。

Confbuilder文件夹内装的是和web项目发布有关的内容,后面将详细解释。
Data文件夹内装的是成品开发框架和第三方类库,如Struts,Spring,Hibernate等。
Docs文件夹内装的是API文档,可以随时查阅,平时可收集好。
Eclipse文件夹内就是eclipse了,最新版直接去官方下载即可。也可根据个人喜好决定。
Jdk文件夹内装的就是jdk了,这里单独把jdk拿出来就是这套开发环境不依赖于操作系统本身,如果机器里没有按装jdk也能使用,这套环境绝对是绿色 版的。
Tomcat内就是tomcat服务器文件了,去官方直接下载拷贝过来就行,不用安装版本的。
Workspace是eclipse的工作区,在eclipse工作区选择填入 ../ 可在这个路径下使用。
Buildconf.bat是项目发布程序。
Eclipse.bat是eclipse启动程序。
Tomcat.bat是tomcat服务器启动程序。
Svn.bat是本机SVN服务启动程序,后续将专门介绍本机SVN的安装和设置。
    下面逐一介绍安装环境的配置。
    首先是confbuilder,这里借助了freemarker的模板来生成配置文件。

    这里就可以看出buildconf.bat是执行这个class文件来工作的,template文件夹内装入的是一个模板文件,freemarker根据 这个模板来生产所需文件。模板源码如下:
    Template文件夹下的webapp.xml,其实这就是tomcat发布文件的模板,这个文件最后要发布到tomcat\conf \Catalina\localhost下。

Xml代码  收藏代码
  1. < Context   docBase = "${docBase}"   path = "${path}"   reloadable = "true" >   
  2. </ Context >   
<Context docBase="${docBase}" path="${path}" reloadable="true">
</Context>


    发布程序文件源码如下:代码不难理解,其中都是中文提示,这里不做过多解释。
    多说一句:其中的方法我们在web项目中也可以作为freemarker生成静态html页面的方法,是值得学习的。

Java代码  收藏代码
  1. import  freemarker.template.*;  
  2. import  java.io.*;  
  3. import  java.util.*;  
  4.   
  5. public   class  WebConfBuilder {  
  6.     private   static  Configuration freemarker_cfg;  
  7.     /**  
  8.      * 读 取控制台输入的内容  
  9.      */   
  10.     public   static  String readLine()  throws  IOException {  
  11.         return   new  BufferedReader( new  InputStreamReader(System.in)).readLine();  
  12.     }  
  13.     public   static   void  writeLine(String text) {  
  14.         System.out.println(text);  
  15.     }  
  16.     public   static   void  writeLine() {  
  17.         System.out.println();  
  18.     }  
  19.     public   static   void  writeText(String text) {  
  20.         System.out.print(text);  
  21.     }  
  22.     /**  
  23.      * 为 freemarker提供配置文件模板,用于生成配置文件  
  24.      */   
  25.     public   static  Configuration getFreeMarkerCFG()  throws  IOException {  
  26.         if  (freemarker_cfg ==  null ) {  
  27.             freemarker_cfg = new  Configuration();  
  28.             freemarker_cfg.setDirectoryForTemplateLoading(new  File( "template" ));  
  29.         }  
  30.         return  freemarker_cfg;  
  31.     }  
  32.   
  33.     /** 主函数 */   
  34.     public   static   void  main(String[] args)  throws  IOException {  
  35.         writeLine();  
  36.         writeLine("-------------------" );  
  37.         writeLine("Web应用自动配置系统" );  
  38.         writeLine("-------------------" );  
  39.         writeLine();  
  40.         writeText("请输入项目名称:" );  
  41.         String prjName = readLine();  
  42.         File f = new  File( "../workspace/"  + prjName);  
  43.         if  (!(f.exists())) {  
  44.             writeLine("对不起,该项目不存在。" );  
  45.             return ;  
  46.         }  
  47.         writeText("请输入Web目录名称(默认为/webapp,不需改变时请直接回车):" );  
  48.         String webName = readLine();  
  49.         if  ( "" .equals(webName)) {  
  50.             webName = "/webapp" ;  
  51.         }  
  52.         f = new  File( "../workspace/"  + prjName + webName);  
  53.         if  (!(f.exists())) {  
  54.             writeLine("对不起,输入的Web目录不存在。" );  
  55.             return ;  
  56.         }  
  57.         writeText("请输入项目的发布名称(最好全部使用小写字母):" );  
  58.         String deployName = readLine();  
  59.         if  ( "" .equals(deployName)) {  
  60.             writeLine("对不起,发布名称不能为空。" );  
  61.             return ;  
  62.         }  
  63.         if  (!(deployName.startsWith( "/" ))) {  
  64.             deployName = "/"  + deployName;  
  65.         }  
  66.   
  67.         Template t = getFreeMarkerCFG().getTemplate("webapp.xml" );  
  68.         Map root = new  HashMap();  
  69.         root.put("docBase" , f.getCanonicalPath().replaceAll( "\\\\", " /"));  
  70.         root.put("path" , deployName);  
  71.   
  72.         f = new  File( "../tomcat/conf/Catalina/localhost" );  
  73.         if  (!(f.exists())) {  
  74.             writeLine("对不起,配置文件存放路径没找到。" );  
  75.             return ;  
  76.         }  
  77.   
  78.         f = new  File( "../tomcat/conf/Catalina/localhost"  + deployName +  ".xml" );  
  79.   
  80.         Writer out = new  BufferedWriter( new  OutputStreamWriter(  
  81.                 new  FileOutputStream(f)));  
  82.         try  {  
  83.             t.process(root, out);  
  84.         } catch  (Exception e) {  
  85.             writeLine("对不起,生成错误。" );  
  86.             return ;  
  87.         }  
  88.         writeLine("文件生成成功。" );  
  89.     }  
  90. }  
import freemarker.template.*;
import java.io.*;
import java.util.*;

public class WebConfBuilder {
	private static Configuration freemarker_cfg;
	/**
	 * 读取控制台输入的内容
	 */
	public static String readLine() throws IOException {
		return new BufferedReader(new InputStreamReader(System.in)).readLine();
	}
	public static void writeLine(String text) {
		System.out.println(text);
	}
	public static void writeLine() {
		System.out.println();
	}
	public static void writeText(String text) {
		System.out.print(text);
	}
	/**
	 * 为freemarker提供配置文件模板,用于生成配置文件
	 */
	public static Configuration getFreeMarkerCFG() throws IOException {
		if (freemarker_cfg == null) {
			freemarker_cfg = new Configuration();
			freemarker_cfg.setDirectoryForTemplateLoading(new File("template"));
		}
		return freemarker_cfg;
	}

	/** 主函数 */
	public static void main(String[] args) throws IOException {
		writeLine();
		writeLine("-------------------");
		writeLine("Web应用自动配置系统");
		writeLine("-------------------");
		writeLine();
		writeText("请输入项目名称:");
		String prjName = readLine();
		File f = new File("../workspace/" + prjName);
		if (!(f.exists())) {
			writeLine("对不起,该项目不存在。");
			return;
		}
		writeText("请输入Web目录名称(默认为/webapp,不需改变时请直接回车):");
		String webName = readLine();
		if ("".equals(webName)) {
			webName = "/webapp";
		}
		f = new File("../workspace/" + prjName + webName);
		if (!(f.exists())) {
			writeLine("对不起,输入的Web目录不存在。");
			return;
		}
		writeText("请输入项目的发布名称(最好全部使用小写字母):");
		String deployName = readLine();
		if ("".equals(deployName)) {
			writeLine("对不起,发布名称不能为空。");
			return;
		}
		if (!(deployName.startsWith("/"))) {
			deployName = "/" + deployName;
		}

		Template t = getFreeMarkerCFG().getTemplate("webapp.xml");
		Map root = new HashMap();
		root.put("docBase", f.getCanonicalPath().replaceAll("\\\\", "/"));
		root.put("path", deployName);

		f = new File("../tomcat/conf/Catalina/localhost");
		if (!(f.exists())) {
			writeLine("对不起,配置文件存放路径没找到。");
			return;
		}

		f = new File("../tomcat/conf/Catalina/localhost" + deployName + ".xml");

		Writer out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(f)));
		try {
			t.process(root, out);
		} catch (Exception e) {
			writeLine("对不起,生成错误。");
			return;
		}
		writeLine("文件生成成功。");
	}
}


将这个源文件编译成class文件,然后放到集成文件的相应位置。因为它所依赖freemarker类库,所以freemarker.jar也不 可缺少,下面就是写个批处理程序来运行了。其中所定的各种路径是结合我的环境设置的,当然大家可以根据自己的偏好进行。
buildconf.bat脚本如下:
@set JAVA_HOME=%cd%\jdk
@set PATH=%JAVA_HOME%\bin
@cd confbuilder
@java -classpath .;freemarker.jar WebConfBuilder
@pause
可以看到它就是运行了这个配置程序,运行效果如下:

Data,docs和eclipse文件夹就没什么可说的了,直接拷贝过来就行了。剩下的就是积累自己的数据和文档了。下面说下jdk。之所以本 身带有jdk就是为了移植性,在任何一台windows机器上都能进行开发,而不需要硬性要求机器必须安装jdk。Jdk可以从一个安装好的目录中拷贝过 来,有如下内容即可。

Tomcat去apache下载非安装版即可,tomcat.bat如下:
@set JAVA_HOME=%cd%\jdk
@set PATH=%JAVA_HOME%\bin
@set CATALINA_HOME=%cd%\tomcat
@cd tomcat/bin
@startup.bat
Eclipse.bat如下:
@set JAVA_HOME=%cd%\jdk
@set PATH=%JAVA_HOME%\bin
@cd eclipse
@start eclipse.exe
SVN在Windows下的配置以后再进行总结。
这只是一种整合方式,当然大家也有自己的偏好,也希望有更好方案的使用者来分享经验。同时希望本文希望对使用者有所帮助。

猜你喜欢

转载自343512613-qq-com.iteye.com/blog/1140740