1 # Copyright 2004-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: java-pkg-simple.eclass
9 # Java maintainers (java@gentoo.org)
10 # @BLURB: Eclass for packaging Java software with ease.
12 # This class is intended to build pure Java packages from Java sources
13 # without the use of any build instructions shipped with the sources.
14 # There is no support for resources besides the generated class files,
15 # or for generating source files, or for controlling the META-INF of
16 # the resulting jar, although these issues may be addressed by an
17 # ebuild by putting corresponding files into the target directory
18 # before calling the src_compile function of this eclass.
22 if ! has java-pkg-2 ${INHERITED}; then
23 eerror "java-pkg-simple eclass can only be inherited AFTER java-pkg-2"
26 EXPORT_FUNCTIONS src_compile src_install
28 # We are only interested in finding all java source files, wherever they may be.
31 # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH
34 # Comma or space separated list of java packages to include in the
35 # class path. The packages will also be registered as runtime
36 # dependencies of this new package. Dependencies will be calculated
37 # transitively. See "java-config -l" for appropriate package names.
40 # JAVA_GENTOO_CLASSPATH="foo,bar-2"
43 # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH_EXTRA
46 # Extra list of colon separated path elements to be put on the
47 # classpath when compiling sources.
49 # @ECLASS-VARIABLE: JAVA_SRC_DIR
52 # Directories relative to ${S} which contain the sources of the
53 # application. The default of "" will be treated mostly as ${S}
54 # itself. For the generated source package (if source is listed in
55 # ${JAVA_PKG_IUSE}), it is important that these directories are
56 # actually the roots of the corresponding source trees.
59 # JAVA_SRC_DIR="src/java/org/gentoo"
62 # @ECLASS-VARIABLE: JAVA_ENCODING
64 # The character encoding used in the source files.
65 : ${JAVA_ENCODING:=UTF-8}
67 # @ECLASS-VARIABLE: JAVAC_ARGS
70 # Additional arguments to be passed to javac.
72 # @ECLASS-VARIABLE: JAVADOC_ARGS
75 # Additional arguments to be passed to javadoc.
77 # @ECLASS-VARIABLE: JAVA_JAR_FILENAME
79 # The name of the jar file to create and install.
80 : ${JAVA_JAR_FILENAME:=${PN}.jar}
82 # @FUNCTION: java-pkg-simple_src_compile
84 # src_compile for simple bare source java packages. Finds all *.java
85 # sources in ${JAVA_SRC_DIR}, compiles them with the classpath
86 # calculated from ${JAVA_GENTOO_CLASSPATH}, and packages the resulting
87 # classes to ${JAVA_JAR_FILENAME}.
88 java-pkg-simple_src_compile() {
89 local sources=sources.lst classes=target/classes apidoc=target/api
92 find ${JAVA_SRC_DIR:-*} -name \*.java > ${sources}
93 mkdir -p ${classes} || die "Could not create target directory"
96 local classpath="${JAVA_GENTOO_CLASSPATH_EXTRA}" dependency
97 for dependency in ${JAVA_GENTOO_CLASSPATH}; do
98 classpath="${classpath}:$(java-pkg_getjars ${dependency})" \
99 || die "getjars failed for ${dependency}"
101 while [[ $classpath = *::* ]]; do classpath="${classpath//::/:}"; done
102 classpath=${classpath%:}
103 classpath=${classpath#:}
104 debug-print "CLASSPATH=${classpath}"
105 ejavac -d ${classes} -encoding ${JAVA_ENCODING} \
106 ${classpath:+-classpath ${classpath}} ${JAVAC_ARGS} \
110 if has doc ${JAVA_PKG_IUSE} && use doc; then
112 ejavadoc -d ${apidoc} \
113 -encoding ${JAVA_ENCODING} -docencoding UTF-8 -charset UTF-8 \
114 ${classpath:+-classpath ${classpath}} ${JAVADOC_ARGS:- -quiet} \
115 @${sources} || die "javadoc failed"
119 local jar_args="cf ${JAVA_JAR_FILENAME}"
120 if [[ -e ${classes}/META-INF/MANIFEST.MF ]]; then
121 jar_args="cfm ${JAVA_JAR_FILENAME} ${classes}/META-INF/MANIFEST.MF"
123 jar ${jar_args} -C ${classes} . || die "jar failed"
126 # @FUNCTION: java-pkg-simple_src_install
128 # src_install for simple single jar java packages. Simply packages the
129 # contents from the target directory and installs it as
130 # ${JAVA_JAR_FILENAME}. If the file target/META-INF/MANIFEST.MF exists,
131 # it is used as the manifest of the created jar.
132 java-pkg-simple_src_install() {
133 local sources=sources.lst classes=target/classes apidoc=target/api
136 java-pkg_dojar ${JAVA_JAR_FILENAME}
139 if has doc ${JAVA_PKG_IUSE} && use doc; then
140 java-pkg_dojavadoc ${apidoc}
144 if has source ${JAVA_PKG_IUSE} && use source; then
146 if [[ ${JAVA_SRC_DIR} ]]; then
148 for parent in ${JAVA_SRC_DIR}; do
149 for child in ${parent}/*; do
150 srcdirs="${srcdirs} ${child}"
154 # take all directories actually containing any sources
155 srcdirs="$(cut -d/ -f1 ${sources} | sort -u)"
157 java-pkg_dosrc ${srcdirs}