程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 簡介Java法式的Shell劇本包裝

簡介Java法式的Shell劇本包裝

編輯:關於JAVA

簡介Java法式的Shell劇本包裝。本站提示廣大學習愛好者:(簡介Java法式的Shell劇本包裝)文章只能為提供參考,不一定能成為您想要的結果。以下是簡介Java法式的Shell劇本包裝正文


在很多Java工程中,常常會看到帶有法式自界說參數挪用Java敕令的包裝shell劇本。例如,
 

$ANT_HOME/bin/ant, $GROOVY_HOME/bin/groovy

,乃至在我們的TimeMachine Scheduler法式中也能見到
 

$TIMEMACHINE_HOME/bin/scheduler.sh

編寫這些包裝劇本很無聊並且輕易失足。年夜多半的成績來自為法式設置准確的classpath。假如你正在為一個公司開辟外部項目標話,那末你有能夠闊別糾結的途徑和情況變量成績。然則關於開源項目,人們須要使包裝加倍靈巧和通用。年夜多半乃至供給了.bat版本。Windows DOS確切是個蠻橫且被限制的終端而不克不及很好的知足你項目劇本需求。是以,我常勉勵他人盡可能照樣多應用Cygwi。至多它具有一個真實的bash shell。其他罕見的成績就是這些包裝很快就會掉去掌握並且在你的項目遍地都邑湧現許多冗余劇本。

run-java包裝劇本引見

假如你看到 $TIMEMACHINE_HOME/bin/scheduler.sh 的代碼,你會看到它實際上是在同目次下輪回挪用run-java劇本。
 

DIR=$(dirname $0)
SCHEDULER_HOME=$DIR/..
$DIR/run-java -Dscheduler.home="$SCHEDULER_HOME" timemachine.scheduler.tool.SchedulerServer "$@"

正如你看到的,我們的 run-java 可使用 -D 選項,不只如許,它異樣也能應用 -cp 選項!並且,你還能在main class前面指定這些選項!如許可以或許使得run-java被其他的劇本包裝,而且仍然可以或許添加額定的體系屬性和classpath。

例如,TimeMachine 附帶了 Groovy 庫,所以你可以簡略的像如許挪用
 

groovy:$TIMEMACHINE_HOME/bin/run-java groovy.ui.GroovyMain test.groovy

,而不消再次下載全部分支。

你可以很便利地在任何目次下應用,它確認本身的目次然後可以主動加載lib目次下的任何jar包。如今假如你想要附加更多的jar包來運轉Groovy的話,可以以下應用 -cp 選項:
 

$TIMEMACHINE_HOME/bin/run-java -cp "$HOME/apps/my-app/lib/*" groovy.ui.GroovyMain test.groovy
平日假如你設置java classpath不敷當心時會常常招致毛病,然則應用 run-java 可以事後運轉一次:
 
RUN_JAVA_DRY=1 $TIMEMACHINE_HOME/bin/run-java -cp "$HOME/apps/my-app/lib/*" groovy.ui.GroovyMain test.groovy

你只需在敕令提醒行中運轉下面一整行代碼便可。它將輸入完全的附帶一切選項和參數的java敕令。

run-script還包括許多其它的選項,你可以經由過程檢查其正文懂得。以後的劇本可以或許在任何的Linux bash和Windows Cygwin中運轉。


在開辟中經由過程Maven應用 run-java

依據下面提到的示例,假定項目宣布構造以下:

