Re: PL/Java new build plugin

From: Kartik Ohri <kartikohri13(at)gmail(dot)com>
To: Chapman Flack <chap(at)anastigmatix(dot)net>
Cc: pljava-dev(at)lists(dot)postgresql(dot)org
Subject: Re: PL/Java new build plugin
Date: 2020-07-18 19:49:42
Message-ID: CAASLQ4OR8ZD4GRNHVGuTzJp_UP-PAcADxcz=uymiSFPpg8EEHQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pljava-dev

>
> But I would really like to know what ends up happening when a Maven
> classpathref is passed to Ant, and ends up solving the problem.
> What is a Maven classpathref? Is it just a list of URLs, or some
> other object?
>

I went through the relevant codebase again going through the files a
Project or a Script Task call. As the manual mentions here, [1]
<https://ant.apache.org/manual/Tasks/script.html> a classpathref is just a
reference to paths stored elsewhere, I also realized that I had it all
inside out. maven.plugin.classpath is not something that maven declares. It
is declared by the maven antrun plugin for use by ant tasks [2]
<https://github.com/apache/maven-antrun-plugin/blob/8e5b3c613e96c20881347ec16813864a2d8ef0bb/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java#L383>.
I had completely understood the opposite of it. This maven-antrun docs
verify this [3]
<https://maven.apache.org/plugins/maven-antrun-plugin/examples/classpaths.html>.
This means that adding classpathref="maven.plugin.classpath" to our
scripts will not be useful. The maven plugin classpath is essentially the
reference to artifact paths. In our case, the maven.plugin.classpath would
be referring to,
/home/amcap1712/pljava/pljava-pgxs/target/pljava-pgxs-0.0.1-SNAPSHOT.jar
/home/amcap1712/.m2/repository/org/codehaus/plexus/plexus-utils/1.1/plexus-utils-1.1.jar
joined into using a file separator. I used the code from maven-antrun to
generate this [4]
<https://github.com/apache/maven-antrun-plugin/blob/8e5b3c613e96c20881347ec16813864a2d8ef0bb/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java#L398>
.

I wonder if it ends up working sort of by accident, maybe Ant uses
> the classpathref to make a more usual classloader structure that has
> the same URLs as the ClassRealm loader, but delegates from there to
> the AppClassLoader instead of the ClassRealm. That could be plausible,
> as Ant came first and wouldn't know about a ClassRealm. It would be
> good to see it in the code though.
>

Ant uses a custom AntLoader which extends the ClassLoader [5]
<https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/AntClassLoader.java>.
It is heavily customized and over 1600 lines. I am still going through it
and trying to understand what it does but it is definitely more than a
URLClassLoader. But I am sure, we might be able to chomp down a lot from
this class loader even if we have to implement one of our own. To
supplement the AntLoaderClass, Ant also has a ClasspathUtils class [6]
<https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/util/ClasspathUtils.java>
which
also provides an instance of an AntLoader. I think this class only serves
as a convenience class and to implement good software design patterns.

------------------------

The actual script evaluation happens here [6]
<https://github.com/apache/ant/blob/1ce1cc2317cb2b2773ab6b765f1a79ccd4806926/src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java#L94>,
assuming Apache BSF is not present. (Since, the scripts do not specify the
manager to use, BSF is preferred if present according to [1] above. If BSF
is absent, then javax.script is used.) Most of the code in the function is
uninteresting and generic (in my opinion) , however one interesting thing
does happen. Before executing the script, Ant changes the ClassLoader and
stores the ClassLoader which it replaces. After the script has been
evaluated, it again switches the ClassLoader to the one which was before it
was replaced in the first place. I do not understand why it is done.

Some other interesting or mysterious things I found in the code related to
the Script task. Here [7]
<https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/util/ScriptRunnerCreator.java#L110>,
Ant loads the javax.script (or BSF) manager class manually into the
classpath which I don't understand why (to be fair, I don't know what
Class.forName even does, my best guess is load the class into classpath).
Then Here [8]
<https://github.com/apache/ant/blob/1ce1cc2317cb2b2773ab6b765f1a79ccd4806926/src/main/org/apache/tools/ant/util/ScriptRunnerHelper.java#L225>,
Ant chooses between three methods to create a classloader. Again, I don't
have the slightest idea why so.

This is all I could gather from the source. I think our answer may lie
inside this most probably inside AntLoader. I'll try to study the AntLoader
class to grasp it and find out the cause.

Regards,
Kartik

In response to

Responses

Browse pljava-dev by date

  From Date Subject
Next Message Chapman Flack 2020-07-18 20:28:52 Re: PL/Java new build plugin
Previous Message Chapman Flack 2020-07-18 18:01:28 Re: PL/Java new build plugin