You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Andres Almiray <aa...@gmail.com> on 2020/04/16 10:54:59 UTC

Launching full Maven programmatically

Hi,

I'm looking for a way to launch Maven programmatically in such a way that I
can get access to fully resolved Model and MavenProject instances, and not
just for a single project but also for all projects that belong to a
Reactor.

At first I used this code

try {
    FileReader reader = new FileReader(pom);
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    return new MavenProject(mavenReader.read(reader));
} catch (Exception e) {
    throw new IllegalArgumentException(e);
}

But of course that's not enough. Next I tried using the maven-model-builder
APIs directly but not all references get resolved (such as parent POMs) as
a custom ModelResolver is required and the default one is not intended to
be used in standalone fashion.

Ideally I would call some sort of embeddable Maven API that can give me the
answers I seek, something like this

MavenProject project = EmbeddableMaven.launch(propsOrFlags).file(pomFile);

or alternatively something like

MavenReactor reactor = EmbeddableMaven.launch(propsOrFlags).file(pomFile);
for(MavenProject project : reactor.getProjects()) { /* do stuff */ }

Of course I'm making up these types and method names as I do not know if
something like this exist or not, hence why I'm asking here.

Thanks in advance.

Cheers
Andres

Re: Launching full Maven programmatically

Posted by Matthieu BROUILLARD <ma...@brouillard.fr>.
I understood your need Andreas. Look at the plugin, if I remember well at
some point it "reloads/recreate" from scratch the Model ; as you would do
in your own code ; to be able to later flatten it.

Regards,

Matthieu

On Thu, Apr 16, 2020 at 4:31 PM Andres Almiray <aa...@gmail.com> wrote:

> Thanks Matthieu, I'll have a look.
>
> Though I failed to specify: the embedded API I'm looking for should work
> anywhere and not just within the confinement of a maven plugin where Maven
> components and services exist, that is, this API should work as a blackbox
> and in standalone mode.
>
> Cheers,
> Andres
>
> On Thu, Apr 16, 2020 at 3:13 PM Matthieu BROUILLARD <
> matthieu@brouillard.fr>
> wrote:
>
> > Hi,
> >
> > If I remember well I think maven-flatten-plugin loads the model in a way
> > that even core-extensions are used; you should have a look there.
> >
> > Matthieu
> >
>

Re: Launching full Maven programmatically

Posted by Andres Almiray <aa...@gmail.com>.
Thanks Matthieu, I'll have a look.

Though I failed to specify: the embedded API I'm looking for should work
anywhere and not just within the confinement of a maven plugin where Maven
components and services exist, that is, this API should work as a blackbox
and in standalone mode.

Cheers,
Andres

On Thu, Apr 16, 2020 at 3:13 PM Matthieu BROUILLARD <ma...@brouillard.fr>
wrote:

> Hi,
>
> If I remember well I think maven-flatten-plugin loads the model in a way
> that even core-extensions are used; you should have a look there.
>
> Matthieu
>

Re: Launching full Maven programmatically

Posted by Matthieu BROUILLARD <ma...@brouillard.fr>.
Hi,

If I remember well I think maven-flatten-plugin loads the model in a way
that even core-extensions are used; you should have a look there.

Matthieu

Re: Launching full Maven programmatically

Posted by Andres Almiray <aa...@gmail.com>.
Hello all,

