1 # Copyright 2004-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: java-ant-2.eclass
8 # kiorky (kiorky@cryptelium.net), Petteri Räty (betelgeuse@gentoo.org)
9 # @BLURB: eclass for ant based Java packages
11 # Eclass for Ant-based Java packages. Provides support for both automatic and
12 # manual manipulation of build.xml files. Should be inherited after java-pkg-2
13 # or java-pkg-opt-2 eclass.
15 inherit java-utils-2 multilib
17 # This eclass provides functionality for Java packages which use
18 # ant to build. In particular, it will attempt to fix build.xml files, so that
19 # they use the appropriate 'target' and 'source' attributes.
21 # @ECLASS-VARIABLE: WANT_ANT_TASKS
24 # An $IFS separated list of ant tasks.
25 # Ebuild can specify this variable before inheriting java-ant-2 eclass to
26 # determine ANT_TASKS it needs. They will be automatically translated to
27 # DEPEND variable and ANT_TASKS variable. JAVA_PKG_FORCE_ANT_TASKS can override
28 # ANT_TASKS set by WANT_ANT_TASKS, but not the DEPEND due to caching.
29 # Ebuilds that need to depend conditionally on certain tasks and specify them
30 # differently for different eant calls can't use this simplified approach.
31 # You also cannot specify version or anything else than ant-*.
34 # WANT_ANT_TASKS="ant-junit ant-trax"
37 #The implementation of dependencies is handled by java-utils-2.eclass
40 # @ECLASS-VARIABLE: JAVA_ANT_DISABLE_ANT_CORE_DEP
43 # Setting this variable non-empty before inheriting java-ant-2 disables adding
44 # dev-java/ant-core into DEPEND.
45 if [[ -z "${JAVA_ANT_DISABLE_ANT_CORE_DEP}" ]]; then
46 JAVA_ANT_E_DEPEND+=" >=dev-java/ant-core-1.8.2"
47 [[ "${EAPI:-0}" != 0 ]] && JAVA_ANT_E_DEPEND+=":0"
50 # add ant tasks specified in WANT_ANT_TASKS to DEPEND
51 local ANT_TASKS_DEPEND;
52 ANT_TASKS_DEPEND="$(java-pkg_ant-tasks-depend)"
53 # check that java-pkg_ant-tasks-depend didn't fail
54 if [[ $? != 0 ]]; then
55 eerror "${ANT_TASKS_DEPEND}"
56 die "java-pkg_ant-tasks-depend() failed"
59 # We need some tools from javatoolkit. We also need ant dependencies
61 JAVA_ANT_E_DEPEND="${JAVA_ANT_E_DEPEND}
63 >=dev-java/javatoolkit-0.3.0-r2"
65 # this eclass must be inherited after java-pkg-2 or java-pkg-opt-2
66 # if it's java-pkg-opt-2, ant dependencies are pulled based on USE flag
67 if has java-pkg-opt-2 ${INHERITED}; then
68 JAVA_ANT_E_DEPEND="${JAVA_PKG_OPT_USE}? ( ${JAVA_ANT_E_DEPEND} )"
69 elif ! has java-pkg-2 ${INHERITED}; then
70 eerror "java-ant-2 eclass can only be inherited AFTER java-pkg-2 or java-pkg-opt-2"
73 DEPEND="${JAVA_ANT_E_DEPEND}"
75 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX
77 # Should we attempt to 'fix' ant build files to include the source/target
78 # attributes when calling javac?
79 JAVA_PKG_BSFIX=${JAVA_PKG_BSFIX:-"on"}
81 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_ALL
83 # If we're fixing build files, should we try to fix all the ones we can find?
84 JAVA_PKG_BSFIX_ALL=${JAVA_PKG_BSFIX_ALL:-"yes"}
86 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_NAME
88 # Filename of build files to fix/search for
89 JAVA_PKG_BSFIX_NAME=${JAVA_PKG_BSFIX_NAME:-"build.xml"}
91 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_TARGET_TAGS
93 # Targets to fix the 'source' attribute in
94 JAVA_PKG_BSFIX_TARGET_TAGS=${JAVA_PKG_BSFIX_TARGET_TAGS:-"javac xjavac javac.preset"}
96 # @ECLASS-VARIABLE: JAVA_PKG_BSFIX_SOURCE_TAGS
98 # Targets to fix the 'target' attribute in
99 JAVA_PKG_BSFIX_SOURCE_TAGS=${JAVA_PKG_BSFIX_SOURCE_TAGS:-"javadoc javac xjavac javac.preset"}
101 # @ECLASS-VARIABLE: JAVA_ANT_CLASSPATH_TAGS
103 # Targets to add the classpath attribute to
104 JAVA_ANT_CLASSPATH_TAGS="javac xjavac"
106 # @ECLASS-VARIABLE: JAVA_ANT_IGNORE_SYSTEM_CLASSES
109 # When set, <available> Ant tasks are rewritten to ignore Ant's runtime classpath.
113 *) EXPORT_FUNCTIONS src_configure ;;
116 # @FUNCTION: java-ant-2_src_configure
118 # src_configure rewrites the build.xml files automatically, unless EAPI is undefined, 0 or 1.
119 java-ant-2_src_configure() {
120 # if java support is optional, don't perform this when the USE flag is off
121 if has java-pkg-opt-2 ${INHERITED}; then
122 use ${JAVA_PKG_OPT_USE} || return
125 # eant will call us unless called by Portage
126 [[ -e "${T}/java-ant-2_src_configure-run" ]] && return
128 [[ "${JAVA_ANT_IGNORE_SYSTEM_CLASSES}" ]] \
129 && java-ant_ignore-system-classes "${S}/build.xml"
132 touch "${T}/java-ant-2_src_configure-run"
135 # @FUNCTION: java-ant_bsfix
138 # Attempts to fix build files.
141 # Affected by variables:
144 # JAVA_PKG_BSFIX_NAME,
147 debug-print-function ${FUNCNAME} $*
149 [[ "${JAVA_PKG_BSFIX}" != "on" ]] && return
150 if ! java-pkg_needs-vm; then
151 echo "QA Notice: Package is using java-ant, but doesn't depend on a Java VM"
154 pushd "${S}" >/dev/null || die
157 [[ "${JAVA_PKG_BSFIX_ALL}" == "yes" ]] || find_args="-maxdepth 1"
159 find_args="${find_args} -type f ( -name ${JAVA_PKG_BSFIX_NAME// / -o -name } )"
161 local bsfix_these=() line
162 while IFS= read -r -d $'\0' line; do
163 bsfix_these+=( "${line}" )
164 done < <(find . ${find_args} -print0)
166 [[ "${bsfix_these[@]}" ]] && java-ant_bsfix_files "${bsfix_these[@]}"
168 popd > /dev/null || die
171 # @FUNCTION: java-ant_bsfix_files
172 # @USAGE: <path/to/first/build.xml> [path/to/second.build.xml ...]
174 # Attempts to fix named build files.
177 # Affected by variables:
178 # JAVA_PKG_BSFIX_SOURCE_TAGS
179 # JAVA_PKG_BSFIX_TARGET_TAGS
180 # JAVA_ANT_REWRITE_CLASSPATH
181 # JAVA_ANT_JAVADOC_INPUT_DIRS: Where we can find java sources for javadoc
182 # input. Can be a space separated list of
184 # JAVA_ANT_BSFIX_EXTRA_ARGS: You can use this to pass extra variables to the
185 # rewriter if you know what you are doing.
188 # If JAVA_ANT_JAVADOC_INPUT_DIRS is set, we will turn on the adding of a basic
189 # javadoc target to the ant's build.xml with the javadoc xml-rewriter feature.
190 # Then we will set EANT DOC TARGET to the added javadoc target
191 # NOTE: the variable JAVA_ANT_JAVADOC_OUTPUT_DIR points where we will
192 # generate the javadocs. This is a read-only variable, dont change it.
194 # When changing this function, make sure that it works with paths with spaces in
196 java-ant_bsfix_files() {
197 debug-print-function ${FUNCNAME} $*
199 [[ ${#} = 0 ]] && die "${FUNCNAME} called without arguments"
201 local want_source="$(java-pkg_get-source)"
202 local want_target="$(java-pkg_get-target)"
204 debug-print "${FUNCNAME}: target: ${want_target} source: ${want_source}"
206 if [ -z "${want_source}" -o -z "${want_target}" ]; then
207 eerror "Could not find valid -source/-target values"
208 eerror "Please file a bug about this on bugs.gentoo.org"
209 die "Could not find valid -source/-target values"
213 for file in "${@}"; do
214 debug-print "${FUNCNAME}: ${file}"
216 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
217 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
220 if [[ ! -w "${file}" ]]; then
221 chmod u+w "${file}" || die "chmod u+w ${file} failed"
224 files+=( -f "${file}" )
227 if [ -e "${EPREFIX}/usr/libexec/javatoolkit" ]; then
228 local rewriter3="${EPREFIX}/usr/libexec/javatoolkit/xml-rewrite-3.py"
229 local rewriter4="${EPREFIX}/usr/libexec/javatoolkit/build-xml-rewrite"
231 local rewriter3="${EPREFIX}/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-3.py"
232 local rewriter4="${EPREFIX}/usr/$(get_libdir)/javatoolkit/bin/build-xml-rewrite"
235 if [[ -x ${rewriter4} && ${JAVA_ANT_ENCODING} ]]; then
236 [[ ${JAVA_ANT_REWRITE_CLASSPATH} ]] && local gcp="-g"
237 [[ ${JAVA_ANT_ENCODING} ]] && local enc="-e ${JAVA_ANT_ENCODING}"
238 echo "cElementTree rewriter"
239 debug-print "${rewriter4} extra args: ${gcp} ${enc}"
240 ${rewriter4} ${gcp} ${enc} \
241 -c "${JAVA_PKG_BSFIX_SOURCE_TAGS}" source ${want_source} \
242 -c "${JAVA_PKG_BSFIX_TARGET_TAGS}" target ${want_target} \
243 "${@}" || die "build-xml-rewrite failed"
245 debug-print "Using third generation rewriter"
246 echo "Rewriting attributes"
247 local bsfix_extra_args=()
248 # WARNING KEEP THE ORDER, ESPECIALLY FOR CHANGED ATTRIBUTES!
249 if [[ -n ${JAVA_ANT_REWRITE_CLASSPATH} ]]; then
250 local cp_tags="${JAVA_ANT_CLASSPATH_TAGS// / -e }"
251 bsfix_extra_args+=( -g -e ${cp_tags} )
252 bsfix_extra_args+=( -a classpath -v '${gentoo.classpath}' )
254 if [[ -n ${JAVA_ANT_JAVADOC_INPUT_DIRS} ]]; then
255 if [[ -n ${JAVA_ANT_JAVADOC_OUTPUT_DIR} ]]; then
256 die "Do not define JAVA_ANT_JAVADOC_OUTPUT_DIR!"
258 # Where will our generated javadoc go.
259 readonly JAVA_ANT_JAVADOC_OUTPUT_DIR="${WORKDIR}/gentoo_javadoc"
260 mkdir -p "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" || die
262 if has doc ${IUSE}; then
264 if [[ -z ${EANT_DOC_TARGET} ]]; then
265 EANT_DOC_TARGET="gentoojavadoc"
267 die "You can't use javadoc adding and set EANT_DOC_TARGET too."
270 for dir in ${JAVA_ANT_JAVADOC_INPUT_DIRS};do
271 if [[ ! -d ${dir} ]]; then
272 eerror "This dir: ${dir} doesnt' exists"
273 die "You must specify directories for javadoc input/output dirs."
276 bsfix_extra_args+=( --javadoc --source-directory )
277 # filter third/double spaces
278 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
279 JAVA_ANT_JAVADOC_INPUT_DIRS=${JAVA_ANT_JAVADOC_INPUT_DIRS// /}
280 bsfix_extra_args+=( ${JAVA_ANT_JAVADOC_INPUT_DIRS// / --source-directory } )
281 bsfix_extra_args+=( --output-directory "${JAVA_ANT_JAVADOC_OUTPUT_DIR}" )
284 die "You need to have doc in IUSE when using JAVA_ANT_JAVADOC_INPUT_DIRS"
288 [[ -n ${JAVA_ANT_BSFIX_EXTRA_ARGS} ]] \
289 && bsfix_extra_args+=( ${JAVA_ANT_BSFIX_EXTRA_ARGS} )
291 debug-print "bsfix_extra_args: ${bsfix_extra_args[*]}"
293 ${rewriter3} "${files[@]}" \
294 -c --source-element ${JAVA_PKG_BSFIX_SOURCE_TAGS// / --source-element } \
295 --source-attribute source --source-value ${want_source} \
296 --target-element ${JAVA_PKG_BSFIX_TARGET_TAGS// / --target-element } \
297 --target-attribute target --target-value ${want_target} \
298 --target-attribute nowarn --target-value yes \
299 "${bsfix_extra_args[@]}" \
300 || die "xml-rewrite-3 failed: ${file}"
303 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
304 for file in "${@}"; do
305 diff -NurbB "${file}.orig" "${file}"
309 return 0 # so that the 1 for diff doesn't get reported
313 # @FUNCTION: java-ant_bsfix_one
314 # @USAGE: <path/to/build.xml>
316 # Attempts to fix named build file.
319 # Affected by variables:
320 # JAVA_PKG_BSFIX_SOURCE_TAGS
321 # JAVA_PKG_BSFIX_TARGET_TAGS
323 java-ant_bsfix_one() {
324 debug-print-function ${FUNCNAME} $*
326 if [ -z "${1}" ]; then
327 eerror "${FUNCNAME} needs one argument"
328 die "${FUNCNAME} needs one argument"
331 java-ant_bsfix_files "${1}"
334 # @FUNCTION: java-ant_rewrite-classpath
335 # @USAGE: [path/to/build.xml]
337 # Adds 'classpath="${gentoo.classpath}"' to specified build file.
340 # JAVA_ANT_CLASSPATH_TAGS
342 # Parameter defaults to build.xml when not specified
343 java-ant_rewrite-classpath() {
344 debug-print-function ${FUNCNAME} $*
347 [[ -z "${1}" ]] && file=build.xml
348 [[ ${#} -gt 1 ]] && die "${FUNCNAME} currently can only rewrite one file."
350 echo "Adding gentoo.classpath to ${file}"
351 debug-print "java-ant_rewrite-classpath: ${file}"
353 cp "${file}" "${file}.orig" || die "failed to copy ${file}"
357 java-ant_xml-rewrite -f "${file}" --change \
358 -e ${JAVA_ANT_CLASSPATH_TAGS// / -e } -a classpath -v '${gentoo.classpath}'
360 if [[ -n "${JAVA_PKG_DEBUG}" ]]; then
361 diff -NurbB "${file}.orig" "${file}"
365 # @FUNCTION: java-ant_ignore-system-classes
366 # @USAGE: [path/to/build.xml]
368 # Makes the available task ignore classes in the system classpath
369 # Parameter defaults to build.xml when not specified
370 java-ant_ignore-system-classes() {
371 debug-print-function ${FUNCNAME} $*
372 local file=${1:-build.xml}
373 echo "Changing ignoresystemclasses to true for available tasks in ${file}"
374 java-ant_xml-rewrite -f "${file}" --change \
375 -e available -a ignoresystemclasses -v "true"
378 # @FUNCTION: java-ant_xml-rewrite
379 # @USAGE: <xml rewriter arguments>
381 # Run the right xml-rewrite binary with the given arguments
382 java-ant_xml-rewrite() {
383 local gen2_1="${EPREFIX}/usr/$(get_libdir)/javatoolkit/bin/xml-rewrite-2.py"
384 local gen2_2="${EPREFIX}/usr/libexec/javatoolkit/xml-rewrite-2.py"
386 if [[ -x "${gen2_2}" ]]; then
387 ${gen2_2} "${@}" || die "${gen2_2} failed"
388 elif [[ -x "${gen2_1}" ]]; then
389 ${gen2_1} "${@}" || die "${gen2_1} failed"
391 eerror "No binary for rewriting found."
392 eerror "Do you have dev-java/javatoolkit installed?"
393 die "xml-rewrite not found"
397 # @FUNCTION: java-ant_rewrite-bootclasspath
398 # @USAGE: <version> [path/to/build.xml] [prepend] [append]
400 # Adds bootclasspath to javac-like tasks in build.xml filled with jars of a
401 # bootclasspath package of given version.
405 # JAVA_PKG_BSFIX_TARGET_TAGS - the tags of javac tasks
408 # $1 - the version of bootclasspath (e.g. 1.5), 'auto' for bootclasspath
410 # $2 - path to desired build.xml file, defaults to 'build.xml'
411 # $3 - (optional) what to prepend the bootclasspath with (to override)
412 # $4 - (optional) what to append to the bootclasspath
414 java-ant_rewrite-bootclasspath() {
416 local file="${2-build.xml}"
417 local extra_before="${3}"
418 local extra_after="${4}"
420 local bcp="$(java-pkg_get-bootclasspath "${version}")"
422 if [[ -n "${extra_before}" ]]; then
423 bcp="${extra_before}:${bcp}"
425 if [[ -n "${extra_after}" ]]; then
426 bcp="${bcp}:${extra_after}"
429 java-ant_xml-rewrite -f "${file}" -c -e ${JAVA_PKG_BSFIX_TARGET_TAGS// / -e } \
430 -a bootclasspath -v "${bcp}"