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