I'm building a tool interface that required reading a fully parsed,
interpolated, Maven Model. It should work for all kinds of Maven projects,
not just trivial ones. For example, think reading Quarkus or Helidon, which
are comprised of several POM files, some of which whose parents are found
in non-standard locations (i.e, in a directory that's not the immediate
above, thus <relativePath> is used), some project properties are
interpolated by other plugins during the build (think the oss extension or
the buildnumber-maven-plugin as shown here
https://github.com/oracle/helidon/blob/master/pom.xml#L423-L438).

As such, the tool should be able to load a single MavenProject from a
lonely POM file (simple project) or a set of MavenProject instances when a
multi-project build (a Reactor?) is requested.

Cheers,
Andres

On Fri, Apr 17, 2020 at 11:42 AM Olivier Lamy <ol...@apache.org> wrote:

> yup but as I said it's only bug to fix but it's something which can
> be embedded. As I understand this is what Andres need.
> Most of the project can be loaded with it as long as long they are simple
> (i.e no core extension in .mvn etc...)
> maven-flatten-plugin will not really help as it's already a maven plugin so
> the mojo can access all the maven mechanism (components etc...) so you need
> to already start maven.
> We probably need to know more about the Andres's context
>
> On Fri, 17 Apr 2020 at 19:26, Matthieu BROUILLARD <ma...@brouillard.fr>
> wrote:
>
> > Sorry but it is fully in the subject of the thread.
> > Andres wants a way to programatically load a POM object. I guess he
> wants a
> > way to load a correctly resolved POM not a partially resolved one.
> >
> > On Fri, Apr 17, 2020 at 11:02 AM Olivier Lamy <ol...@apache.org> wrote:
> >
> > > yes maybe some stuff to fix (feel free to send a PR :P )
> > > But it's not the subject of the thread.... :)
> > > But did you try without all the Jenkins but only the mentioned library?
> > > It's not too hard to fix (the code pointed in this thread may have the
> > same
> > > issue)
> > >
> > >
> > > On Fri, 17 Apr 2020 at 18:49, Matthieu BROUILLARD <
> > matthieu@brouillard.fr>
> > > wrote:
> > >
> > > > Olivier,
> > > >
> > > > If the project you are pointing is what is behind "Maven Integration"
> > in
> > > > Jenkins then it does things wrong by not taking into account maven
> > > > core-extensions.
> > > >
> > > > I just retested now:
> > > > - download Jenkins.war 2.232
> > > > - launch as java -jar
> > > > - default installation
> > > > - add plugin "Maven integration"
> > > > - in settings add a local JDK 11
> > > > - in settings add a default maven 3.6.3
> > > > - create new job "jgitver in jenkins"
> > > > - set git url : https://github.com/jgitver/jgitver.git
> > > > - set goal : validate
> > > > - run the job
> > > >
> > > > You will see in the output that the version of the project is 0
> whereas
> > > it
> > > > should have been computed by a core extension.
> > > >
> > > > ...
> > > > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > > > >----------------------
> > > > [INFO] Building GIT versioning using jgit 0
> > > > [INFO] --------------------------------[ jar
> > > > ]---------------------------------
> > > > ...
> > > >
> > > >
> > > > If you do the same in CMD/sh you should see the automatic computation
> > > >
> > > > [INFO] Using jgitver configuration file:
> > > > C:\workspace\projects\oss\jgitver\jgitver-lib\.mvn\jgitver.config.xml
> > > > [INFO] Using jgitver-maven-plugin [1.5.1] (sha1:
> > > > e45d1669b39cedb98720dd33cc14d0185b455ca1)
> > > > [INFO]     version '0.12.1-SNAPSHOT' computed in 15595 ms
> > > > [INFO]
> > > > [INFO] Scanning for projects...
> > > > [INFO] jgitver-maven-plugin is about to change project(s) version(s)
> > > > [INFO]     fr.brouillard.oss::jgitver::0 -> 0.12.1-SNAPSHOT
> > > > [INFO]
> > > > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > > > >----------------------
> > > > [INFO] Building GIT versioning using jgit 0.12.1-SNAPSHOT
> > > > [INFO] --------------------------------[ jar
> > > > ]---------------------------------
> > > > ...
> > > >
> > > > This is not particular to jgitver but to any core extension (takari
> > > > polyglot, tycho pom less, ...).
> > > > The way this jenkins project loads the Maven Project Object Model is
> > > > somehow wrong or at least not complete.
> > > >
> > > > IMO the POM loading is better done in maven-flatten-plugin.
> > > >
> > > > Matthieu
> > > >
> > > > On Fri, Apr 17, 2020 at 6:00 AM Olivier Lamy <ol...@apache.org>
> wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > That's the way to do it
> > > > > There is a library to ease that here
> > > > >
> https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master
> > > > >
> > > > > cheers
> > > > > Olivier
> > > > >
> > > > >
> > > > > On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki
> <suztomo@google.com.invalid
> > >
> > > > > wrote:
> > > > >
> > > > > > I'm interested in that problem too. In the project below, we use
> > > > > > PlexusContainer to instantiate MavenProject class from pom file:
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
> > > > > >
> > > > > > I hope there's a better way to achieve this.
> > > > > >
> > > > > > On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <
> aalmiray@gmail.com
> > >
> > > > > wrote:
> > > > > >
> > > > > > > Hi,
> > > > > > >
> > > > > > > I'm looking for a way to launch Maven programmatically in such
> a
> > > way
> > > > > > that I
> > > > > > > can get access to fully resolved Model and MavenProject
> > instances,
> > > > and
> > > > > > not
> > > > > > > just for a single project but also for all projects that belong
> > to
> > > a
> > > > > > > Reactor.
> > > > > > >
> > > > > > > At first I used this code
> > > > > > >
> > > > > > > try {
> > > > > > >     FileReader reader = new FileReader(pom);
> > > > > > >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> > > > > > >     return new MavenProject(mavenReader.read(reader));
> > > > > > > } catch (Exception e) {
> > > > > > >     throw new IllegalArgumentException(e);
> > > > > > > }
> > > > > > >
> > > > > > > But of course that's not enough. Next I tried using the
> > > > > > maven-model-builder
> > > > > > > APIs directly but not all references get resolved (such as
> parent
> > > > POMs)
> > > > > > as
> > > > > > > a custom ModelResolver is required and the default one is not
> > > > intended
> > > > > to
> > > > > > > be used in standalone fashion.
> > > > > > >
> > > > > > > Ideally I would call some sort of embeddable Maven API that can
> > > give
> > > > me
> > > > > > the
> > > > > > > answers I seek, something like this
> > > > > > >
> > > > > > > MavenProject project =
> > > > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > > > >
> > > > > > > or alternatively something like
> > > > > > >
> > > > > > > MavenReactor reactor =
> > > > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > > > > for(MavenProject project : reactor.getProjects()) { /* do stuff
> > */
> > > }
> > > > > > >
> > > > > > > Of course I'm making up these types and method names as I do
> not
> > > know
> > > > > if
> > > > > > > something like this exist or not, hence why I'm asking here.
> > > > > > >
> > > > > > > Thanks in advance.
> > > > > > >
> > > > > > > Cheers
> > > > > > > Andres
> > > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Regards,
> > > > > > Tomo
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Olivier Lamy
> > > > > http://twitter.com/olamy | http://linkedin.com/in/olamy
> > > > >
> > > >
> > >
> > >
> > > --
> > > Olivier Lamy
> > > http://twitter.com/olamy | http://linkedin.com/in/olamy
> > >
> >
>
>
> --
> Olivier Lamy
> http://twitter.com/olamy | http://linkedin.com/in/olamy
>

Re: Launching full Maven programmatically

Posted by Olivier Lamy <ol...@apache.org>.
yup but as I said it's only bug to fix but it's something which can
be embedded. As I understand this is what Andres need.
Most of the project can be loaded with it as long as long they are simple
(i.e no core extension in .mvn etc...)
maven-flatten-plugin will not really help as it's already a maven plugin so
the mojo can access all the maven mechanism (components etc...) so you need
to already start maven.
We probably need to know more about the Andres's context

On Fri, 17 Apr 2020 at 19:26, Matthieu BROUILLARD <ma...@brouillard.fr>
wrote:

> Sorry but it is fully in the subject of the thread.
> Andres wants a way to programatically load a POM object. I guess he wants a
> way to load a correctly resolved POM not a partially resolved one.
>
> On Fri, Apr 17, 2020 at 11:02 AM Olivier Lamy <ol...@apache.org> wrote:
>
> > yes maybe some stuff to fix (feel free to send a PR :P )
> > But it's not the subject of the thread.... :)
> > But did you try without all the Jenkins but only the mentioned library?
> > It's not too hard to fix (the code pointed in this thread may have the
> same
> > issue)
> >
> >
> > On Fri, 17 Apr 2020 at 18:49, Matthieu BROUILLARD <
> matthieu@brouillard.fr>
> > wrote:
> >
> > > Olivier,
> > >
> > > If the project you are pointing is what is behind "Maven Integration"
> in
> > > Jenkins then it does things wrong by not taking into account maven
> > > core-extensions.
> > >
> > > I just retested now:
> > > - download Jenkins.war 2.232
> > > - launch as java -jar
> > > - default installation
> > > - add plugin "Maven integration"
> > > - in settings add a local JDK 11
> > > - in settings add a default maven 3.6.3
> > > - create new job "jgitver in jenkins"
> > > - set git url : https://github.com/jgitver/jgitver.git
> > > - set goal : validate
> > > - run the job
> > >
> > > You will see in the output that the version of the project is 0 whereas
> > it
> > > should have been computed by a core extension.
> > >
> > > ...
> > > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > > >----------------------
> > > [INFO] Building GIT versioning using jgit 0
> > > [INFO] --------------------------------[ jar
> > > ]---------------------------------
> > > ...
> > >
> > >
> > > If you do the same in CMD/sh you should see the automatic computation
> > >
> > > [INFO] Using jgitver configuration file:
> > > C:\workspace\projects\oss\jgitver\jgitver-lib\.mvn\jgitver.config.xml
> > > [INFO] Using jgitver-maven-plugin [1.5.1] (sha1:
> > > e45d1669b39cedb98720dd33cc14d0185b455ca1)
> > > [INFO]     version '0.12.1-SNAPSHOT' computed in 15595 ms
> > > [INFO]
> > > [INFO] Scanning for projects...
> > > [INFO] jgitver-maven-plugin is about to change project(s) version(s)
> > > [INFO]     fr.brouillard.oss::jgitver::0 -> 0.12.1-SNAPSHOT
> > > [INFO]
> > > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > > >----------------------
> > > [INFO] Building GIT versioning using jgit 0.12.1-SNAPSHOT
> > > [INFO] --------------------------------[ jar
> > > ]---------------------------------
> > > ...
> > >
> > > This is not particular to jgitver but to any core extension (takari
> > > polyglot, tycho pom less, ...).
> > > The way this jenkins project loads the Maven Project Object Model is
> > > somehow wrong or at least not complete.
> > >
> > > IMO the POM loading is better done in maven-flatten-plugin.
> > >
> > > Matthieu
> > >
> > > On Fri, Apr 17, 2020 at 6:00 AM Olivier Lamy <ol...@apache.org> wrote:
> > >
> > > > Hi,
> > > >
> > > > That's the way to do it
> > > > There is a library to ease that here
> > > > https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master
> > > >
> > > > cheers
> > > > Olivier
> > > >
> > > >
> > > > On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki <suztomo@google.com.invalid
> >
> > > > wrote:
> > > >
> > > > > I'm interested in that problem too. In the project below, we use
> > > > > PlexusContainer to instantiate MavenProject class from pom file:
> > > > >
> > > > >
> > > >
> > >
> >
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
> > > > >
> > > > > I hope there's a better way to achieve this.
> > > > >
> > > > > On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aalmiray@gmail.com
> >
> > > > wrote:
> > > > >
> > > > > > Hi,
> > > > > >
> > > > > > I'm looking for a way to launch Maven programmatically in such a
> > way
> > > > > that I
> > > > > > can get access to fully resolved Model and MavenProject
> instances,
> > > and
> > > > > not
> > > > > > just for a single project but also for all projects that belong
> to
> > a
> > > > > > Reactor.
> > > > > >
> > > > > > At first I used this code
> > > > > >
> > > > > > try {
> > > > > >     FileReader reader = new FileReader(pom);
> > > > > >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> > > > > >     return new MavenProject(mavenReader.read(reader));
> > > > > > } catch (Exception e) {
> > > > > >     throw new IllegalArgumentException(e);
> > > > > > }
> > > > > >
> > > > > > But of course that's not enough. Next I tried using the
> > > > > maven-model-builder
> > > > > > APIs directly but not all references get resolved (such as parent
> > > POMs)
> > > > > as
> > > > > > a custom ModelResolver is required and the default one is not
> > > intended
> > > > to
> > > > > > be used in standalone fashion.
> > > > > >
> > > > > > Ideally I would call some sort of embeddable Maven API that can
> > give
> > > me
> > > > > the
> > > > > > answers I seek, something like this
> > > > > >
> > > > > > MavenProject project =
> > > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > > >
> > > > > > or alternatively something like
> > > > > >
> > > > > > MavenReactor reactor =
> > > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > > > for(MavenProject project : reactor.getProjects()) { /* do stuff
> */
> > }
> > > > > >
> > > > > > Of course I'm making up these types and method names as I do not
> > know
> > > > if
> > > > > > something like this exist or not, hence why I'm asking here.
> > > > > >
> > > > > > Thanks in advance.
> > > > > >
> > > > > > Cheers
> > > > > > Andres
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Regards,
> > > > > Tomo
> > > > >
> > > >
> > > >
> > > > --
> > > > Olivier Lamy
> > > > http://twitter.com/olamy | http://linkedin.com/in/olamy
> > > >
> > >
> >
> >
> > --
> > Olivier Lamy
> > http://twitter.com/olamy | http://linkedin.com/in/olamy
> >
>


-- 
Olivier Lamy
http://twitter.com/olamy | http://linkedin.com/in/olamy

Re: Launching full Maven programmatically

Posted by Matthieu BROUILLARD <ma...@brouillard.fr>.
Sorry but it is fully in the subject of the thread.
Andres wants a way to programatically load a POM object. I guess he wants a
way to load a correctly resolved POM not a partially resolved one.

On Fri, Apr 17, 2020 at 11:02 AM Olivier Lamy <ol...@apache.org> wrote:

> yes maybe some stuff to fix (feel free to send a PR :P )
> But it's not the subject of the thread.... :)
> But did you try without all the Jenkins but only the mentioned library?
> It's not too hard to fix (the code pointed in this thread may have the same
> issue)
>
>
> On Fri, 17 Apr 2020 at 18:49, Matthieu BROUILLARD <ma...@brouillard.fr>
> wrote:
>
> > Olivier,
> >
> > If the project you are pointing is what is behind "Maven Integration" in
> > Jenkins then it does things wrong by not taking into account maven
> > core-extensions.
> >
> > I just retested now:
> > - download Jenkins.war 2.232
> > - launch as java -jar
> > - default installation
> > - add plugin "Maven integration"
> > - in settings add a local JDK 11
> > - in settings add a default maven 3.6.3
> > - create new job "jgitver in jenkins"
> > - set git url : https://github.com/jgitver/jgitver.git
> > - set goal : validate
> > - run the job
> >
> > You will see in the output that the version of the project is 0 whereas
> it
> > should have been computed by a core extension.
> >
> > ...
> > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > >----------------------
> > [INFO] Building GIT versioning using jgit 0
> > [INFO] --------------------------------[ jar
> > ]---------------------------------
> > ...
> >
> >
> > If you do the same in CMD/sh you should see the automatic computation
> >
> > [INFO] Using jgitver configuration file:
> > C:\workspace\projects\oss\jgitver\jgitver-lib\.mvn\jgitver.config.xml
> > [INFO] Using jgitver-maven-plugin [1.5.1] (sha1:
> > e45d1669b39cedb98720dd33cc14d0185b455ca1)
> > [INFO]     version '0.12.1-SNAPSHOT' computed in 15595 ms
> > [INFO]
> > [INFO] Scanning for projects...
> > [INFO] jgitver-maven-plugin is about to change project(s) version(s)
> > [INFO]     fr.brouillard.oss::jgitver::0 -> 0.12.1-SNAPSHOT
> > [INFO]
> > [INFO] ---------------------< fr.brouillard.oss:jgitver
> > >----------------------
> > [INFO] Building GIT versioning using jgit 0.12.1-SNAPSHOT
> > [INFO] --------------------------------[ jar
> > ]---------------------------------
> > ...
> >
> > This is not particular to jgitver but to any core extension (takari
> > polyglot, tycho pom less, ...).
> > The way this jenkins project loads the Maven Project Object Model is
> > somehow wrong or at least not complete.
> >
> > IMO the POM loading is better done in maven-flatten-plugin.
> >
> > Matthieu
> >
> > On Fri, Apr 17, 2020 at 6:00 AM Olivier Lamy <ol...@apache.org> wrote:
> >
> > > Hi,
> > >
> > > That's the way to do it
> > > There is a library to ease that here
> > > https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master
> > >
> > > cheers
> > > Olivier
> > >
> > >
> > > On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki <su...@google.com.invalid>
> > > wrote:
> > >
> > > > I'm interested in that problem too. In the project below, we use
> > > > PlexusContainer to instantiate MavenProject class from pom file:
> > > >
> > > >
> > >
> >
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
> > > >
> > > > I hope there's a better way to achieve this.
> > > >
> > > > On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aa...@gmail.com>
> > > wrote:
> > > >
> > > > > Hi,
> > > > >
> > > > > I'm looking for a way to launch Maven programmatically in such a
> way
> > > > that I
> > > > > can get access to fully resolved Model and MavenProject instances,
> > and
> > > > not
> > > > > just for a single project but also for all projects that belong to
> a
> > > > > Reactor.
> > > > >
> > > > > At first I used this code
> > > > >
> > > > > try {
> > > > >     FileReader reader = new FileReader(pom);
> > > > >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> > > > >     return new MavenProject(mavenReader.read(reader));
> > > > > } catch (Exception e) {
> > > > >     throw new IllegalArgumentException(e);
> > > > > }
> > > > >
> > > > > But of course that's not enough. Next I tried using the
> > > > maven-model-builder
> > > > > APIs directly but not all references get resolved (such as parent
> > POMs)
> > > > as
> > > > > a custom ModelResolver is required and the default one is not
> > intended
> > > to
> > > > > be used in standalone fashion.
> > > > >
> > > > > Ideally I would call some sort of embeddable Maven API that can
> give
> > me
> > > > the
> > > > > answers I seek, something like this
> > > > >
> > > > > MavenProject project =
> > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > >
> > > > > or alternatively something like
> > > > >
> > > > > MavenReactor reactor =
> > > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > > for(MavenProject project : reactor.getProjects()) { /* do stuff */
> }
> > > > >
> > > > > Of course I'm making up these types and method names as I do not
> know
> > > if
> > > > > something like this exist or not, hence why I'm asking here.
> > > > >
> > > > > Thanks in advance.
> > > > >
> > > > > Cheers
> > > > > Andres
> > > > >
> > > >
> > > >
> > > > --
> > > > Regards,
> > > > Tomo
> > > >
> > >
> > >
> > > --
> > > Olivier Lamy
> > > http://twitter.com/olamy | http://linkedin.com/in/olamy
> > >
> >
>
>
> --
> Olivier Lamy
> http://twitter.com/olamy | http://linkedin.com/in/olamy
>