$TIMEMACHINE_HOME
 +- bin/run-java
 +- lib/*.jar

然則在開辟進程中目次會是如何呢?一個罕見的用例就是:你願望可以或許運轉target/classes下最新編譯的代碼而不是將全部項目打包或許宣布。你異樣可以在此種情形下應用 run-java 。起首,簡略的將 bin/run-java 添加進你的項目,然後運轉
 

mvn compile dependency:copy-dependencies

將會在target/dependency下生成一切的jar文件。就只須要做這些。run-java將主動的檢測這些目次,並為你的main class創立准確的classpath。

假如你應用Eclipse來開辟,那末你的target/classes目次將老是在更新的,run-java便能成為你項目開辟中的珍寶。

獲得 run-java 包裝劇本
 

#!/usr/bin/env bash
#
# Copyright 2012 Zemian Deng
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
# A wrapper script that run any Java6 application in unix/cygwin env.
#
# This script is assumed to be located in an application's "bin" directory. It will
# auto resolve any symbolic link and always run in relative to this application
# directory (which is one parent up from the script.) Therefore, this script can be
# run any where in the file system and it will still reference this application
# directory.
#
# This script will by default auto setup a Java classpath that picks up any "config"
# and "lib" directories under the application directory. It also will also add a
# any typical Maven project output directories such as "target/test-classes",
# "target/classes", and "target/dependency" into classpath. This can be disable by
# setting RUN_JAVA_NO_PARSE=1.
#
# If the "Default parameters" section bellow doesn't match to user's env, then user
# may override these variables in their terminal session or preset them in shell's
# profile startup script. The values of all path should be in cygwin/unix path,
# and this script will auto convert them into Windows path where is needed.
#
# User may customize the Java classpath by setting RUN_JAVA_CP, which will prefix to existing
# classpath, or use the "-cp" option, which will postfix to existing classpath.
#
# Usage:
# run-java [java_opts] <java_main_class> [-cp /more/classpath] [-Dsysprop=value]
#
# Example:
# run-java example.Hello
# run-java example.Hello -Dname=World
# run-java org.junit.runner.JUnitCore example.HelloTest -cp "C:\apps\lib\junit4.8.2\*"
#
# Created by: Zemian Deng 03/09/2012
 
# This run script dir (resolve to absolute path)
SCRIPT_DIR=$(cd $(dirname $0) && pwd) # This dir is where this script live.
APP_DIR=$(cd $SCRIPT_DIR/.. && pwd)  # Assume the application dir is one level up from script dir.
 
# Default parameters
JAVA_HOME=${JAVA_HOME:=/apps/jdk}  # This is the home directory of Java development kit.
RUN_JAVA_CP=${RUN_JAVA_CP:=$CLASSPATH}  # A classpath prefix before -classpath option, default to $CLASSPATH
RUN_JAVA_OPTS=${RUN_JAVA_OPTS:=}   # Java options (-Xmx512m -XX:MaxPermSize=128m etc)
RUN_JAVA_DEBUG=${RUN_JAVA_DEBUG:=}   # If not empty, print the full java command line before executing it.
RUN_JAVA_NO_PARSE=${RUN_JAVA_NO_PARSE:=} # If not empty, skip the auto parsing of -D and -cp options from script arguments.
RUN_JAVA_NO_AUTOCP=${RUN_JAVA_NO_AUTOCP:=} # If not empty, do not auto setup Java classpath
RUN_JAVA_DRY=${RUN_JAVA_DRY:=}    # If not empty, do not exec Java command, but just print
 
# OS specific support. $var _must_ be set to either true or false.
CYGWIN=false;
case "`uname`" in
 CYGWIN*) CYGWIN=true ;;
esac
 
# Define where is the java executable is
JAVA_CMD=java
if [ -d "$JAVA_HOME" ]; then
  JAVA_CMD="$JAVA_HOME/bin/java"
fi
 
# Auto setup applciation's Java Classpath (only if they exists)
if [ -z "$RUN_JAVA_NO_AUTOCP" ]; then
  if $CYGWIN; then
    # Provide Windows directory conversion
    JAVA_HOME_WIN=$(cygpath -aw "$JAVA_HOME")
    APP_DIR_WIN=$(cygpath -aw "$APP_DIR")
 
    if [ -d "$APP_DIR_WIN\config" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\config" ; fi
    if [ -d "$APP_DIR_WIN\target\test-classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\test-classes" ; fi
    if [ -d "$APP_DIR_WIN\target\classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\classes" ; fi
    if [ -d "$APP_DIR_WIN\target\dependency" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\target\dependency\*" ; fi
    if [ -d "$APP_DIR_WIN\lib" ]; then RUN_JAVA_CP="$RUN_JAVA_CP;$APP_DIR_WIN\lib\*" ; fi
  else
    if [ -d "$APP_DIR/config" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/config" ; fi
    if [ -d "$APP_DIR/target/test-classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/test-classes" ; fi
    if [ -d "$APP_DIR/target/classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/classes" ; fi
    if [ -d "$APP_DIR/target/dependency" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/dependency/*" ; fi
    if [ -d "$APP_DIR/lib" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/lib/*" ; fi
  fi
fi
 
# Parse addition "-cp" and "-D" after the Java main class from script arguments
# This is done for convenient sake so users do not have to export RUN_JAVA_CP and RUN_JAVA_OPTS
# saparately, but now they can pass into end of this run-java script instead.
# This can be disable by setting RUN_JAVA_NO_PARSE=1.
if [ -z "$RUN_JAVA_NO_PARSE" ]; then 
  # Prepare variables for parsing
  FOUND_CP=
  declare -a NEW_ARGS
  IDX=0
   
  # Parse all arguments and look for "-cp" and "-D"
  for ARG in "$@"; do
    if [[ -n $FOUND_CP ]]; then 
      if [ "$OS" = "Windows_NT" ]; then
        # Can't use cygpath here, because cygpath will auto expand "*", which we do not
        # want. User will just have to use OS path when specifying "-cp" option.  
        #ARG=$(cygpath -w -a $ARG)
        RUN_JAVA_CP="$RUN_JAVA_CP;$ARG"
      else
        RUN_JAVA_CP="$RUN_JAVA_CP:$ARG"
      fi
      FOUND_CP=
    else
      case $ARG in
      '-cp')
        FOUND_CP=1
        ;;
      '-D'*)
        RUN_JAVA_OPTS="$RUN_JAVA_OPTS $ARG"
        ;;
      *)
        NEW_ARGS[$IDX]="$ARG"
        let IDX=$IDX+1
        ;;
      esac
    fi
  done
     
  # Display full Java command.
  if [ -n "$RUN_JAVA_DEBUG" ] || [ -n "$RUN_JAVA_DRY" ]; then
    echo "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "${NEW_ARGS[@]}"
  fi
   
  # Run Java Main class using parsed variables
  if [ -z "$RUN_JAVA_DRY" ]; then
    "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "${NEW_ARGS[@]}"
  fi
else
  # Display full Java command.
  if [ -n "$RUN_JAVA_DEBUG" ] || [ -n "$RUN_JAVA_DRY" ]; then
    echo "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "$@"
  fi
   
  # Run Java Main class
  if [ -z "$RUN_JAVA_DRY" ]; then
    "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" "$@"
  fi
fi

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved