Java compilation automation for operation and maintenance automation

Source: Internet
Author: User

"Requirements description"
in order for code to develop, it is necessary to automate the code compilation and deployment process by improving the efficiency of testing and deployment on-line.
here's how to use Javac Automation to compile Java code.
because the project is currently not using Ant, Maven and other management constructs, so need to use the comparison of the original javac to achieve, very hurt wood.

"Background notes"
Code directory Structure
1. Web type code: Reach the War package (on one directory), deploy with Tomcat

Introduction to the project directory structure for Web types
# Ls/home/souces/boss-web
src webcontent
src store Java source code: Java files, properties files, XML files, etc.
WebContent Store Html,js,css and other documents
 
2, service type of code, will eventually be hit as a jar package deployment

The Lib directory holds the jar packages that the Java project relies on
src store Java source code: Java files, properties files, XML files, etc.

PS: Well, this is a relatively primitive project structure, the latter to promote the development of the use of MAVEN, although not maven, but as an operation, to see recruit demolition Ah, so also in the current situation to give the program drops.

3, compile directory structure comb
/home/build/Store Compilation script: Build.sh
/home/sources/Storing source code
/home/target/to store compiled "directories"
/home/tgzs/Store tgz package after compiling and compressing.
After each release of the tgz integration of Tgz and config using code, you can publish the
test-web.20140826234520.p_3547.tgz
config_2.20140826234520.cfg_3544.tgz
In fact, I omitted the code, the configuration file svn management, and update the source code of/home/sources/script, the script will be implemented.

"Effects"

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/47/97/wKioL1P8rtCgubB4AADunoR8oo4730.jpg "title=" Config.png "alt=" Wkiol1p8rtcgubb4aadunor8oo4730.jpg "/>
"The detailed script code is as follows":

#!/bin/sh# the parameters to make a judgment if [ $# -lt 1 ]; then    echo -e  ' \033[31m input parameter Error! \033[37m '     echo -e  \033[31m for example: $0 test-web\033[37m "     echo -e  initialization project for "\033[31m For example: $0 config\033[37m"     exit 1fi# variables =$1java_home=/usr/local/jdk/jreproject_path=/home/souces/$PROJECT   #源代码路径echo   "$PROJECT" |grep -q  service  #判断是web还是service类型SERVICEorNOT =$? Jar_path= $PROJECT _path/webcontent/web-inf/lib/  #项目jar包路径CLASS_PATH = $PROJECT _path/webcontent/web-inf/ classes/  #编译好的代码, class storage path target=/home/target  #编译好的代码, stored root path target_properties_location= "$TARGET/$ Project/web-inf/classes "  #编译好之后, where the profile should be placed src_path= $PROJECT _path/src  #源代码的src的路径TIME = ' date +%y% m%d%h%m%s ' if [  "$SERVICEorNOT"  -eq 0 ];then# if the service type          jar_path= "$PROJECT _path/lib/"          class_path= $PROJECT _path/classes         target_properties_location= "$TARGET/$PROJECT"         RM -RF   $CLASS _path/*        mkdir -p  $CLASS _path/meta-inffiif  [[  $PROJECT  !=  "config"  &&  $PROJECT  !=  "Config_2"  ]]; Then        java_source_dirs= ' cd  $SRC _path &&  ls */ -d|sed  ' s/\/*$//g '   #将src下面的com等目录取出fifunction  compile_project () {         find  $SRC _path -name *.java >  $SRC _path/ sources.list  #将java的所有文件都找出         for JAVA_SOURCE_DIR  in  $JAVA _source_dirs;do                 rm -rf&nBSP; $CLASS _path/$JAVA _source_dir                 mkdir -p  $CLASS _path/$JAVA _source_dir  #将源目录class下面的com等目录创建好, Otherwise, you can't find the target directory when you copy the class file. Pull         done         echo -e  ' \033[32m start compiling ing\033[37m '         # The key is this lira,-classpath need to explain what Lib is used when compiling,-D is, after compiling, the class should be generated to which directory          #-extdirs describes the external jar package that this project relies on, and @ explains the list of Java files that need to be compiled, which is why there was a love-oh-find java file before           #使用-xdignore.symbol.file  Reason: Because the use of sun.awt.event.*  does not add this parameter will be an error, the better solution is to let the development changes , do not use these libraries         javac -g -xdignore.symbol.file - classpath  $JAVA _home/lib/ -d  $CLASS _path -extdirs  $JAR _path @ "$SRC _path/ Sources.list "                       # Service type hit jar Pack         if [  "$SERVICEorNOT"  -eq 0  ];then                cd   $PROJECT _path &&                  #关键, here is the build MANIFEST.MF,MANIFEST.MF illustrates that the jar package depends on the Lib library already the main function, so need a second step                   #注意这里需要用sed向每行前面添加2个空格, Otherwise, when you hit the jar, you throw ioexception:java.io.ioexception: line too long      .           echo  "class-path:$ (find lib -name   ' *.jar ' |sed  ' s/lib\//  lib\//g ') " > classes/meta-inf/manifest. mf                 #main函数的路径使用svn的配置文件来管理, because the development of non-standard,                  #导致了可能java出现了多个main, so need to develop modify this configuration file to specify the main function                 svn up  /home/souces/config/project_maininfo                 mainclass= ' grep  $PROJECT  /home/souces/config_2/project_maininfo/ Maininfo.properties |awk -f ' = '   ' {print $2} '                  echo  "main-class:  $MainClass"  >>  classes/meta-inf/manifest. mf                # In fact, all the work started for this key statement, the following is a jar package                  # "$PROJECT". jar Specifies the name of the jar, what manifest is used, and-C describes the path to classes,                 jar cvfm  "$PROJECT". JAR CLASSES/META-INF/MANIFEST.MF  -C classes/ . 2>&1 >/dev/null         fi        if [ $? -eq 0 ];then                  echo -e   ' \033[32m compiled successfully \033[37m '         else                 echo -e  ' \033[31m compilation failed \033[37m '                 exit 1         fi        # Service type copy jar package and lib        if [  "$SERVICEorNOT"  -eq 0 ];then                 /bin/cp -rf  $PROJECT _ path/"$PROJECT" .jar  $TARGET/$PROJECT                  rsync -a --exclude= ". SVN"  --delete   $PROJECT _path/lib   $TARGET/$PROJECT/lib        else                 rsync -a --exclude= ". SVN"  -- delete  $PROJECT _path/webcontent/*  $TARGET/$PROJECT         fi          #需要拷贝class文件         if  [ $? -eq 0 ];then                 echo -e  "\033[32m copy class to  $TARGET/$PROJECT success \033[37m"          else                 echo -e  "\033[31m copy class to  $TARGET/$PROJECT failed \033[37m"          fi        # Copy the configuration file (Properties,xml, etc.)         cd  $PROJECT _path/src  When you have finished compiling &&        {                 for PROPERTIES in  ' find . -type  f|grep -v  '. java$ ' |grep -v  '. svn ' |grep -v  '. class ';d o                          mkdir -p  ' dirname  $TARGET _properties_location/$PROPERTIES '                          /bin/cp -f  $PROPERTIES  $ target_properties_location/$PROPERTIES                  done        }         if [ $? -eq 0 ];then                 echo -e  ' \033[32m copy properties,config success \033[37m '         else                 echo -e  ' \033[31m copy properties,config failed \033[37m '          FI} #项目打包的名字把编译时间, the version number of the project was added to later to trace the SVN version number and compile time of this package function  Project_tgz () {        cd  $TARGET  &&         project_version=$ (svn info  $PROJECT _path|grep revision|cut -d '   '  -f2)          target_tgz= "$PROJECT". " $TIME "." P_$project_version ".tgz        tar -zcf  $TARGET _tgz $ project &&        mv  $TARGET _tgz /home/tgzs/         echo -e  "\033[32m Packaging $target_tgz  Success \033[37m"}# The name of the project package adds the compile time, the version number of the configuration file, which is used to trace the SVN version number and compile time of the package later function config_tgz () {         config_2_version=$ (svn info /home/souces/config_2/|grep revision|cut -d '   '  -f2 '         rsync -a --exclude= ". SVN"  --delete  /home/souces/config_2/* /home/target/config_2        target_tgz= "Config_2". " $TIME ". Cfg_" $CONFIG _2_version ".tgz        cd  $TARGET  & &        tar -zcf  $TARGET _tgz config_2 & &        mv  $TARGET _tgz /home/tgzs/         echo -e  "\033[32m Packaging $target_tgz  Success \033[37m"} #如果只更新了配置文件if  [[   "$PROJECT"  ==  "config"  ]];then        config_tgz# Default compilation code and packaged configuration file else        compile_project         project_tgzfi


This article is from "H2O's Operation & Development Road" blog, reprint please contact the author!

Java compilation automation for operation and maintenance automation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.