Re: Launching full Maven programmatically

Posted by Olivier Lamy <ol...@apache.org>.
yes maybe some stuff to fix (feel free to send a PR :P )
But it's not the subject of the thread.... :)
But did you try without all the Jenkins but only the mentioned library?
It's not too hard to fix (the code pointed in this thread may have the same
issue)


On Fri, 17 Apr 2020 at 18:49, Matthieu BROUILLARD <ma...@brouillard.fr>
wrote:

> Olivier,
>
> If the project you are pointing is what is behind "Maven Integration" in
> Jenkins then it does things wrong by not taking into account maven
> core-extensions.
>
> I just retested now:
> - download Jenkins.war 2.232
> - launch as java -jar
> - default installation
> - add plugin "Maven integration"
> - in settings add a local JDK 11
> - in settings add a default maven 3.6.3
> - create new job "jgitver in jenkins"
> - set git url : https://github.com/jgitver/jgitver.git
> - set goal : validate
> - run the job
>
> You will see in the output that the version of the project is 0 whereas it
> should have been computed by a core extension.
>
> ...
> [INFO] ---------------------< fr.brouillard.oss:jgitver
> >----------------------
> [INFO] Building GIT versioning using jgit 0
> [INFO] --------------------------------[ jar
> ]---------------------------------
> ...
>
>
> If you do the same in CMD/sh you should see the automatic computation
>
> [INFO] Using jgitver configuration file:
> C:\workspace\projects\oss\jgitver\jgitver-lib\.mvn\jgitver.config.xml
> [INFO] Using jgitver-maven-plugin [1.5.1] (sha1:
> e45d1669b39cedb98720dd33cc14d0185b455ca1)
> [INFO]     version '0.12.1-SNAPSHOT' computed in 15595 ms
> [INFO]
> [INFO] Scanning for projects...
> [INFO] jgitver-maven-plugin is about to change project(s) version(s)
> [INFO]     fr.brouillard.oss::jgitver::0 -> 0.12.1-SNAPSHOT
> [INFO]
> [INFO] ---------------------< fr.brouillard.oss:jgitver
> >----------------------
> [INFO] Building GIT versioning using jgit 0.12.1-SNAPSHOT
> [INFO] --------------------------------[ jar
> ]---------------------------------
> ...
>
> This is not particular to jgitver but to any core extension (takari
> polyglot, tycho pom less, ...).
> The way this jenkins project loads the Maven Project Object Model is
> somehow wrong or at least not complete.
>
> IMO the POM loading is better done in maven-flatten-plugin.
>
> Matthieu
>
> On Fri, Apr 17, 2020 at 6:00 AM Olivier Lamy <ol...@apache.org> wrote:
>
> > Hi,
> >
> > That's the way to do it
> > There is a library to ease that here
> > https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master
> >
> > cheers
> > Olivier
> >
> >
> > On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki <su...@google.com.invalid>
> > wrote:
> >
> > > I'm interested in that problem too. In the project below, we use
> > > PlexusContainer to instantiate MavenProject class from pom file:
> > >
> > >
> >
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
> > >
> > > I hope there's a better way to achieve this.
> > >
> > > On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aa...@gmail.com>
> > wrote:
> > >
> > > > Hi,
> > > >
> > > > I'm looking for a way to launch Maven programmatically in such a way
> > > that I
> > > > can get access to fully resolved Model and MavenProject instances,
> and
> > > not
> > > > just for a single project but also for all projects that belong to a
> > > > Reactor.
> > > >
> > > > At first I used this code
> > > >
> > > > try {
> > > >     FileReader reader = new FileReader(pom);
> > > >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> > > >     return new MavenProject(mavenReader.read(reader));
> > > > } catch (Exception e) {
> > > >     throw new IllegalArgumentException(e);
> > > > }
> > > >
> > > > But of course that's not enough. Next I tried using the
> > > maven-model-builder
> > > > APIs directly but not all references get resolved (such as parent
> POMs)
> > > as
> > > > a custom ModelResolver is required and the default one is not
> intended
> > to
> > > > be used in standalone fashion.
> > > >
> > > > Ideally I would call some sort of embeddable Maven API that can give
> me
> > > the
> > > > answers I seek, something like this
> > > >
> > > > MavenProject project =
> > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > >
> > > > or alternatively something like
> > > >
> > > > MavenReactor reactor =
> > > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > > for(MavenProject project : reactor.getProjects()) { /* do stuff */ }
> > > >
> > > > Of course I'm making up these types and method names as I do not know
> > if
> > > > something like this exist or not, hence why I'm asking here.
> > > >
> > > > Thanks in advance.
> > > >
> > > > Cheers
> > > > Andres
> > > >
> > >
> > >
> > > --
> > > Regards,
> > > Tomo
> > >
> >
> >
> > --
> > Olivier Lamy
> > http://twitter.com/olamy | http://linkedin.com/in/olamy
> >
>


