知识大全 用shell和java实现自动部署

Posted 文件

篇首语:对所学知识内容的兴趣可能成为学习动机。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 用shell和java实现自动部署相关的知识,希望对你有一定的参考价值。

  公司用到了rackspace的cloudserver 做为压力测试的服务器 但是一旦启动就开始收费 一个小时 刀 因此要求用的时候打开 不用的时候关掉

  但是遇到一个问题就是 这样的话在上边部署应用程序和软件就比较麻烦了 不能每次都重新部署吧 那样效率太低了 因此想到写一些脚本进行自动的部署 其他的都好办 但是有些需要修改xml文件和properties文件的 用shell实现起来就费力了

  既然是java的应用就用java来实现吧 用了一下午写好了 下边我把修改properties和xml的代码贴出来 希望对大家有所帮助

   修改properties文件

  package util xml;

  import java io File;

  import java io FileInputStream;

  import java io FileOutputStream;

  import java io IOException;

  import java util List;

  import java util Properties;

  import java util Date;

  import java text SimpleDateFormat;

  import java util ArrayList;

  import java util List;

  public class ModifyPropertiesFile

  public String propertyFilePath= remoting_client properties ;

  public String stagingIp= ;

  public String messageIp= ;

  public void modifyPropertiesFile()

  

  try

  Properties p = new Properties();

  File f = new File(propertyFilePath);

  List<String> list = new ArrayList<String>();

  list add(stagingIp);

  list add(messageIp);

  if(f exists() && f isFile())

  

  FileInputStream fis;

  fis = new FileInputStream(f);

  p load(fis);

  p setProperty( service online url socket:// +list get( )+ : );

  p setProperty( ssage url socket:// +list get( )+ : );

  p setProperty( service account url socket:// +list get( )+ : );

  p setProperty( service order url socket:// +list get( )+ : );

  p setProperty( service party url socket:// +list get( )+ : );

  p setProperty( service product url socket:// +list get( )+ : );

  p setProperty( service webimgateway url socket:// +list get( )+ : );

  p setProperty( service dirtyword url socket:// +list get( )+ : );

  FileOutputStream   oFile   =   new   FileOutputStream(f);

  p store(oFile );

  fis close();

  oFile close();

  

  

  catch (Exception e)

  

  e printStackTrace();

  e getMessage();

  System out println( Can t modify this properties file please check your program again!!!! );

  

  

  public   static   void   main(String   args[])

  

  ModifyPropertiesFile  t=new  ModifyPropertiesFile();

  t modifyPropertiesFile();

  

  

   修改xml文件

  package util xml;

  import java io File;

  import java io FileInputStream;

  import java io FileOutputStream;

  import java io IOException;

  import java util List;

  import java util Properties;

  import java util Date;

  import java text SimpleDateFormat;

  import jdom Document;

  import jdom Element;

  import jdom JDOMException;

  import jdom input SAXBuilder;

  import jdom output XMLOutputter;

  import jdom output Format;

  import jdom xpath XPath;

  import apache log j Logger;

  import xml sax EntityResolver;

  import xml sax InputSource;

  import xml sax SAXException;

  import java util HashMap;

  import java util Iterator;

  public class ModifyHibernateXml

  private String dataBaseInfo;

  private String userName;

  private String userPass;

  private String deploymentTitle;

  private String c p max_size;

  private String fileName;

  //defind propertis file name

  public String propertyFilePath= ModifyXml properties ;

  public  void  modifyXml()

  

  try

  

  Properties p = new Properties();

  HashMap<String String> map=new HashMap<String String>();

  map put( xmlFileName propertyFilePath);

  File f = new File(map get( xmlFileName ));

  // load properties value from properties file

  if(f exists() && f isFile())

  

  FileInputStream fis;

  fis = new FileInputStream(f);

  p load(fis);

  dataBaseInfo   =p getProperty( nnection url );

  userName       =p getProperty( nnection username );

  userPass       =p getProperty( nnection password );

  deploymentTitle=p getProperty( deployment title );

  c p max_size  =p getProperty( hibernate c p max_size );

  fileName       =p getProperty( nfigrtion path );

  fis close();

  

  SAXBuilder builder = new SAXBuilder();

  Document doc = builder build(fileName);

  Element root = doc getRootElement();

  XMLOutputter xmlOutputer = new XMLOutputter();

  builder setValidation(false);

  builder setEntityResolver(new EntityResolver()

  public InputSource resolveEntity(String publicId

  String systemId) throws SAXException IOException

  return new InputSource( /hibernate configuration dt

  d );

  

  );

  // use XPath search your need node

  List list = XPath selectNodes(root /hibernate configuration/session factory/property );

  for(int i = ;i<list size();i++)

  

  Element disk_element = (Element) list get(i);

  String name= disk_element getAttributue( name );

  if  (name equals( connection url ))

  

  System out println( database connection info + +name);

  disk_element setText(dataBaseInfo);

  

  else if (name equals( connection username ))

  

  System out println( database connection username + +name);

  disk_element setText(userName);

  

  else if (name equals( connection password ))

  

  System out println( database connection password + +name);

  disk_element setText(userPass);

  

  else if (name equals( c p max_size ))

  

  System out println( database max connnection is  +c p max_size);

  disk_element setText(c p max_size);

  

  

  //      If you want to take out blank line you can unment format mect

  //      Format format = Format getPrettyFormat();

  //      format setEncoding( UTF );

  //      xmlOutputer setFormat(format);

  xmlOutputer output(doc new FileOutputStream(fileName));

  catch (Exception e)

  

  e printStackTrace();

  e getMessage();

  System out println( Can t modify xml file please check your program again!!!! );

  

  

  public static void main(String[] args)

  

  ModifyHibernateXml  hibernate =new ModifyHibernateXml();

  hibernate modifyXml();

  

  

   最后可以写一个总的类 来调用以上两个类 实现模块话 厉害吧就几个文件还来了一个模块化

  package util xml;

  public class CloudServer

  

  public   static   void   main(String   args[])

  

  ModifyPropertiesFile  t=new  ModifyPropertiesFile();

  t modifyPropertiesFile();

  ModifyHibernateXml  hibernate =new ModifyHibernateXml();

  hibernate modifyXml();

  

cha138/Article/program/Java/hx/201311/26690

相关参考