Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398311
  • 博文数量: 42
  • 博客积分: 1181
  • 博客等级: 少尉
  • 技术积分: 602
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-28 22:19
文章分类

全部博文(42)

文章存档

2012年(42)

分类: Java

2012-05-17 08:43:37

说到Java你想到什么哩? 说实话, 我还是喜欢sun......:P

但是Ubuntu把sun的JDK移出repository之后再Ubuntu下要安装JDK总是步骤繁琐,现在,终于有了完美个解决方案了。


点击(此处)折叠或打开

  1. wget
  2. sudo bash oab-java6.sh
当然啦, Launchpad还是有”神谕”JDK 7的安装檔。 ~webupd8team/+archive/java
不过我们念旧的人当然还是喜欢太阳公公的JDK阿…..:P
备份下 script

点击(此处)折叠或打开

  1. #!/usr/bin/env bash
  2. # Copyright (c) Martin Wimpress
  3. #
  4. # See the file "LICENSE" for the full license governing this code.
  5. # References
  6. # -
  7. # -
  8. # - ~lockhart/gpg/gpg-cs.html
  9. # Variables
  10. VER="0.2.1"
  11. function copyright_msg() {
  12. local MODE=${1}
  13. if [ "${MODE}" == "build_docs" ]; then
  14. echo "OAB-Java6"
  15. echo "========="
  16. fi
  17. echo `basename ${0}`" v${VER} - Create a local 'apt' repository for Ubuntu Java packages."
  18. echo "Copyright (c) Martin Wimpress, MIT License"
  19. echo
  20. echo "By running this script to download Java you acknowledge that you have"
  21. echo "read and accepted the terms of the Oracle end user license agreement."
  22. echo
  23. echo "* "
  24. echo
  25. # Adjust the output if we are executing the script.
  26. # It doesn't make sense to see this message here in the documentation.
  27. if [ "${MODE}" != "build_docs" ]; then
  28. echo "If you want to see what this is script is doing while it is running then execute"
  29. echo "the following from another shell:"
  30. echo
  31. echo " tail -f `pwd`/`basename ${0}`.log"
  32. echo
  33. fi
  34. }
  35. function usage() {
  36. local MODE=${1}
  37. echo "Usage"
  38. echo "-----"
  39. echo "::"
  40. echo
  41. echo " sudo ${0}"
  42. echo
  43. echo "Optional parameters"
  44. echo
  45. echo "* ``-c`` : Remove pre-existing packages from ``/var/local/oab/deb``"
  46. echo "* ``-s`` : Skip building if the packages already exist"
  47. echo "* ``-h`` : This help"
  48. echo
  49. echo "How do I download and run this thing?"
  50. echo "-------------------------------------"
  51. echo "Like this."
  52. echo "::"
  53. echo
  54. echo " cd ~/"
  55. echo " wget {VER}/`basename ${0}` -O `basename ${0}`"
  56. echo " chmod +x `basename ${0}`"
  57. echo " sudo ./`basename ${0}`"
  58. echo
  59. echo "If you are behind a proxy you may need to run using:"
  60. echo "::"
  61. echo
  62. echo " sudo -i ./`basename ${0}`"
  63. echo
  64. # Adjust the output if we are building the docs.
  65. if [ "${MODE}" == "build_docs" ]; then
  66. echo "If you want to see what this is script is doing while it is running then execute"
  67. echo "the following from another shell:"
  68. echo "::"
  69. echo
  70. echo " tail -f ./`basename ${0}`.log"
  71. echo
  72. fi
  73. echo "How it works"
  74. echo "------------"
  75. echo "This script is merely a wrapper for the most excellent Debian packaging"
  76. echo "scripts prepared by Janusz Dziemidowicz."
  77. echo
  78. echo "* "
  79. echo
  80. echo "The basic execution steps are:"
  81. echo
  82. echo "* Remove, my now disabled, Java PPA 'ppa:flexiondotorg/java'."
  83. echo "* Install the tools required to build the Java packages."
  84. echo "* Create download cache in ``/var/local/oab/pkg``."
  85. echo "* Download the i586 and x64 Java install binaries from Oracle. Yes, both are required."
  86. echo "* Clone the build scripts from "
  87. echo "* Build the Java packages applicable to your system."
  88. echo "* Create local ``apt`` repository in ``/var/local/oab/deb`` for the newly built Java Packages."
  89. echo "* Create a GnuPG signing key in ``/var/local/oab/gpg`` if none exists."
  90. echo "* Sign the local ``apt`` repository using the local GnuPG signing key."
  91. echo
  92. echo "What gets installed?"
  93. echo "--------------------"
  94. echo "Nothing!"
  95. echo
  96. echo "This script will no longer try and directly install or upgrade any Java"
  97. echo "packages, instead a local ``apt`` repository is created that hosts locally"
  98. echo "built Java packages applicable to your system. It is up to you to install"
  99. echo "or upgrade the Java packages you require using ``apt-get``, ``aptitude`` or"
  100. echo "``synaptic``, etc. For example, once this script has been run you can simply"
  101. echo "install the JRE by executing the following from a shell."
  102. echo "::"
  103. echo
  104. echo " sudo apt-get install sun-java6-jre"
  105. echo
  106. echo "Or if you already have the *\"official\"* Ubuntu packages installed then you"
  107. echo "can upgrade by executing the following from a shell."
  108. echo "::"
  109. echo
  110. echo " sudo apt-get upgrade"
  111. echo
  112. echo "The local ``apt`` repository is just that, **local**. It is not accessible"
  113. echo "remotely and `basename ${0}` will never enable that capability to ensure"
  114. echo "compliance with Oracle's asinine license requirements."
  115. echo
  116. echo "Known Issues"
  117. echo "------------"
  118. echo
  119. echo "* The Oracle download servers can be horribly slow. My script caches the downloads so you only need download each file once."
  120. echo
  121. echo "What is 'oab'?"
  122. echo "--------------"
  123. echo "Because, O.A.B! ;-)"
  124. echo
  125. # Only exit if we are not build docs.
  126. if [ "${MODE}" != "build_docs" ]; then
  127. exit 1
  128. fi
  129. }
  130. function build_docs() {
  131. copyright_msg build_docs > README.rst
  132. # Add the usage instructions
  133. usage build_docs >> README.rst
  134. # Add the CHANGES
  135. if [ -e CHANGES ]; then
  136. cat CHANGES >> README.rst
  137. fi
  138. # Add the AUTHORS
  139. if [ -e AUTHORS ]; then
  140. cat AUTHORS >> README.rst
  141. fi
  142. # Add the TODO
  143. if [ -e TODO ]; then
  144. cat TODO >> README.rst
  145. fi
  146. # Add the LICENSE
  147. if [ -e LICENSE ]; then
  148. cat LICENSE >> README.rst
  149. fi
  150. echo "Documentation built."
  151. exit 0
  152. }
  153. copyright_msg
  154. # 'source' my common functions
  155. if [ -r /tmp/common.sh ]; then
  156. source /tmp/common.sh
  157. if [ $? -ne 0 ]; then
  158. echo "ERROR! Couldn't import common functions from /tmp/common.sh"
  159. rm /tmp/common.sh 2>/dev/null
  160. exit 1
  161. else
  162. update_thyself
  163. fi
  164. else
  165. echo "Downloading common.sh"
  166. wget --no-check-certificate -q "" -O /tmp/common.sh
  167. chmod 666 /tmp/common.sh
  168. source /tmp/common.sh
  169. if [ $? -ne 0 ]; then
  170. echo "ERROR! Couldn't import common functions from /tmp/common.sh"
  171. rm /tmp/common.sh 2>/dev/null
  172. exit 1
  173. fi
  174. fi
  175. # Check we are running on a supported system in the correct way.
  176. check_root
  177. check_sudo
  178. check_ubuntu "all"
  179. BUILD_KEY=""
  180. BUILD_CLEAN=0
  181. SKIP_REBUILD=""
  182. # Parse the options
  183. OPTSTRING=bchk:s
  184. while getopts ${OPTSTRING} OPT
  185. do
  186. case ${OPT} in
  187. b) build_docs;;
  188. c) BUILD_CLEAN=1;;
  189. h) usage;;
  190. k) BUILD_KEY=${OPTARG};;
  191. s) SKIP_REBUILD=1;;
  192. *) usage;;
  193. esac
  194. done
  195. shift "$(( $OPTIND - 1 ))"
  196. # Remove my, now disabled, Java PPA.
  197. if [ -e /etc/apt/sources.list.d/flexiondotorg-java-${LSB_CODE}.list ]; then
  198. ncecho " [x] Removing ppa:flexiondotorg/java "
  199. rm -v /etc/apt/sources.list.d/flexiondotorg-java-${LSB_CODE}.list* >> "$log" 2>&1
  200. cecho success
  201. fi
  202. # Determine the build and runtime requirements.
  203. BUILD_DEPS="build-essential debhelper defoma devscripts dpkg-dev git-core \
  204. gnupg imvirt libasound2 libxi6 libxt6 libxtst6 rng-tools unixodbc unzip"
  205. if [ "${LSB_ARCH}" == "amd64" ]; then
  206. BUILD_DEPS="${BUILD_DEPS} lib32asound2 ia32-libs"
  207. fi
  208. # Install the Java build requirements
  209. ncecho " [x] Installing Java build requirements "
  210. apt-get install -y --no-install-recommends ${BUILD_DEPS} >> "$log" 2>&1 &
  211. pid=$!;progress $pid
  212. # Make sure the required dirs exist.
  213. ncecho " [x] Making build directories "
  214. mkdir -p /var/local/oab/{deb,gpg,pkg} >> "$log" 2>&1 &
  215. pid=$!;progress $pid
  216. # Set the permissions appropriately for 'gpg'
  217. chown root:root /var/local/oab/gpg 2>/dev/null
  218. chmod 0700 /var/local/oab/gpg 2>/dev/null
  219. # Remove the 'src' directory everytime.
  220. ncecho " [x] Removing clones of "
  221. rm -rfv /var/local/oab/sun-java6* 2>/dev/null >> "$log" 2>&1
  222. rm -rfv /var/local/oab/src 2>/dev/null >> "$log" 2>&1 &
  223. pid=$!;progress $pid
  224. # Clone the code
  225. ncecho " [x] Cloning "
  226. cd /var/local/oab/ >> "$log" 2>&1
  227. git clone src >> "$log" 2>&1 &
  228. pid=$!;progress $pid
  229. # Get the last commit tag.
  230. cd /var/local/oab/src >> "$log" 2>&1
  231. TAG=`git tag -l | tail -n1`
  232. # Checkout the tagged, stable, version.
  233. ncecho " [x] Checking out ${TAG} "
  234. git checkout ${TAG} >> "$log" 2>&1 &
  235. pid=$!;progress $pid
  236. # Cet the current Debian package version and package urgency
  237. DEB_VERSION=`head -n1 /var/local/oab/src/debian/changelog | cut -d'(' -f2 | cut -d')' -f1 | cut -d'~' -f1`
  238. DEB_URGENCY=`head -n1 /var/local/oab/src/debian/changelog | cut -d'=' -f2`
  239. # Determine the currently supported Java version and update
  240. JAVA_VER=`echo ${DEB_VERSION} | cut -d'.' -f1`
  241. JAVA_UPD=`echo ${DEB_VERSION} | cut -d'.' -f2 | cut -d'-' -f1`
  242. # Try and dynamic find the JDK downloads
  243. ncecho " [x] Getting Java SE download page"
  244. wget "" -O /tmp/oab-index.html >> "$log" 2>&1 &
  245. pid=$!;progress $pid
  246. # See if the Java version is on the download frontpage, otherwise look for it in
  247. # the previous releases page.
  248. DOWNLOAD_INDEX=`grep -P -o "/technetwork/java/javase/downloads/jdk-${JAVA_VER}u${JAVA_UPD}-downloads-\d+\.html" /tmp/oab-index.html | uniq`
  249. if [ -n "${DOWNLOAD_INDEX}" ]; then
  250. ncecho " [x] Getting current release download page "
  251. wget {DOWNLOAD_INDEX} -O /tmp/oab-download.html >> "$log" 2>&1 &
  252. pid=$!;progress $pid
  253. else
  254. ncecho " [x] Getting previous releases download page "
  255. wget -O /tmp/oab-download.html >> "$log" 2>&1 &
  256. pid=$!;progress $pid
  257. fi
  258. # Download the Oracle install packages.
  259. for JAVA_BIN in jdk-${JAVA_VER}u${JAVA_UPD}-linux-i586.bin jdk-${JAVA_VER}u${JAVA_UPD}-linux-x64.bin
  260. do
  261. # Get the download URL and size
  262. DOWNLOAD_URL=`grep ${JAVA_BIN} /tmp/oab-download.html | cut -d'{' -f2 | cut -d',' -f3 | cut -d'"' -f4`
  263. DOWNLOAD_SIZE=`grep ${JAVA_BIN} /tmp/oab-download.html | cut -d'{' -f2 | cut -d',' -f2 | cut -d':' -f2 | sed 's/"//g'`
  264. # Cookies required for download
  265. COOKIES="oraclelicensejdk-${JAVA_VER}u${JAVA_UPD}-oth-JPR=accept-securebackup-cookie;gpw_e24="
  266. ncecho " [x] Downloading ${JAVA_BIN} : ${DOWNLOAD_SIZE} "
  267. wget --no-check-certificate --header="Cookie: ${COOKIES}" -c "${DOWNLOAD_URL}" -O /var/local/oab/pkg/${JAVA_BIN} >> "$log" 2>&1 &
  268. pid=$!;progress_loop $pid
  269. ncecho " [x] Symlinking ${JAVA_BIN} "
  270. ln -s /var/local/oab/pkg/${JAVA_BIN} /var/local/oab/src/${JAVA_BIN} >> "$log" 2>&1 &
  271. pid=$!;progress_loop $pid
  272. done
  273. # Determine the new version
  274. NEW_VERSION="${DEB_VERSION}~${LSB_CODE}1"
  275. if [ -n "${SKIP_REBUILD}" -a -r "/var/local/oab/deb/sun-java${JAVA_VER}_${NEW_VERSION}_${LSB_ARCH}.changes" ]; then
  276. echo " [!] Package exists, skipping build "
  277. echo "All done!"
  278. exit
  279. fi
  280. # Genereate a build message
  281. BUILD_MESSAGE="Automated build for ${LSB_REL} using "
  282. # Change directory to the build directory
  283. cd /var/local/oab/src
  284. # Update the changelog
  285. ncecho " [x] Updating the changelog "
  286. dch --distribution ${LSB_CODE} --force-distribution --newversion ${NEW_VERSION} --force-bad-version --urgency=${DEB_URGENCY} "${BUILD_MESSAGE}" >> "$log" 2>&1 &
  287. pid=$!;progress $pid
  288. # Build the binary packages
  289. ncecho " [x] Building the packages "
  290. dpkg-buildpackage -b >> "$log" 2>&1 &
  291. pid=$!;progress_can_fail $pid
  292. if [ -e /var/local/oab/sun-java${JAVA_VER}_${NEW_VERSION}_${LSB_ARCH}.changes ]; then
  293. # Remove any existing .deb files if the 'clean' option was selected.
  294. if [ ${BUILD_CLEAN} -eq 1 ]; then
  295. ncecho " [x] Removing existing .deb packages "
  296. rm -fv /var/local/oab/deb/* >> "$log" 2>&1 &
  297. pid=$!;progress $pid
  298. fi
  299. # Populate the 'apt' repository with .debs
  300. ncecho " [x] Moving the packages "
  301. mv -v /var/local/oab/sun-java${JAVA_VER}_${NEW_VERSION}_${LSB_ARCH}.changes /var/local/oab/deb/ >> "$log" 2>&1
  302. mv -v /var/local/oab/*sun-java${JAVA_VER}-*_${NEW_VERSION}_*.deb /var/local/oab/deb/ >> "$log" 2>&1 &
  303. pid=$!;progress $pid
  304. else
  305. error_msg "ERROR! Packages failed to build."
  306. fi
  307. # Create a temporary 'override' file, which may contain duplicates
  308. echo "#Override" > /tmp/override
  309. echo "#Package priority section" >> /tmp/override
  310. for FILE in /var/local/oab/deb/*.deb
  311. do
  312. DEB_PACKAGE=`dpkg --info ${FILE} | grep Package | cut -d':' -f2`
  313. DEB_SECTION=`dpkg --info ${FILE} | grep Section | cut -d'/' -f2`
  314. echo "${DEB_PACKAGE} high ${DEB_SECTION}" >> /tmp/override
  315. done
  316. # Remove the duplicates from the overide file
  317. uniq /tmp/override > /var/local/oab/deb/override
  318. # Create the 'apt' Packages.gz
  319. ncecho " [x] Creating Packages.gz file "
  320. cd /var/local/oab/deb
  321. dpkg-scanpackages . override 2>/dev/null > Packages
  322. cat Packages | gzip -c9 > Packages.gz
  323. rm /var/local/oab/deb/override 2>/dev/null
  324. cecho success
  325. # Create a 'Release' file
  326. ncecho " [x] Creating Release file "
  327. cd /var/local/oab/deb
  328. echo "Origin: `hostname --fqdn`" > Release
  329. echo "Label: Java" >> Release
  330. echo "Suite: ${LSB_CODE}" >> Release
  331. echo "Version: ${LSB_REL}" >> Release
  332. echo "Codename: ${LSB_CODE}" >> Release
  333. echo "Date: `date -R`" >> Release
  334. echo "Architectures: ${LSB_ARCH}" >> Release
  335. echo "Components: restricted" >> Release
  336. echo "Description: Local Java Repository" >> Release
  337. echo "MD5Sum:" >> Release
  338. for PACKAGE in Packages*
  339. do
  340. printf ' '`md5sum ${PACKAGE} | cut -d' ' -f1`" %16d ${PACKAGE}\n" `wc --bytes ${PACKAGE} | cut -d' ' -f1` >> Release
  341. done
  342. cecho success
  343. # Skip anything todo with automated key creation if this script is running in
  344. # an OpenVZ container.
  345. if [[ `imvirt` != "OpenVZ" ]]; then
  346. # Do we need to create signing keys
  347. if [ ! -e /var/local/oab/gpg/pubring.gpg ] && [ ! -e /var/local/oab/gpg/secring.gpg ] && [ ! -e /var/local/oab/gpg/trustdb.gpg ]; then
  348. ncecho " [x] Create GnuPG configuration "
  349. echo "Key-Type: DSA" > /var/local/oab/gpg-key.conf
  350. echo "Key-Length: 1024" >> /var/local/oab/gpg-key.conf
  351. echo "Subkey-Type: ELG-E" >> /var/local/oab/gpg-key.conf
  352. echo "Subkey-Length: 2048" >> /var/local/oab/gpg-key.conf
  353. echo "Name-Real: `hostname --fqdn`" >> /var/local/oab/gpg-key.conf
  354. echo "Name-Email: root@`hostname --fqdn`" >> /var/local/oab/gpg-key.conf
  355. echo "Expire-Date: 0" >> /var/local/oab/gpg-key.conf
  356. cecho success
  357. # Stop the system 'rngd'.
  358. /etc/init.d/rng-tools stop >> "$log" 2>&1
  359. ncecho " [x] Start generating entropy "
  360. rngd -r /dev/urandom -p /tmp/rngd.pid >> "$log" 2>&1 &
  361. pid=$!;progress $pid
  362. ncecho " [x] Creating signing key "
  363. gpg --homedir /var/local/oab/gpg --batch --gen-key /var/local/oab/gpg-key.conf >> "$log" 2>&1 &
  364. pid=$!;progress $pid
  365. ncecho " [x] Stop generating entropy "
  366. kill -9 `cat /tmp/rngd.pid` >> "$log" 2>&1 &
  367. pid=$!;progress $pid
  368. rm /tmp/rngd.pid 2>/dev/null
  369. # Start the system 'rngd'.
  370. /etc/init.d/rng-tools start >> "$log" 2>&1
  371. fi
  372. fi
  373. # Do we have signing keys, if so use them.
  374. if [ -e /var/local/oab/gpg/pubring.gpg ] && [ -e /var/local/oab/gpg/secring.gpg ] && [ -e /var/local/oab/gpg/trustdb.gpg ]; then
  375. # Sign the Release
  376. ncecho " [x] Signing the 'Release' file "
  377. rm /var/local/oab/deb/Release.gpg 2>/dev/null
  378. gpg --homedir /var/local/oab/gpg --armor --detach-sign --output /var/local/oab/deb/Release.gpg /var/local/oab/deb/Release >> "$log" 2>&1 &
  379. pid=$!;progress $pid
  380. # Export public signing key
  381. ncecho " [x] Exporting public key "
  382. gpg --homedir /var/local/oab/gpg --export -a "`hostname --fqdn`" > /var/local/oab/deb/pubkey.asc
  383. cecho success
  384. # Add the public signing key
  385. ncecho " [x] Adding public key "
  386. apt-key add /var/local/oab/deb/pubkey.asc >> "$log" 2>&1 &
  387. pid=$!;progress $pid
  388. fi
  389. # Update apt cache
  390. echo "deb file:///var/local/oab/deb / #Sun Java6 - " > /etc/apt/sources.list.d/oab.list
  391. apt_update
  392. echo "All done!"

阅读(2363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~