-- 
Olivier Lamy
http://twitter.com/olamy | http://linkedin.com/in/olamy

Re: Launching full Maven programmatically

Posted by Matthieu BROUILLARD <ma...@brouillard.fr>.
Olivier,

If the project you are pointing is what is behind "Maven Integration" in
Jenkins then it does things wrong by not taking into account maven
core-extensions.

I just retested now:
- download Jenkins.war 2.232
- launch as java -jar
- default installation
- add plugin "Maven integration"
- in settings add a local JDK 11
- in settings add a default maven 3.6.3
- create new job "jgitver in jenkins"
- set git url : https://github.com/jgitver/jgitver.git
- set goal : validate
- run the job

You will see in the output that the version of the project is 0 whereas it
should have been computed by a core extension.

...
[INFO] ---------------------< fr.brouillard.oss:jgitver >----------------------
[INFO] Building GIT versioning using jgit 0
[INFO] --------------------------------[ jar ]---------------------------------
...


If you do the same in CMD/sh you should see the automatic computation

[INFO] Using jgitver configuration file:
C:\workspace\projects\oss\jgitver\jgitver-lib\.mvn\jgitver.config.xml
[INFO] Using jgitver-maven-plugin [1.5.1] (sha1:
e45d1669b39cedb98720dd33cc14d0185b455ca1)
[INFO]     version '0.12.1-SNAPSHOT' computed in 15595 ms
[INFO]
[INFO] Scanning for projects...
[INFO] jgitver-maven-plugin is about to change project(s) version(s)
[INFO]     fr.brouillard.oss::jgitver::0 -> 0.12.1-SNAPSHOT
[INFO]
[INFO] ---------------------< fr.brouillard.oss:jgitver
>----------------------
[INFO] Building GIT versioning using jgit 0.12.1-SNAPSHOT
[INFO] --------------------------------[ jar
]---------------------------------
...

