On 2017-12-31, Lasse Collin wrote:

> On 2017-12-30 Stefan Bodewig wrote:
>> if XZ for Java is used as a Java9 module java will derive the name of
>> the module from the jar name and will most likely be "xz". If you add
>> an "Automatic-Module-Name" entry to the jar's manifest you can
>> control the name yourself, without turing XZ into a proper Java9
>> module.

> I read a little about Java modules. I got an impression that
> Automatic-Module-Name is a good start and the minimum that libraries
> should do. So adding that entry sounds good.

> Would it be too complicated to turn XZ into a proper module? How useful
> is that?

As XZ hasn't got any dependencies you'd only benefit from an explicit
module info if you wanted to restrict the set of packages you
expose. Otherwise I think Automatic-Module-Name would achieve exactly
the same thing that you could achieve with an explicit
module-info.java. At least for now. module-info.java may give you more
control in the long term, though.

> From what I read, it needs module-info.java which should be easy to
> write as XZ doesn't depend on external packages. I don't know how to
> set it up with Ant. It would be very good if XZ could be built on
> older JDKs too, even if those compilers cannot build the
> module-info.java file.

You could check for JDK9 and only compile module-info.java if you found
it. That way XZ compiled with Java8 or earlier wouldn't be a proper
Java9 module and one compiled with Java9 would contain the module
descriptor.

Assuming you'd want to target Java7 and compile module-info.java if Ant
was actually on Java9 something like the patch below should do it. Here
module-info.java would be compiled by a separate javac task that comes
up with an empty set of sources to compile on Java8 and earlier.

Stefan

diff --git a/build.xml b/build.xml
index 52d3764..514e0e0 100644
--- a/build.xml
+++ b/build.xml
@@ -44,13 +44,22 @@
     </target>
 
     <target name="compile" description="Compiles the classes">
+        <available property="jdk9+" 
classname="java.lang.module.ModuleDescriptor"/>
         <mkdir dir="${classes_dir}"/>
         <javac srcdir="." sourcepath="${src_dir}" destdir="${classes_dir}"
+               target="7" source="7"
                includeAntRuntime="false" debug="${debug}"
                includesfile="fileset-src.txt"
                excludes="**/package-info.java">
             <compilerarg compiler="modern" value="-Xlint"/>
         </javac>
+        <javac srcdir="${src_dir}" destdir="${classes_dir}"
+               includeAntRuntime="false" debug="${debug}"
+               target="9" source="9"
+               taskname="compile-module-info">
+            <include name="**/module-info.java"/>
+            <exclude name="**/module-info.java" unless="jdk9+"/>
+        </javac>
     </target>
 
     <target name="jar" depends="compile"
@@ -71,6 +80,7 @@
                 <attribute name="Export-Package" value="org.tukaani.xz"/>
                 <attribute name="Bundle-Name" value="${title}"/>
                 <attribute name="Bundle-DocURL" value="${homepage}"/>
+                <attribute name="Automatic-Module-Name" 
value="org.tukaani.xz"/>
             </manifest>
         </jar>
 

Reply via email to