Getting project classes from plugin

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting project classes from plugin

Christopher
Hi,

I'm trying to write a Maven plugin which gets, and processes, a list of
classes from the project. I want to be able to get the project classes
compiled from either src/main/java (compile scope), and src/test/java (test
scope, minus compile scope), depending on user configuration.

So far, the closest things I've found to help me are (project is an
instance of MavenProject):
project.getCompileClasspathElements()
and
project.getTestClasspathElements()

I then use that to build a URLClassLoader, which I then use Guava's
ClassPath utility to find all the classes.

The problem is, that brings in the whole classpath, and all I want is just
the project's own classes, not those from dependencies (including Java
itself). And, the test classpath elements includes the compile-time scoped
items, as well, which I don't necessarily want. I could probably do set
subtraction to remove the compile-time scope from the test scope, but I
can't figure out how to get rid of what's being added as dependencies,
which I don't wish to process. Perhaps I can do set subtraction from the
dependencies? Is there a way to enumerate the classpath of just the
dependencies?

Has anybody done anything like this? Is there a better way to get only the
project's own classes?
Reply | Threaded
Open this post in threaded view
|

Re: Getting project classes from plugin

Barrie Treloar
On 5 September 2016 at 12:11, Christopher <[hidden email]> wrote:

> Hi,
>
> I'm trying to write a Maven plugin which gets, and processes, a list of
> classes from the project. I want to be able to get the project classes
> compiled from either src/main/java (compile scope), and src/test/java (test
> scope, minus compile scope), depending on user configuration.
>
> So far, the closest things I've found to help me are (project is an
> instance of MavenProject):
> project.getCompileClasspathElements()
> and
> project.getTestClasspathElements()
>
> I then use that to build a URLClassLoader, which I then use Guava's
> ClassPath utility to find all the classes.
>
> The problem is, that brings in the whole classpath, and all I want is just
> the project's own classes, not those from dependencies (including Java
> itself). And, the test classpath elements includes the compile-time scoped
> items, as well, which I don't necessarily want. I could probably do set
> subtraction to remove the compile-time scope from the test scope, but I
> can't figure out how to get rid of what's being added as dependencies,
> which I don't wish to process. Perhaps I can do set subtraction from the
> dependencies? Is there a way to enumerate the classpath of just the
> dependencies?
>
> Has anybody done anything like this? Is there a better way to get only the
> project's own classes?
>

Why are you wanting to do this?

Are the output files in target/classes and target/test-classes not
sufficient?
Reply | Threaded
Open this post in threaded view
|

Re: Getting project classes from plugin

Francois-Xavier Bonnet-3
When I wrote builder-maven-plugin <https://javabuild.java.net/> I had to do
things with some project classes. What I did was scan the source code and
then load the corresponding .class for each .java file found. Maybe you
could do the same.
The code is here:
https://github.com/javabuild/builder-parent/blob/master/builder-maven-plugin/src/main/java/net/java/javabuild/ExecuteMojo.java

On 5 September 2016 at 13:56, Barrie Treloar <[hidden email]> wrote:

> On 5 September 2016 at 12:11, Christopher <[hidden email]> wrote:
>
> > Hi,
> >
> > I'm trying to write a Maven plugin which gets, and processes, a list of
> > classes from the project. I want to be able to get the project classes
> > compiled from either src/main/java (compile scope), and src/test/java
> (test
> > scope, minus compile scope), depending on user configuration.
> >
> > So far, the closest things I've found to help me are (project is an
> > instance of MavenProject):
> > project.getCompileClasspathElements()
> > and
> > project.getTestClasspathElements()
> >
> > I then use that to build a URLClassLoader, which I then use Guava's
> > ClassPath utility to find all the classes.
> >
> > The problem is, that brings in the whole classpath, and all I want is
> just
> > the project's own classes, not those from dependencies (including Java
> > itself). And, the test classpath elements includes the compile-time
> scoped
> > items, as well, which I don't necessarily want. I could probably do set
> > subtraction to remove the compile-time scope from the test scope, but I
> > can't figure out how to get rid of what's being added as dependencies,
> > which I don't wish to process. Perhaps I can do set subtraction from the
> > dependencies? Is there a way to enumerate the classpath of just the
> > dependencies?
> >
> > Has anybody done anything like this? Is there a better way to get only
> the
> > project's own classes?
> >
>
> Why are you wanting to do this?
>
> Are the output files in target/classes and target/test-classes not
> sufficient?
>
Reply | Threaded
Open this post in threaded view
|

Re: Getting project classes from plugin

gboue
Hi,

You should be able to get only the project compiled classes with
project.getBuild().getOutputDirectory(). Internally, this is what
getCompileClasspathElements does. For the test classpath, you have the
similar project.getBuild().getTestOutputDirectory().

With those two folders, you can create the URLClassLoader that load the
classes.

Guillaume,


Le 05/09/2016 à 06:05, Francois-Xavier Bonnet a écrit :

