java-pkg-simple.eclass 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Copyright 2004-2015 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: java-pkg-simple.eclass
  4. # @MAINTAINER:
  5. # java@gentoo.org
  6. # @AUTHOR:
  7. # Java maintainers (java@gentoo.org)
  8. # @BLURB: Eclass for packaging Java software with ease.
  9. # @DESCRIPTION:
  10. # This class is intended to build pure Java packages from Java sources
  11. # without the use of any build instructions shipped with the sources.
  12. # There is no support for resources besides the generated class files,
  13. # or for generating source files, or for controlling the META-INF of
  14. # the resulting jar, although these issues may be addressed by an
  15. # ebuild by putting corresponding files into the target directory
  16. # before calling the src_compile function of this eclass.
  17. inherit java-utils-2
  18. if ! has java-pkg-2 ${INHERITED}; then
  19. eerror "java-pkg-simple eclass can only be inherited AFTER java-pkg-2"
  20. fi
  21. EXPORT_FUNCTIONS src_compile src_install
  22. # We are only interested in finding all java source files, wherever they may be.
  23. S="${WORKDIR}"
  24. # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH
  25. # @DEFAULT_UNSET
  26. # @DESCRIPTION:
  27. # Comma or space separated list of java packages to include in the
  28. # class path. The packages will also be registered as runtime
  29. # dependencies of this new package. Dependencies will be calculated
  30. # transitively. See "java-config -l" for appropriate package names.
  31. #
  32. # @CODE
  33. # JAVA_GENTOO_CLASSPATH="foo,bar-2"
  34. # @CODE
  35. # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH_EXTRA
  36. # @DEFAULT_UNSET
  37. # @DESCRIPTION:
  38. # Extra list of colon separated path elements to be put on the
  39. # classpath when compiling sources.
  40. # @ECLASS-VARIABLE: JAVA_SRC_DIR
  41. # @DEFAULT_UNSET
  42. # @DESCRIPTION:
  43. # Directories relative to ${S} which contain the sources of the
  44. # application. The default of "" will be treated mostly as ${S}
  45. # itself. For the generated source package (if source is listed in
  46. # ${JAVA_PKG_IUSE}), it is important that these directories are
  47. # actually the roots of the corresponding source trees.
  48. #
  49. # @CODE
  50. # JAVA_SRC_DIR="src/java/org/gentoo"
  51. # @CODE
  52. # @ECLASS-VARIABLE: JAVA_ENCODING
  53. # @DESCRIPTION:
  54. # The character encoding used in the source files.
  55. : ${JAVA_ENCODING:=UTF-8}
  56. # @ECLASS-VARIABLE: JAVAC_ARGS
  57. # @DEFAULT_UNSET
  58. # @DESCRIPTION:
  59. # Additional arguments to be passed to javac.
  60. # @ECLASS-VARIABLE: JAVADOC_ARGS
  61. # @DEFAULT_UNSET
  62. # @DESCRIPTION:
  63. # Additional arguments to be passed to javadoc.
  64. # @ECLASS-VARIABLE: JAVA_JAR_FILENAME
  65. # @DESCRIPTION:
  66. # The name of the jar file to create and install.
  67. : ${JAVA_JAR_FILENAME:=${PN}.jar}
  68. # @FUNCTION: java-pkg-simple_src_compile
  69. # @DESCRIPTION:
  70. # src_compile for simple bare source java packages. Finds all *.java
  71. # sources in ${JAVA_SRC_DIR}, compiles them with the classpath
  72. # calculated from ${JAVA_GENTOO_CLASSPATH}, and packages the resulting
  73. # classes to ${JAVA_JAR_FILENAME}.
  74. java-pkg-simple_src_compile() {
  75. local sources=sources.lst classes=target/classes apidoc=target/api
  76. # auto generate classpath
  77. java-pkg_gen-cp JAVA_GENTOO_CLASSPATH
  78. # gather sources
  79. find ${JAVA_SRC_DIR:-*} -name \*.java > ${sources}
  80. mkdir -p ${classes} || die "Could not create target directory"
  81. # compile
  82. local classpath="${JAVA_GENTOO_CLASSPATH_EXTRA}" dependency
  83. for dependency in ${JAVA_GENTOO_CLASSPATH}; do
  84. classpath="${classpath}:$(java-pkg_getjars ${dependency})" \
  85. || die "getjars failed for ${dependency}"
  86. done
  87. while [[ $classpath = *::* ]]; do classpath="${classpath//::/:}"; done
  88. classpath=${classpath%:}
  89. classpath=${classpath#:}
  90. debug-print "CLASSPATH=${classpath}"
  91. ejavac -d ${classes} -encoding ${JAVA_ENCODING} \
  92. ${classpath:+-classpath ${classpath}} ${JAVAC_ARGS} \
  93. @${sources}
  94. # javadoc
  95. if has doc ${JAVA_PKG_IUSE} && use doc; then
  96. mkdir -p ${apidoc}
  97. ejavadoc -d ${apidoc} \
  98. -encoding ${JAVA_ENCODING} -docencoding UTF-8 -charset UTF-8 \
  99. ${classpath:+-classpath ${classpath}} ${JAVADOC_ARGS:- -quiet} \
  100. @${sources} || die "javadoc failed"
  101. fi
  102. # package
  103. local jar_args="cf ${JAVA_JAR_FILENAME}"
  104. if [[ -e ${classes}/META-INF/MANIFEST.MF ]]; then
  105. jar_args="cfm ${JAVA_JAR_FILENAME} ${classes}/META-INF/MANIFEST.MF"
  106. fi
  107. jar ${jar_args} -C ${classes} . || die "jar failed"
  108. }
  109. # @FUNCTION: java-pkg-simple_src_install
  110. # @DESCRIPTION:
  111. # src_install for simple single jar java packages. Simply packages the
  112. # contents from the target directory and installs it as
  113. # ${JAVA_JAR_FILENAME}. If the file target/META-INF/MANIFEST.MF exists,
  114. # it is used as the manifest of the created jar.
  115. java-pkg-simple_src_install() {
  116. local sources=sources.lst classes=target/classes apidoc=target/api
  117. # main jar
  118. java-pkg_dojar ${JAVA_JAR_FILENAME}
  119. # javadoc
  120. if has doc ${JAVA_PKG_IUSE} && use doc; then
  121. java-pkg_dojavadoc ${apidoc}
  122. fi
  123. # dosrc
  124. if has source ${JAVA_PKG_IUSE} && use source; then
  125. local srcdirs=""
  126. if [[ ${JAVA_SRC_DIR} ]]; then
  127. local parent child
  128. for parent in ${JAVA_SRC_DIR}; do
  129. for child in ${parent}/*; do
  130. srcdirs="${srcdirs} ${child}"
  131. done
  132. done
  133. else
  134. # take all directories actually containing any sources
  135. srcdirs="$(cut -d/ -f1 ${sources} | sort -u)"
  136. fi
  137. java-pkg_dosrc ${srcdirs}
  138. fi
  139. }