distutils-r1.eclass: Ban EXAMPLES in EAPI 6
[gentoo.git] / eclass / java-pkg-simple.eclass
1 # Copyright 2004-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: java-pkg-simple.eclass
6 # @MAINTAINER:
7 # java@gentoo.org
8 # @AUTHOR:
9 # Java maintainers (java@gentoo.org)
10 # @BLURB: Eclass for packaging Java software with ease.
11 # @DESCRIPTION:
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.
19
20 inherit java-utils-2
21
22 if ! has java-pkg-2 ${INHERITED}; then
23         eerror "java-pkg-simple eclass can only be inherited AFTER java-pkg-2"
24 fi
25
26 EXPORT_FUNCTIONS src_compile src_install
27
28 # We are only interested in finding all java source files, wherever they may be.
29 S="${WORKDIR}"
30
31 # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH
32 # @DEFAULT_UNSET
33 # @DESCRIPTION:
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.
38 #
39 # @CODE
40 #       JAVA_GENTOO_CLASSPATH="foo,bar-2"
41 # @CODE
42
43 # @ECLASS-VARIABLE: JAVA_GENTOO_CLASSPATH_EXTRA
44 # @DEFAULT_UNSET
45 # @DESCRIPTION:
46 # Extra list of colon separated path elements to be put on the
47 # classpath when compiling sources.
48
49 # @ECLASS-VARIABLE: JAVA_SRC_DIR
50 # @DEFAULT_UNSET
51 # @DESCRIPTION:
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.
57 #
58 # @CODE
59 #       JAVA_SRC_DIR="src/java/org/gentoo"
60 # @CODE
61
62 # @ECLASS-VARIABLE: JAVA_ENCODING
63 # @DESCRIPTION:
64 # The character encoding used in the source files.
65 : ${JAVA_ENCODING:=UTF-8}
66
67 # @ECLASS-VARIABLE: JAVAC_ARGS
68 # @DEFAULT_UNSET
69 # @DESCRIPTION:
70 # Additional arguments to be passed to javac.
71
72 # @ECLASS-VARIABLE: JAVADOC_ARGS
73 # @DEFAULT_UNSET
74 # @DESCRIPTION:
75 # Additional arguments to be passed to javadoc.
76
77 # @ECLASS-VARIABLE: JAVA_JAR_FILENAME
78 # @DESCRIPTION:
79 # The name of the jar file to create and install.
80 : ${JAVA_JAR_FILENAME:=${PN}.jar}
81
82 # @FUNCTION: java-pkg-simple_src_compile
83 # @DESCRIPTION:
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
90
91         # gather sources
92         find ${JAVA_SRC_DIR:-*} -name \*.java > ${sources}
93         mkdir -p ${classes} || die "Could not create target directory"
94
95         # compile
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}"
100         done
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} \
107                 @${sources}
108
109         # javadoc
110         if has doc ${JAVA_PKG_IUSE} && use doc; then
111                 mkdir -p ${apidoc}
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"
116         fi
117
118         # package
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"
122         fi
123         jar ${jar_args} -C ${classes} . || die "jar failed"
124 }
125
126 # @FUNCTION: java-pkg-simple_src_install
127 # @DESCRIPTION:
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
134
135         # main jar
136         java-pkg_dojar ${JAVA_JAR_FILENAME}
137
138         # javadoc
139         if has doc ${JAVA_PKG_IUSE} && use doc; then
140                 java-pkg_dojavadoc ${apidoc}
141         fi
142
143         # dosrc
144         if has source ${JAVA_PKG_IUSE} && use source; then
145                 local srcdirs=""
146                 if [[ ${JAVA_SRC_DIR} ]]; then
147                         local parent child
148                         for parent in ${JAVA_SRC_DIR}; do
149                                 for child in ${parent}/*; do
150                                         srcdirs="${srcdirs} ${child}"
151                                 done
152                         done
153                 else
154                         # take all directories actually containing any sources
155                         srcdirs="$(cut -d/ -f1 ${sources} | sort -u)"
156                 fi
157                 java-pkg_dosrc ${srcdirs}
158         fi
159 }