> When I wrote builder-maven-plugin <https://javabuild.java.net/> I had to do
> things with some project classes. What I did was scan the source code and
> then load the corresponding .class for each .java file found. Maybe you
> could do the same.
> The code is here:
> https://github.com/javabuild/builder-parent/blob/master/builder-maven-plugin/src/main/java/net/java/javabuild/ExecuteMojo.java
>
> On 5 September 2016 at 13:56, Barrie Treloar <[hidden email]> wrote:
>
>> On 5 September 2016 at 12:11, Christopher <[hidden email]> wrote:
>>
>>> Hi,
>>>
>>> I'm trying to write a Maven plugin which gets, and processes, a list of
>>> classes from the project. I want to be able to get the project classes
>>> compiled from either src/main/java (compile scope), and src/test/java
>> (test
>>> scope, minus compile scope), depending on user configuration.
>>>
>>> So far, the closest things I've found to help me are (project is an
>>> instance of MavenProject):
>>> project.getCompileClasspathElements()
>>> and
>>> project.getTestClasspathElements()
>>>
>>> I then use that to build a URLClassLoader, which I then use Guava's
>>> ClassPath utility to find all the classes.
>>>
>>> The problem is, that brings in the whole classpath, and all I want is
>> just
>>> the project's own classes, not those from dependencies (including Java
>>> itself). And, the test classpath elements includes the compile-time
>> scoped
>>> items, as well, which I don't necessarily want. I could probably do set
>>> subtraction to remove the compile-time scope from the test scope, but I
>>> can't figure out how to get rid of what's being added as dependencies,
>>> which I don't wish to process. Perhaps I can do set subtraction from the
>>> dependencies? Is there a way to enumerate the classpath of just the
>>> dependencies?
>>>
>>> Has anybody done anything like this? Is there a better way to get only
>> the
>>> project's own classes?
>>>
>> Why are you wanting to do this?
>>
>> Are the output files in target/classes and target/test-classes not
>> sufficient?
>>


---
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
https://www.avast.com/antivirus


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Getting project classes from plugin

Christopher
Unfortunately, using project.getBuild().getOutputDirectory() doesn't work
either. The classloader I created from that still contains other classes,
like plexus, and jdk classes. Additionally, because it didn't have the
dependencies, some of the annotations I need to process weren't loaded by
the classloader.

At this point, I think what I need to do is manually walk through the
*.class files in project.getBuild().getOutputDirectory() and use what I
find to filter the classes in the classloader from
project.getCompileClasspathElements(). I was hoping Maven provided a
primitive to list these classes, but walking the directory tree should
work. I've just got to be careful about matching the filenames with the
class names in the classloader, especially with respect to anonymous
inner-classes, member classes, static nested classes, and secondary classes
defined in the same .java file.

On Mon, Sep 5, 2016 at 1:40 PM Guillaume Boué <[hidden email]> wrote:

> Hi,
>
> You should be able to get only the project compiled classes with
> project.getBuild().getOutputDirectory(). Internally, this is what
> getCompileClasspathElements does. For the test classpath, you have the
> similar project.getBuild().getTestOutputDirectory().
>
> With those two folders, you can create the URLClassLoader that load the
> classes.
>
> Guillaume,
>
>
> Le 05/09/2016 à 06:05, Francois-Xavier Bonnet a écrit :
> > When I wrote builder-maven-plugin <https://javabuild.java.net/> I had
> to do
> > things with some project classes. What I did was scan the source code and
> > then load the corresponding .class for each .java file found. Maybe you
> > could do the same.
> > The code is here:
> >
> https://github.com/javabuild/builder-parent/blob/master/builder-maven-plugin/src/main/java/net/java/javabuild/ExecuteMojo.java
> >
> > On 5 September 2016 at 13:56, Barrie Treloar <[hidden email]> wrote:
> >
> >> On 5 September 2016 at 12:11, Christopher <[hidden email]> wrote:
> >>
> >>> Hi,
> >>>
> >>> I'm trying to write a Maven plugin which gets, and processes, a list of
> >>> classes from the project. I want to be able to get the project classes
> >>> compiled from either src/main/java (compile scope), and src/test/java
> >> (test
> >>> scope, minus compile scope), depending on user configuration.
> >>>
> >>> So far, the closest things I've found to help me are (project is an
> >>> instance of MavenProject):
> >>> project.getCompileClasspathElements()
> >>> and
> >>> project.getTestClasspathElements()
> >>>
> >>> I then use that to build a URLClassLoader, which I then use Guava's
> >>> ClassPath utility to find all the classes.
> >>>
> >>> The problem is, that brings in the whole classpath, and all I want is
> >> just
> >>> the project's own classes, not those from dependencies (including Java
> >>> itself). And, the test classpath elements includes the compile-time
> >> scoped
> >>> items, as well, which I don't necessarily want. I could probably do set
> >>> subtraction to remove the compile-time scope from the test scope, but I
> >>> can't figure out how to get rid of what's being added as dependencies,
> >>> which I don't wish to process. Perhaps I can do set subtraction from
> the
> >>> dependencies? Is there a way to enumerate the classpath of just the
> >>> dependencies?
> >>>
> >>> Has anybody done anything like this? Is there a better way to get only
> >> the
> >>> project's own classes?
> >>>
> >> Why are you wanting to do this?
> >>
> >> Are the output files in target/classes and target/test-classes not
> >> sufficient?
> >>
>
>
> ---
> L'absence de virus dans ce courrier électronique a été vérifiée par le
> logiciel antivirus Avast.
> https://www.avast.com/antivirus
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Getting project classes from plugin

