1

Is this code

project.getPluginRepositories().add(myCustomRepository); 

executed inside afterProjectsRead method of a maven extension (class extending AbstractMavenLifecycleParticipant) supposed to work? It executes fine (no errors nor warnings) but the repo does not seem to be taken into account by Maven and build fails with "Plugin .... or one of its dependencies could not be resolved"!

If this is not possible this way, are there any other ways to dynamically add a repo from maven extension?

Milen Dyankov
  • 2,972
  • 14
  • 25
  • No it can't cause it's not intended to change the `settings.xml` based configuration...Why not changing the `settings.xml` ? What is the real problem? – khmarbaise May 26 '17 at 16:48
  • This has nothing to do with `settings.xml`. It is expected to behave like if `...` was added to the pom. – Milen Dyankov May 26 '17 at 17:10
  • Which is nothing different than that. The `settings.xml` defines where everything is consumed from. So usually you shouldn't change that. Can you explain more what kind of problem you are trying to resolve? – khmarbaise May 26 '17 at 17:20
  • I need to be able to generate url and dynamically add custom repo based on some information that is available outside maven and may change anytime. – Milen Dyankov May 26 '17 at 17:40
  • Why custom repos dynamically ? and changing? Sounds like it would be easier to create a template of the settings.xml and do so template processing with the informations... – khmarbaise May 26 '17 at 17:45
  • Well it's a bit complicated and unusual case. The extention adds or reconfigures plugins depending on some project specific properties. Some of those plugins are available in own repositories so those need to be added as well for the plugins to discovered and downloaded. Of course I can ask my users to do that manually but they'd have to know the internal logic of the extension or add upfront all potentially needed repos. I'm trying to avoid that. – Milen Dyankov May 26 '17 at 18:15
  • An extension is changing the plugins configuration? Sounds really weird...sorry to say that...Having repositories in different repositories is against all best practices...the plugins/artifacts are separated by their gav so no need for that...Another time. What kind of problem are you trying to solve? Why are the plugins not configured correctly in the pom file? – khmarbaise May 27 '17 at 10:13
  • As I said, it's not an usual usecase. It's too complex to be explained here. You can think of it as something that simplifies POM files significantly and makes Maven more flexible. What I don't get is why an extension can add/remove/modify any plugin but can not do the same with plugin repository. – Milen Dyankov May 27 '17 at 11:14
  • If you are saying it is not an usual use case you should explain it otherwise I can't help with best practices..The question is if there exist other/better solutions for your problem already but without knowing what your problem is I can't help...simplifying pom's often lacks from not using defaults and using corporate pom's etc. Or trying to do many things in a single module (better separation of concerns) etc. I can only speculate. It would helpful if you could make an simplified example project on github so I can take a look... – khmarbaise May 27 '17 at 13:45
  • @khmarbaise I figured out a way to do it. Probably going against all Maven best practices :) Still as I said this is unusual case and it's only a PoC. I liked the project where I use it (for only one repo for now). If you know of any better way, please let me know. – Milen Dyankov Jun 14 '17 at 09:21
  • https://issues.apache.org/jira/browse/MNG-6327 – caduceus Jun 25 '20 at 08:38

2 Answers2

1

Another way is to listen as an EventSpy then inject project level settings, within what we can define custom repos.

For example,

in ${basedir}/.mvn/extensions.xml

    <extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
      <extension>
        <groupId>com.github.gzm55.maven</groupId>
        <artifactId>project-settings-extension</artifactId>
        <version>0.1.1</version>
      </extension>
    </extensions>

in ${basedir}/.mvn/settings.xml

  <settings>

    <mirrors>...</mirrors>

    <profiles>
      <profile>
        <repositories/>
      </profile>
    </profiles>

  </settings>
James Z.M. Gao
  • 516
  • 1
  • 8
  • 13
  • Your method works for reactor projects, but not other extensions. Im trying to get a maven-buildtime-extension to use the aopalliance artefact from my repository. Unfortunately the extension ignores the repositories defined in .m2/settings.xml because of https://issues.apache.org/jira/browse/MNG-6327 – caduceus Jun 25 '20 at 08:43
  • yes, it is a known issue (https://github.com/gzm55/project-settings-maven-extension#known-issue) as a extension. An ultimate solution is move this extension into maven core (https://issues.apache.org/jira/browse/MNG-6392). A work around may be put this extension jar file into $M2_HOME/lib/ext dir, then the extension will be take effect before resolving the project core-extensions. – James Z.M. Gao Jun 25 '20 at 11:15
  • i copied project-settings-extension-0.1.1.jar to .m2/lib/ext, but the maven-buildtime-extension still does not utilize the repository defined in the activated profile in ${base.dir}/.mvn/settings.xml – caduceus Jun 25 '20 at 13:24
  • not .m2/ dir, $M2_HOME means the location where maven is installed, e.g., $M2_HOME/lib/ext --> /opt/maven/lib/ext – James Z.M. Gao Jun 25 '20 at 14:17
  • i see, well I cant do that – caduceus Jun 25 '20 at 15:57
0

If this is not possible this way, are there any other ways to dynamically add a repo from maven extension?

It seams this code

List<ArtifactRepository> pluginRepos = new LinkedList<>();
pluginRepos.addAll(project.getPluginArtifactRepositories());
pluginRepos.add(myCustomRepository);
project.setPluginArtifactRepositories(pluginRepos);

works. Here is an example from ExecutionListener rather than MavenLifecycleParticipant but I guess it should work in afterProjectsRead too.

WARNING: This is probably not what Maven expects you to do and may break some plugins. For example maven-shade-plugin works mostly OK but breaks (and fails the build) while trying to generate reduced POM when repositories are added this way.

Milen Dyankov
  • 2,972
  • 14
  • 25