Skip to content

[KARAF-7667] Support Maven split local repository in karaf-maven-plugin - #2979

Open
ponziani wants to merge 1 commit into
apache:mainfrom
ponziani:karaf-maven-plugin-split-local-repository
Open

ponziani wants to merge 1 commit into
apache:mainfrom
ponziani:karaf-maven-plugin-split-local-repository

Conversation

@ponziani

@ponziani ponziani commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes KARAF-7667.

Problem

Since Maven 3.9 the local repository can be split into installed and cached artifacts, and further into releases and snapshots or by remote repository (docs). Maven Resolver 2.x (Maven 3.10, Maven 4) uses the aether.lrm.enhanced.* properties for this and still reads the older aether.enhancedLocalRepository.* names.

The assembly and verify goals resolve artifacts through the MavenResolverFactory provider (pax-url), which only gets the plain local repository directory. With a split local repository the plugin cannot find the artifacts installed by an earlier build, typically SNAPSHOTs under installed/snapshots/..., and the build fails with "Could not find artifact".

Fix

ReactorMavenResolver, which both goals already use, now asks Maven where the artifact is before it delegates to the provider:

  1. the reactor (as before);
  2. LocalRepositoryManager#find(...) of the Maven session, with the project's remote repositories and the project context;
  3. the MavenResolverFactory provider.

Maven's own local repository manager does the lookup, so every layout works: split with either set of property names, custom prefixes, maven.repo.local.tail. The plugin does not depend on pax-url for this, and no provider-specific syntax goes through the SPI.

Step 2 is only done with the enhanced local repository (the Maven default), because only that one records where an artifact comes from. It takes:

  • artifacts installed by a local build;
  • release artifacts cached from one of the project repositories;
  • SNAPSHOT artifacts cached from one of the project repositories, only when Maven runs offline.