Christopher
If it helps anybody else, I ended up using Guava's
ClassInfo.getResourceName() and concatenated that with
project.getBuild().get[Test]OutputDirectory() and checked that with
File.exists(). I then encapsulated that in a Java 8 Predicate called
"isProjectClass()" and used that as a Java 8 Stream filter. I'll soon be
pushing this to https://github.com/revelc/warbucks-maven-plugin, if anybody
is interested in reusing my strategy.

On Mon, Sep 5, 2016 at 2:04 PM Christopher <[hidden email]> wrote:

> Unfortunately, using project.getBuild().getOutputDirectory() doesn't work
> either. The classloader I created from that still contains other classes,
> like plexus, and jdk classes. Additionally, because it didn't have the
> dependencies, some of the annotations I need to process weren't loaded by
> the classloader.
>
> At this point, I think what I need to do is manually walk through the
> *.class files in project.getBuild().getOutputDirectory() and use what I
> find to filter the classes in the classloader from
> project.getCompileClasspathElements(). I was hoping Maven provided a
> primitive to list these classes, but walking the directory tree should
> work. I've just got to be careful about matching the filenames with the
> class names in the classloader, especially with respect to anonymous
> inner-classes, member classes, static nested classes, and secondary classes
> defined in the same .java file.
>
> On Mon, Sep 5, 2016 at 1:40 PM Guillaume Boué <[hidden email]> wrote:
>
>> Hi,
>>
>> You should be able to get only the project compiled classes with
>> project.getBuild().getOutputDirectory(). Internally, this is what
>> getCompileClasspathElements does. For the test classpath, you have the
>> similar project.getBuild().getTestOutputDirectory().
>>
>> With those two folders, you can create the URLClassLoader that load the
>> classes.
>>
>> Guillaume,
>>
>>
>> Le 05/09/2016 à 06:05, Francois-Xavier Bonnet a écrit :
>> > When I wrote builder-maven-plugin <https://javabuild.java.net/> I had
>> to do
>> > things with some project classes. What I did was scan the source code
>> and
>> > then load the corresponding .class for each .java file found. Maybe you
>> > could do the same.
>> > The code is here:
>> >
>> https://github.com/javabuild/builder-parent/blob/master/builder-maven-plugin/src/main/java/net/java/javabuild/ExecuteMojo.java
>> >
>> > On 5 September 2016 at 13:56, Barrie Treloar <[hidden email]>
>> wrote:
>> >
>> >> On 5 September 2016 at 12:11, Christopher <[hidden email]> wrote:
>> >>
>> >>> Hi,
>> >>>
>> >>> I'm trying to write a Maven plugin which gets, and processes, a list
>> of
>> >>> classes from the project. I want to be able to get the project classes
>> >>> compiled from either src/main/java (compile scope), and src/test/java
>> >> (test
>> >>> scope, minus compile scope), depending on user configuration.
>> >>>
>> >>> So far, the closest things I've found to help me are (project is an
>> >>> instance of MavenProject):
>> >>> project.getCompileClasspathElements()
>> >>> and
>> >>> project.getTestClasspathElements()
>> >>>
>> >>> I then use that to build a URLClassLoader, which I then use Guava's
>> >>> ClassPath utility to find all the classes.
>> >>>
>> >>> The problem is, that brings in the whole classpath, and all I want is
>> >> just
>> >>> the project's own classes, not those from dependencies (including Java
>> >>> itself). And, the test classpath elements includes the compile-time
>> >> scoped
>> >>> items, as well, which I don't necessarily want. I could probably do
>> set
>> >>> subtraction to remove the compile-time scope from the test scope, but
>> I
>> >>> can't figure out how to get rid of what's being added as dependencies,
>> >>> which I don't wish to process. Perhaps I can do set subtraction from
>> the
>> >>> dependencies? Is there a way to enumerate the classpath of just the
>> >>> dependencies?
>> >>>
>> >>> Has anybody done anything like this? Is there a better way to get only
>> >> the
>> >>> project's own classes?
>> >>>
>> >> Why are you wanting to do this?
>> >>
>> >> Are the output files in target/classes and target/test-classes not
>> >> sufficient?
>> >>
>>
>>
>> ---
>> L'absence de virus dans ce courrier électronique a été vérifiée par le
>> logiciel antivirus Avast.
>> https://www.avast.com/antivirus
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>