This is not particular to jgitver but to any core extension (takari
polyglot, tycho pom less, ...).
The way this jenkins project loads the Maven Project Object Model is
somehow wrong or at least not complete.

IMO the POM loading is better done in maven-flatten-plugin.

Matthieu

On Fri, Apr 17, 2020 at 6:00 AM Olivier Lamy <ol...@apache.org> wrote:

> Hi,
>
> That's the way to do it
> There is a library to ease that here
> https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master
>
> cheers
> Olivier
>
>
> On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki <su...@google.com.invalid>
> wrote:
>
> > I'm interested in that problem too. In the project below, we use
> > PlexusContainer to instantiate MavenProject class from pom file:
> >
> >
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
> >
> > I hope there's a better way to achieve this.
> >
> > On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aa...@gmail.com>
> wrote:
> >
> > > Hi,
> > >
> > > I'm looking for a way to launch Maven programmatically in such a way
> > that I
> > > can get access to fully resolved Model and MavenProject instances, and
> > not
> > > just for a single project but also for all projects that belong to a
> > > Reactor.
> > >
> > > At first I used this code
> > >
> > > try {
> > >     FileReader reader = new FileReader(pom);
> > >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> > >     return new MavenProject(mavenReader.read(reader));
> > > } catch (Exception e) {
> > >     throw new IllegalArgumentException(e);
> > > }
> > >
> > > But of course that's not enough. Next I tried using the
> > maven-model-builder
> > > APIs directly but not all references get resolved (such as parent POMs)
> > as
> > > a custom ModelResolver is required and the default one is not intended
> to
> > > be used in standalone fashion.
> > >
> > > Ideally I would call some sort of embeddable Maven API that can give me
> > the
> > > answers I seek, something like this
> > >
> > > MavenProject project =
> > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > >
> > > or alternatively something like
> > >
> > > MavenReactor reactor =
> > EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > > for(MavenProject project : reactor.getProjects()) { /* do stuff */ }
> > >
> > > Of course I'm making up these types and method names as I do not know
> if
> > > something like this exist or not, hence why I'm asking here.
> > >
> > > Thanks in advance.
> > >
> > > Cheers
> > > Andres
> > >
> >
> >
> > --
> > Regards,
> > Tomo
> >
>
>
> --
> Olivier Lamy
> http://twitter.com/olamy | http://linkedin.com/in/olamy
>