Everything else goes to the provider as before: cached SNAPSHOTs when online (so its update policy applies), version ranges, LATEST/RELEASE, URLs that name their own repository (mvn:http://...!g/a/v), and every artifact with the simple local repository (-llr).

Behaviour change

This applies to every build with the enhanced local repository, not only to split builds:

  • A locally installed SNAPSHOT takes precedence over a newer remote SNAPSHOT of the same version, the same as the reactor does for the modules being built. Before, pax-url could choose the remote one.
  • A cached release is taken from the local repository without going through the provider.

Tests

  • ReactorMavenResolverLocalRepositoryTest uses Maven Resolver's real EnhancedLocalRepositoryManager (test-scoped maven-resolver-impl). It covers a SNAPSHOT installed with aether.lrm.enhanced.split/splitLocal, the same with the old aether.enhancedLocalRepository.* names, custom prefixes, cached releases (also from a repository manager, which tracks the context), a cached SNAPSHOT online and offline, and version ranges and LATEST/RELEASE, which have no file of their own and go to the provider. It uses org.eclipse.aether.internal.impl constructors because that is the only way to get the real manager without a full repository system. A Resolver upgrade that changes them shows at compile time.
  • ReactorMavenResolverTest covers the cases the real manager cannot easily produce: the request it gets (repositories and context), an unavailable or missing file, -llr, URLs with a repository, and reactor precedence.
  • Manual end-to-end check with Maven 3.10.1 (Resolver 2.0.25), -Daether.lrm.enhanced.split=true -Daether.lrm.enhanced.splitLocal=true and an empty local repository: a bundle 1.0-SNAPSHOT is installed (into installed/snapshots/...), then a karaf-assembly project lists it in startupBundles.
    • with karaf-maven-plugin 4.4.11: Could not find artifact org.example.e2e:e2e-lib:jar:1.0-SNAPSHOT;
    • with this change: BUILD SUCCESS, and the bundle is in system/ and etc/startup.properties.

The verify goal uses the same resolver wrapper and was not run end-to-end.

Not in this PR

The Builder offline key (pid + "offline", missing dot) and the missing maven.offline in VerifyMojo change every mvn -o assembly build. They will go into a separate JIRA and PR with an offline assembly IT and a release note.

Backport

The main code only uses Maven Resolver API that also exists in 1.x (LocalRepositoryManager#find, LocalArtifactResult), so it should apply to karaf-4.4.x. The real-manager test uses Resolver 2.x internals and would need adapting there.

🤖 Generated with Claude Code

@jbonofre jbonofre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

Nit: the split local repo handling in MojoSupport assumes the pax-url option names and defaults match Maven's aether.enhancedLocalRepository.* semantics. A pax-url upgrade could change that silently. The new test covers only split plus splitLocal, so a test for the prefix options would help later.

@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Test Results

  747 files  +3    747 suites  +3   1h 7m 4s ⏱️ + 4m 20s
1 111 tests +3    977 ✅ +3  134 💤 ±0  0 ❌ ±0 
3 333 runs  +9  2 930 ✅ +9  403 💤 ±0  0 ❌ ±0 

Results for commit 02f22a6. ± Comparison against base commit 747f63d.

♻️ This comment has been updated with latest results.

* Prefix of the Maven resolver (1.9+) configuration properties for the split local repository.
*/
private static final String ENHANCED_LOCAL_REPOSITORY = "aether.enhancedLocalRepository.";

@CptBartender CptBartender Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider extracting the 2d arrays as static constants

Suggested change
private static final String[][] OPTIONS = {
{ ENHANCED_LOCAL_REPOSITORY + "split", ServiceConstants.OPTION_SPLIT },
{ ENHANCED_LOCAL_REPOSITORY + "splitLocal", ServiceConstants.OPTION_SPLIT_LOCAL },
{ ENHANCED_LOCAL_REPOSITORY + "splitRemote", ServiceConstants.OPTION_SPLIT_REMOTE },
{ ENHANCED_LOCAL_REPOSITORY + "splitRemoteRepository", ServiceConstants.OPTION_SPLIT_REMOTE_REPOSITORY },
{ ENHANCED_LOCAL_REPOSITORY + "splitRemoteRepositoryLast", ServiceConstants.OPTION_SPLIT_REMOTE_REPOSITORY_LAST }
};
private static final String[][] PREFIXES = {
{ ENHANCED_LOCAL_REPOSITORY + "localPrefix", ServiceConstants.OPTION_SPLIT_LOCAL_PREFIX },
{ ENHANCED_LOCAL_REPOSITORY + "remotePrefix", ServiceConstants.OPTION_SPLIT_REMOTE_PREFIX },
{ ENHANCED_LOCAL_REPOSITORY + "releasesPrefix", ServiceConstants.OPTION_SPLIT_RELEASES_PREFIX },
{ ENHANCED_LOCAL_REPOSITORY + "snapshotsPrefix", ServiceConstants.OPTION_SPLIT_SNAPSHOTS_PREFIX }
};

With this, we wouldn't have to concat the strings each time #getPaxUrlLocalRepository is called.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @CptBartender. Agreed, extracting the tables to static constants makes getPaxUrlLocalRepository easier to read.

@ponziani you call if you wanna apply it? One caveat: the suggested keys already include ENHANCED_LOCAL_REPOSITORY, so the loops need to look them up directly with configProperties.get(option[0]) and configProperties.get(prefix[0]). Otherwise the prefix gets applied twice and no option matches. testLocalRepositoryWithSplit should catch that if it's missed.

Since the order of the generated options (@split@splitLocal@...) is asserted in the tests, please keep the declaration order. If you would rather not expose mutable arrays, an ordered immutable structure such as a List of pairs or a LinkedHashMap wrapped in Collections.unmodifiableMap works too.

Also, the test already covers splitLocalPrefix and splitSnapshotsPrefix, so only remotePrefix and releasePrefix are missing. Adding those to testLocalRepositoryWithSplit while you are in there would close that gap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CptBartender, applied: the options and prefixes are now static constants holding the full property keys, in the same order. Thanks @jbonofre for spotting the double-prefix catch. I also added remotePrefix and releasesPrefix to testLocalRepositoryWithSplit, so all four prefixes are covered now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@ponziani
ponziani force-pushed the karaf-maven-plugin-split-local-repository branch from a8e49e1 to a96394b Compare November 4, 2026 06:49
@ponziani

ponziani commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main after #2928. The only conflict was the offline flag in Builder#createMavenResolver(). It now uses the factory PID with the missing dot fixed (pid + ".offline"). The split-local-repository handling stays in MojoSupport and builds the Pax URL localRepository option string. That's still correct, because the plugin uses the Pax URL resolver provider. If you'd rather move it into maven/resolver-paxurl or behind the SPI, I'm happy to do that in a follow-up. MojoSupportTest and the profile tests pass locally. @jbonofre @CptBartender, could you take another look?

It would be nice if this makes into the 4.5 release.

import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.settings.Proxy;
import org.codehaus.plexus.PlexusContainer;
import org.ops4j.pax.url.mvn.ServiceConstants;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You offered to move this behind the SPI in a follow-up. I'd rather sort it out in this PR. Since #2928, the plugin and Builder don't import anything from org.ops4j. They only talk to the resolver through org.apache.karaf.features.spi.MavenResolverFactory, so pax-url can be replaced by another provider. This import brings pax-url back into the plugin. The @id=local@split... value is also pax-url-specific syntax passed through that neutral interface, so any other provider would read it as a literal directory. The new test likewise builds its resolver with pax-url's MavenResolvers instead of org.apache.karaf.features.spi.MavenResolvers.factory().

The ReactorMavenResolver approach from my comment on ENHANCED_LOCAL_REPOSITORY avoids this, since the lookup goes through Maven's own session and needs nothing from the provider. It would also keep the 4.4.x backport the same shape, since that branch has no resolver-paxurl module. If you'd rather keep the configuration approach, please move the pax-url mapping into PaxUrlMavenResolverFactory and give it provider-neutral settings.

/**
* Prefix of the Maven resolver (1.9+) configuration properties for the split local repository.
*/
private static final String ENHANCED_LOCAL_REPOSITORY = "aether.enhancedLocalRepository.";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only reads the aether.enhancedLocalRepository.* names. Maven Resolver 2.x (used by Maven 3.10, which Karaf itself builds against, and by Maven 4) documents aether.lrm.enhanced.* instead and keeps the old names only as fallbacks. Resolver 2.0.24's LocalPathPrefixComposerFactorySupport reads both sets.

So with -Daether.lrm.enhanced.split=true -Daether.lrm.enhanced.splitLocal=true, Maven installs the SNAPSHOT under installed/snapshots/..., but this method returns the plain directory. Assembly and verify then still fail with "Could not find artifact" (reproduced with pax-url 3.0.3). KARAF-7667 stays open for anyone following the current Maven docs.

Rather than copying Maven's settings into pax-url, could ReactorMavenResolver ask Maven where the artifact is first? mavenSession.getRepositorySession().getLocalRepositoryManager().find(...), called with the project's remote repositories and using the file when isAvailable(), works whatever layout Maven is using: split with either key set, custom prefixes, trackingFilename, maven.repo.local.tail. Both goals already wrap their resolver in ReactorMavenResolver, and pax-url could keep the plain directory. If you'd rather keep passing the settings to the resolver, it needs to read both key sets (new names taking precedence). See my comment on the import about where that mapping should live.

Dictionary<String, String> props = new Hashtable<>();
if (offline) {
props.put(pid + "offline", "true");
props.put(pid + ".offline", "true");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key fix is right, but it changes every mvn -o assembly build, not only split-repository ones. Until now the misspelled key meant pax-url always stayed online, so under -o it quietly fetched feature bundles that Maven itself never resolved. With this change:

  • a bundle referenced only from a features.xml and not yet in the local repository now fails the build instead of being downloaded;
  • an artifact cached under a different repository id (_remote.repositories says nexus, pax-url is configured with central) now fails with "Cannot access central ... in offline mode and the artifact has not been downloaded from it before", where it used to succeed.

That's arguably what -o should mean, but it's a visible behaviour change for existing builds, and the description proposes taking it to karaf-4.4.x too. Could you move this to its own PR and JIRA, with an invoker IT that runs an assembly offline and a release note? Today the test-offline invoker execution excludes all three assembly ITs (test-assembly, test-assembly-prop-edits, test-dockerfile), so nothing covers this path.

getLog().info("Using repositories: " + remoteRepositories);
config.put("maven.repositories", remoteRepositories);
config.put("maven.localRepository", localRepo.getBasedir());
config.put("maven.localRepository", getPaxUrlLocalRepository());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whichever PR ends up carrying the Builder offline fix, the verify goal needs it too. The resolver created at line 203 never gets maven.offline, so under mvn -o assembly resolves offline while verify still contacts the remote repositories (timeouts behind a firewall, downloads despite -o). Something like:

if (mavenSession.isOffline()) {
    config.put("maven.offline", "true");
}

Ideally with a test checking that both goals honour -o.

@ponziani
ponziani force-pushed the karaf-maven-plugin-split-local-repository branch from 02f22a6 to a25b3e2 Compare November 22, 2026 12:05
@ponziani ponziani changed the title [karaf-maven-plugin] Support Maven split local repository [KARAF-7667] Support Maven split local repository in karaf-maven-plugin Nov 22, 2026
@ponziani

Copy link
Copy Markdown
Contributor Author

Thanks @jbonofre, I took the ReactorMavenResolver route and updated the description.

  • After the reactor, it now asks mavenSession.getRepositorySession().getLocalRepositoryManager().find(...) with the project's remote repositories and the project context. Only then does it delegate to the MavenResolverFactory provider. The pax-url mapping in MojoSupport, the org.ops4j import and the pax-url based test are gone, so the plugin only talks to the SPI again, and the 4.4.x backport keeps the same shape.
  • The lookup is only done with the enhanced local repository. It takes installed artifacts, cached releases, and cached SNAPSHOTs when offline. Cached SNAPSHOTs when online, version ranges, LATEST/RELEASE and URLs that name their own repository still go to the provider.
  • This is not limited to split repositories. A locally installed SNAPSHOT now wins over a newer remote one (like the reactor), and cached releases skip the provider. I wrote that down in the description, and I can add a release note.
  • ReactorMavenResolverLocalRepositoryTest runs against the real EnhancedLocalRepositoryManager, with both property name sets, prefixes, and cached artifacts.
  • I also reproduced KARAF-7667 end-to-end with Maven 3.10.1 and -Daether.lrm.enhanced.split=true -Daether.lrm.enhanced.splitLocal=true: the plugin 4.4.11 fails with "Could not find artifact" for the installed SNAPSHOT, and this change builds the assembly with it. Details are in the description.
  • The Builder offline fix is out of this PR. I'll open a separate JIRA and PR for it, with the VerifyMojo offline flag, an offline assembly IT and a release note.

Rebased on current main and squashed.

@jbonofre

Copy link
Copy Markdown
Member

@ponziani thanks for the update, I gonna take a new look.

ReactorMavenResolver, used by the assembly and verify goals, now looks
up artifacts in the local repository of the Maven session, through its
LocalRepositoryManager, before it delegates to the MavenResolverFactory
provider. Artifacts are found whatever layout Maven uses: a split local
repository with the aether.lrm.enhanced.* or the older
aether.enhancedLocalRepository.* names, custom prefixes, or
maven.repo.local.tail.

The lookup is only done with the enhanced local repository. It takes
installed artifacts, cached releases, and cached SNAPSHOTs when Maven is
offline. Cached SNAPSHOTs when online, version ranges, LATEST/RELEASE and
URLs that name their own repository still go to the provider.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ponziani
ponziani force-pushed the karaf-maven-plugin-split-local-repository branch from a25b3e2 to ab162a8 Compare November 22, 2026 13:36
@ponziani

Copy link
Copy Markdown
Contributor Author

The first CI run of the update failed in karaf:verify: VerifyMojo redeclared the project field, so the inherited MojoSupport.project that the new resolver helper reads stayed null. I removed the duplicate field (amended into the same commit). karaf:verify on assemblies/features/standard now passes locally. Could you approve the workflow run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants