1 # Copyright 2004-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: java-ant-2.eclass
9 # kiorky (kiorky@cryptelium.net), Petteri Räty (betelgeuse@gentoo.org)
10 # @BLURB: eclass for ant based Java packages
12 # Eclass for Ant-based Java packages. Provides support for both automatic and
13 # manual manipulation of build.xml files. Should be inherited after java-pkg-2
14 # or java-pkg-opt-2 eclass.
16 inherit java-utils-2 multilib
18 # This eclass provides functionality for Java packages which use
19 # ant to build. In particular, it will attempt to fix build.xml files, so that
20 # they use the appropriate 'target' and 'source' attributes.
22 # @ECLASS-VARIABLE: WANT_ANT_TASKS
25 # An $IFS separated list of ant tasks.
26 # Ebuild can specify this variable before inheriting java-ant-2 eclass to
27 # determine ANT_TASKS it needs. They will be automatically translated to
28 # DEPEND variable and ANT_TASKS variable. JAVA_PKG_FORCE_ANT_TASKS can override
29 # ANT_TASKS set by WANT_ANT_TASKS, but not the DEPEND due to caching.
30 # Ebuilds that need to depend conditionally on certain tasks and specify them
31 # differently for different eant calls can't use this simplified approach.
32 # You also cannot specify version or anything else than ant-*.
35 # WANT_ANT_TASKS="ant-junit ant-trax"
38 #The implementation of dependencies is handled by java-utils-2.eclass
41 # @ECLASS-VARIABLE: JAVA_ANT_DISABLE_ANT_CORE_DEP
44 # Setting this variable non-empty before inheriting java-ant-2 disables adding
45 # dev-java/ant-core into DEPEND.
46 if [[ -z "${JAVA_ANT_DISABLE_ANT_CORE_DEP}" ]]; then
47 JAVA_ANT_E_DEPEND+=" >=dev-java/ant-core-1.8.2"
48 [[ "${EAPI:-0}" != 0 ]] && JAVA_ANT_E_DEPEND+=":0"
51 # add ant tasks specified in WANT_ANT_TASKS to DEPEND
52 local ANT_TASKS_DEPEND;
53 ANT_TASKS_DEPEND="$(java-pkg_ant-tasks-depend)"
54 # check that java-pkg_ant-tasks-depend didn't fail
55 if [[ $? != 0 ]]; then
56 eerror "${ANT_TASKS_DEPEND}"
57 die "java-pkg_ant-tasks-depend() failed"
60 # We need some tools from javatoolkit. We also need portage 2.1 for phase hooks
61 # and ant dependencies constructed above. Python is there for
62 # java-ant_remove-taskdefs
63 JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND}
65 ${JAVA_PKG_PORTAGE_DEP}
66 >=dev-java/javatoolkit-0.3.0-r2"
68 # this eclass must be inherited after java-pkg-2 or java-pkg-opt-2
69 # if it's java-pkg-opt-2, ant dependencies are pulled based on USE flag
70 if has java-pkg-opt-2 ${INHERITED}; then
71 JAVA_ANT_E_DEPEND="${JAVA_PKG_OPT_USE}? ( ${JAVA_ANT_E_DEPEND} )"
72 elif ! has java-pkg-2 ${INHERITED}; then
73 eerror "java-ant-2 eclass can only be inherited AFTER java-pkg-2 or java-pkg-opt-2"
76 DEPEND="${JAVA_ANT_E_DEPEND}"
78 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX
80 # Should we attempt to 'fix' ant build files to include the source/target
81 # attributes when calling javac?
82 JAVA_PKG_BSFIX=${JAVA_PKG_BSFIX:-"on"}
84 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_ALL
86 # If we're fixing build files, should we try to fix all the ones we can find?
87 JAVA_PKG_BSFIX_ALL=${JAVA_PKG_BSFIX_ALL:-"yes"}
89 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_NAME
91 # Filename of build files to fix/search for
92 JAVA_PKG_BSFIX_NAME=${JAVA_PKG_BSFIX_NAME:-"build.xml"}
94 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_TARGET_TAGS
96 # Targets to fix the 'source' attribute in
97 JAVA_PKG_BSFIX_TARGET_TAGS=${JAVA_PKG_BSFIX_TARGET_TAGS:-"javac xjavac javac.preset"}
99 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_SOURCE_TAGS
101 # Targets to fix the 'target' attribute in
102 JAVA_PKG_BSFIX_SOURCE_TAGS=${JAVA_PKG_BSFIX_SOURCE_TAGS:-"javadoc javac xjavac javac.preset"}
104 # @ECLASS-VARIABLE: JAVA_ANT_CLASSPATH_TAGS
106 # Targets to add the classpath attribute to
107 JAVA_ANT_CLASSPATH_TAGS="javac xjavac"
109 # @ECLASS-VARIABLE: JAVA_ANT_IGNORE_SYSTEM_CLASSES
112 # When set, <available> Ant tasks are rewritten to ignore Ant's runtime classpath.
116 *) EXPORT_FUNCTIONS src_configure ;;
119 # @FUNCTION: java-ant-2_src_configure
121 # src_configure rewrites the build.xml files automatically, unless EAPI is undefined, 0 or 1.
122 java-ant-2_src_configure() {
123 # if java support is optional, don't perform this when the USE flag is off
124 if has java-pkg-opt-2 ${INHERITED}; then
125 use ${JAVA_PKG_OPT_USE} || return
128 # eant will call us unless called by Portage
129 [[ -e "${T}/java-ant-2_src_configure-run" ]] && return
131 [[ "${JAVA_ANT_IGNORE_SYSTEM_CLASSES}" ]] \
132 && java-ant_ignore-system-classes "${S}/build.xml"
135 touch "${T}/java-ant-2_src_configure-run"
138 # @FUNCTION: java-ant_bsfix
141 # Attempts to fix build files.
144 # Affected by variables:
147 # JAVA_PKG_BSFIX_NAME,
150 debug-print-function ${FUNCNAME} $*
152 [[ "${JAVA_PKG_BSFIX}" != "on" ]] && return
153 if ! java-pkg_needs-vm; then
154 echo "QA Notice: Package is using java-ant, but doesn't depend on a Java VM"
157 pushd "${S}" >/dev/null || die
160 [[ "${JAVA_PKG_BSFIX_ALL}" == "yes" ]] || find_args="-maxdepth 1"
162 find_args="${find_args} -type f ( -name ${JAVA_PKG_BSFIX_NAME// / -o -name } )"
164 # This voodoo is done for paths with spaces
167 [[ -z ${line} ]] && continue
168 bsfix_these="${bsfix_these} '${line}'"
170 $(find . ${find_args})
173 [[ "${bsfix_these// /}" ]] && eval java-ant_bsfix_files ${bsfix_these}
175 popd > /dev/null || die
179 if has_version dev-python/pyxml; then
180 eerror "If the output above contains:"
181 eerror "ImportError:"
182 eerror "/usr/lib/python2.4/site-packages/_xmlplus/parsers/pyexpat.so:"
183 eerror "undefined symbol: PyUnicodeUCS2_DecodeUTF8"
184 eerror "Try re-emerging dev-python/pyxml"
185 die ${1} " Look at the eerror message above"
191 # @FUNCTION: java-ant_bsfix_files
192 # @USAGE: <path/to/first/build.xml> [path/to/second.build.xml ...]
194 # Attempts to fix named build files.
197 # Affected by variables:
198 # JAVA_PKG_BSFIX_SOURCE_TAGS
199 # JAVA_PKG_BSFIX_TARGET_TAGS
200 # JAVA_ANT_REWRITE_CLASSPATH
201 # JAVA_ANT_JAVADOC_INPUT_DIRS: Where we can find java sources for javadoc
202 # input. Can be a space separated list of
204 # JAVA_ANT_BSFIX_EXTRA_ARGS: You can use this to pass extra variables to the
205 # rewriter if you know what you are doing.
208 # If JAVA_ANT_JAVADOC_INPUT_DIRS is set, we will turn on the adding of a basic
209 # javadoc target to the ant's build.xml with the javadoc xml-rewriter feature.
210 # Then we will set EANT DOC TARGET to the added javadoc target
211 # NOTE: the variable JAVA_ANT_JAVADOC_OUTPUT_DIR points where we will
212 # generate the javadocs. This is a read-only variable, dont change it.
214 # When changing this function, make sure that it works with paths with spaces in
216 java-ant_bsfix_files() {
217 debug-print-function ${FUNCNAME} $*
219 [[ ${#} = 0 ]] && die "${FUNCNAME} called without arguments"
221 local want_source="$(java-pkg_get-source)"
222 local want_target="$(java-pkg_get-target)"
224 debug-print "${FUNCNAME}: target: ${want_target} source: ${want_source}"
226 if [ -z "${want_source}" -o -z "${want_target}" ]; then
227 eerror "Could not find valid -source/-target values"
228 eerror "Please file a bug about this on bugs.gentoo.org"
229 die "Could not find valid -source/-target values"
233 for file in "${@}"; do
234 debug-print "${FUNCNAME}: ${file}"
236 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
237 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
240 if [[ ! -w "${file}" ]]; then
241 chmod u+w "${file}" || die "chmod u+w ${file} failed"
244 files="${files} -f '${file}'"
247 # for javadoc target and all in one pass, we need the new rewriter.
248 local rewriter3="/usr/share/javatoolkit/xml-rewrite-3.py"
249 if [[ ! -f ${rewriter3} ]]; then
250 rewriter3="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-3.py"
253 local rewriter4="/usr/$(get_libdir)/javatoolkit/bin/build-xml-rewrite"
255 if [[ -x ${rewriter4} && ${JAVA_ANT_ENCODING} ]]; then
256 [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]] && local gcp="-g"
257 [[ ${JAVA_ANT_ENCODING} ]] && local enc="-e ${JAVA_ANT_ENCODING}"
258 eval echo "cElementTree rewriter"
259 debug-print "${rewriter4} extra args: ${gcp} ${enc}"
260 ${rewriter4} ${gcp} ${enc} \
261 -c "${JAVA_PKG_BSFIX_SOURCE_TAGS}" source ${want_source} \
262 -c "${JAVA_PKG_BSFIX_TARGET_TAGS}" target ${want_target} \
263 "${@}" || die "build-xml-rewrite failed"
264 elif [[ ! -f ${rewriter3} ]]; then
265 debug-print "Using second generation rewriter"
266 eval echo "Rewriting source attributes"
267 eval xml-rewrite-2.py ${files} \
268 -c -e ${JAVA_PKG_BSFIX_SOURCE_TAGS// / -e } \
269 -a source -v ${want_source} || _bsfix_die "xml-rewrite2 failed: ${file}"
271 eval echo "Rewriting target attributes"
272 eval xml-rewrite-2.py ${files} \
273 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
274 -a target -v ${want_target} || _bsfix_die "xml-rewrite2 failed: ${file}"
276 eval echo "Rewriting nowarn attributes"
277 eval xml-rewrite-2.py ${files} \
278 -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
279 -a nowarn -v yes || _bsfix_die "xml-rewrite2 failed: ${file}"
281 if [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
282 eval echo "Adding gentoo.classpath to javac tasks"
283 eval xml-rewrite-2.py ${files} \
284 -c -e javac -e xjavac -a classpath -v \
285 '\${gentoo.classpath}' \
286 || _bsfix_die "xml-rewrite2 failed"
289 debug-print "Using third generation rewriter"
290 eval echo "Rewriting attributes"
291 local bsfix_extra_args=""
292 # WARNING KEEP THE ORDER, ESPECIALLY FOR CHANGED ATTRIBUTES!
293 if [[ -n ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
294 local cp_tags="${JAVA_ANT_CLASSPATH_TAGS// / -e }"
295 bsfix_extra_args="${bsfix_extra_args} -g -e ${cp_tags}"
296 bsfix_extra_args="${bsfix_extra_args} -a classpath -v '\${gentoo.classpath}'"
298 if [[ -n ${JAVA_ANT_JAVADOC_INPUT_DIRS} ]]; then
299 if [[ -n ${JAVA_ANT_JAVADOC_OUTPUT_DIR} ]]; then
300 die "Do not define JAVA_ANT_JAVADOC_OUTPUT_DIR!"
302 # Where will our generated javadoc go.
303 readonly JAVA_ANT_JAVADOC_OUTPUT_DIR="${WORKDIR}/gentoo_javadoc"
304 mkdir -p "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" || die
306 if has doc ${IUSE}; then
308 if [[ -z ${EANT_DOC_TARGET} ]]; then
309 EANT_DOC_TARGET="gentoojavadoc"
311 die "You can't use javadoc adding and set EANT_DOC_TARGET too."
314 for dir in ${JAVA_ANT_JAVADOC_INPUT_DIRS};do
315 if [[ ! -d ${dir} ]]; then
316 eerror "This dir: ${dir} doesnt' exists"
317 die "You must specify directories for javadoc input/output dirs."
320 bsfix_extra_args="${bsfix_extra_args} --javadoc --source-directory "
321 # filter third/double spaces
322 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
323 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
324 bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_JAVADOC_INPUT_DIRS// / --source-directory }"
325 bsfix_extra_args="${bsfix_extra_args} --output-directory ${JAVA_ANT_JAVADOC_OUTPUT_DIR}"
328 die "You need to have doc in IUSE when using JAVA_ANT_JAVADOC_INPUT_DIRS"
332 [[ -n ${JAVA_ANT_BSFIX_EXTRA_ARGS} ]] \
333 && bsfix_extra_args="${bsfix_extra_args} ${JAVA_ANT_BSFIX_EXTRA_ARGS}"
335 debug-print "bsfix_extra_args: ${bsfix_extra_args}"
337 eval ${rewriter3} ${files} \
338 -c --source-element ${JAVA_PKG_BSFIX_SOURCE_TAGS// / --source-element } \
339 --source-attribute source --source-value ${want_source} \
340 --target-element ${JAVA_PKG_BSFIX_TARGET_TAGS// / --target-element } \
341 --target-attribute target --target-value ${want_target} \
342 --target-attribute nowarn --target-value yes \
343 ${bsfix_extra_args} \
344 || _bsfix_die "xml-rewrite2 failed: ${file}"
347 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
348 for file in "${@}"; do
349 diff -NurbB "${file}.orig" "${file}"
353 return 0 # so that the 1 for diff doesn't get reported
357 # @FUNCTION: java-ant_bsfix_one
358 # @USAGE: <path/to/build.xml>
360 # Attempts to fix named build file.
363 # Affected by variables:
364 # JAVA_PKG_BSFIX_SOURCE_TAGS
365 # JAVA_PKG_BSFIX_TARGET_TAGS
367 java-ant_bsfix_one() {
368 debug-print-function ${FUNCNAME} $*
370 if [ -z "${1}" ]; then
371 eerror "${FUNCNAME} needs one argument"
372 die "${FUNCNAME} needs one argument"
375 java-ant_bsfix_files "${1}"
378 # @FUNCTION: java-ant_rewrite-classpath
379 # @USAGE: [path/to/build.xml]
381 # Adds 'classpath="${gentoo.classpath}"' to specified build file.
384 # JAVA_ANT_CLASSPATH_TAGS
386 # Parameter defaults to build.xml when not specified
387 java-ant_rewrite-classpath() {
388 debug-print-function ${FUNCNAME} $*
391 [[ -z "${1}" ]] && file=build.xml
392 [[ ${#} -gt 1 ]] && die "${FUNCNAME} currently can only rewrite one file."
394 echo "Adding gentoo.classpath to ${file}"
395 debug-print "java-ant_rewrite-classpath: ${file}"
397 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
401 java-ant_xml-rewrite -f "${file}" --change \
402 -e ${JAVA_ANT_CLASSPATH_TAGS// / -e } -a classpath -v '${gentoo.classpath}'
404 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
405 diff -NurbB "${file}.orig" "${file}"
409 # @FUNCTION: java-ant_remove-taskdefs
410 # @USAGE: [--name NAME] [path/to/build.xml]
412 # Removes (named) taskdef elements from the build.xml file.
413 # When --name NAME is specified, only remove taskdef with name NAME. Otherwise,
414 # all taskdefs are removed.
415 # The file to rewrite defaults to build.xml when not specified.
416 java-ant_remove-taskdefs() {
417 debug-print-function ${FUNCNAME} $*
419 die "${FUNCNAME} has been banned, see bug #479838."
422 if [[ "${1}" == --name ]]; then
426 local file="${1:-build.xml}"
427 echo "Removing taskdefs from ${file}"
430 from xml.dom.minidom import parse
431 dom = parse("${file}")
432 for elem in dom.getElementsByTagName('taskdef'):
433 if (len("${task_name}") == 0 or elem.getAttribute("name") == "${task_name}"):
434 elem.parentNode.removeChild(elem)
436 f = open("${file}", "w")
440 [[ $? != 0 ]] && die "Removing taskdefs failed"
443 # @FUNCTION: java-ant_ignore-system-classes
444 # @USAGE: [path/to/build.xml]
446 # Makes the available task ignore classes in the system classpath
447 # Parameter defaults to build.xml when not specified
448 java-ant_ignore-system-classes() {
449 debug-print-function ${FUNCNAME} $*
450 local file=${1:-build.xml}
451 echo "Changing ignoresystemclasses to true for available tasks in ${file}"
452 java-ant_xml-rewrite -f "${file}" --change \
453 -e available -a ignoresystemclasses -v "true"
456 # @FUNCTION: java-ant_xml-rewrite
457 # @USAGE: <xml rewriter arguments>
459 # Run the right xml-rewrite binary with the given arguments
460 java-ant_xml-rewrite() {
461 local gen2="/usr/bin/xml-rewrite-2.py"
462 local gen2_1="/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-2.py"
464 if [[ -x "${gen2}" ]]; then
465 ${gen2} "${@}" || die "${gen2} failed"
466 elif [[ -x "${gen2_1}" ]]; then
467 ${gen2_1} "${@}" || die "${gen2_1} failed"
469 eerror "No binary for rewriting found."
470 eerror "Do you have dev-java/javatoolkit installed?"
471 die "xml-rewrite not found"
475 # @FUNCTION: java-ant_rewrite-bootclasspath
476 # @USAGE: <version> [path/to/build.xml] [prepend] [append]
478 # Adds bootclasspath to javac-like tasks in build.xml filled with jars of a
479 # bootclasspath package of given version.
483 # JAVA_PKG_BSFIX_TARGET_TAGS - the tags of javac tasks
486 # $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
488 # $2 - path to desired build.xml file, defaults to 'build.xml'
489 # $3 - (optional) what to prepend the bootclasspath with (to override)
490 # $4 - (optional) what to append to the bootclasspath
492 java-ant_rewrite-bootclasspath() {
494 local file="${2-build.xml}"
495 local extra_before="${3}"
496 local extra_after="${4}"
498 local bcp="$(java-pkg_get-bootclasspath "${version}")"
500 if [[ -n "${extra_before}" ]]; then
501 bcp="${extra_before}:${bcp}"
503 if [[ -n "${extra_after}" ]]; then
504 bcp="${bcp}:${extra_after}"
507 java-ant_xml-rewrite -f "${file}" -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
508 -a bootclasspath -v "${bcp}"