Re: Launching full Maven programmatically

Posted by Olivier Lamy <ol...@apache.org>.
Hi,

That's the way to do it
There is a library to ease that here
https://github.com/jenkinsci/lib-jenkins-maven-embedder/tree/master

cheers
Olivier


On Fri, 17 Apr 2020 at 13:04, Tomo Suzuki <su...@google.com.invalid>
wrote:

> I'm interested in that problem too. In the project below, we use
> PlexusContainer to instantiate MavenProject class from pom file:
>
> https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209
>
> I hope there's a better way to achieve this.
>
> On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aa...@gmail.com> wrote:
>
> > Hi,
> >
> > I'm looking for a way to launch Maven programmatically in such a way
> that I
> > can get access to fully resolved Model and MavenProject instances, and
> not
> > just for a single project but also for all projects that belong to a
> > Reactor.
> >
> > At first I used this code
> >
> > try {
> >     FileReader reader = new FileReader(pom);
> >     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
> >     return new MavenProject(mavenReader.read(reader));
> > } catch (Exception e) {
> >     throw new IllegalArgumentException(e);
> > }
> >
> > But of course that's not enough. Next I tried using the
> maven-model-builder
> > APIs directly but not all references get resolved (such as parent POMs)
> as
> > a custom ModelResolver is required and the default one is not intended to
> > be used in standalone fashion.
> >
> > Ideally I would call some sort of embeddable Maven API that can give me
> the
> > answers I seek, something like this
> >
> > MavenProject project =
> EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> >
> > or alternatively something like
> >
> > MavenReactor reactor =
> EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> > for(MavenProject project : reactor.getProjects()) { /* do stuff */ }
> >
> > Of course I'm making up these types and method names as I do not know if
> > something like this exist or not, hence why I'm asking here.
> >
> > Thanks in advance.
> >
> > Cheers
> > Andres
> >
>
>
> --
> Regards,
> Tomo
>


-- 
Olivier Lamy
http://twitter.com/olamy | http://linkedin.com/in/olamy

Re: Launching full Maven programmatically

Posted by Tomo Suzuki <su...@google.com.INVALID>.
I'm interested in that problem too. In the project below, we use
PlexusContainer to instantiate MavenProject class from pom file:
https://github.com/GoogleCloudPlatform/cloud-opensource-java/blob/master/dependencies/src/main/java/com/google/cloud/tools/opensource/dependencies/RepositoryUtility.java#L209

I hope there's a better way to achieve this.

On Thu, Apr 16, 2020 at 6:55 AM Andres Almiray <aa...@gmail.com> wrote:

> Hi,
>
> I'm looking for a way to launch Maven programmatically in such a way that I
> can get access to fully resolved Model and MavenProject instances, and not
> just for a single project but also for all projects that belong to a
> Reactor.
>
> At first I used this code
>
> try {
>     FileReader reader = new FileReader(pom);
>     MavenXpp3Reader mavenReader = new MavenXpp3Reader();
>     return new MavenProject(mavenReader.read(reader));
> } catch (Exception e) {
>     throw new IllegalArgumentException(e);
> }
>
> But of course that's not enough. Next I tried using the maven-model-builder
> APIs directly but not all references get resolved (such as parent POMs) as
> a custom ModelResolver is required and the default one is not intended to
> be used in standalone fashion.
>
> Ideally I would call some sort of embeddable Maven API that can give me the
> answers I seek, something like this
>
> MavenProject project = EmbeddableMaven.launch(propsOrFlags).file(pomFile);
>
> or alternatively something like
>
> MavenReactor reactor = EmbeddableMaven.launch(propsOrFlags).file(pomFile);
> for(MavenProject project : reactor.getProjects()) { /* do stuff */ }
>
> Of course I'm making up these types and method names as I do not know if
> something like this exist or not, hence why I'm asking here.
>
> Thanks in advance.
>
> Cheers
> Andres
>


-- 
Regards,
Tomo