Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Repository;
import org.apache.maven.model.building.FileModelSource;
import org.apache.maven.model.building.ModelSource;

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-26.04 jdk-17-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-8-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 33 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated
import org.apache.maven.model.resolution.ModelResolver;
import org.apache.maven.model.resolution.UnresolvableModelException;
import org.apache.maven.project.MavenProject;
Expand All @@ -36,6 +40,7 @@
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.repository.RepositoryPolicy;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.VersionRangeRequest;
Expand All @@ -60,6 +65,8 @@

private final List<RemoteRepository> repositories;

private final Set<String> repositoryIds;

/** The modules of the project being built. */
private final ReactorModelPool reactorModelPool;

Expand All @@ -77,7 +84,8 @@
this.repositorySystem = repositorySystem;
this.trace = trace;
this.context = context;
this.repositories = repositories;
this.repositories = new ArrayList<>(repositories);
this.repositoryIds = new HashSet<>();

this.reactorModelPool = new ReactorModelPool();
reactorModelPool.addProjects(reactorModels);
Expand All @@ -88,14 +96,15 @@
this.repositorySystem = other.repositorySystem;
this.trace = other.trace;
this.context = other.context;
this.repositories = other.repositories;
this.repositories = new ArrayList<>(other.repositories);
this.repositoryIds = new HashSet<>(other.repositoryIds);
this.reactorModelPool = other.reactorModelPool;
}

/**
* {@inheritDoc}
*/
public ModelSource resolveModel(String groupId, String artifactId, String version)

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-26.04 jdk-17-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-8-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 107 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated
throws UnresolvableModelException {
File pomFile = reactorModelPool.find(groupId, artifactId, version);
if (pomFile == null) {
Expand All @@ -118,7 +127,7 @@
* {@inheritDoc}
*/
public void addRepository(Repository repository) {
// ignoring... artifact resolution via repository should already have happened before by maven core.
addRepository(repository, false);
}

/**
Expand All @@ -135,13 +144,13 @@
* @return The source of the requested POM, never {@code null}
* @since Apache-Maven-3.2.2 (MNG-5639)
*/
public ModelSource resolveModel(Parent parent) throws UnresolvableModelException {

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-26.04 jdk-17-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-8-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 147 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated
parent.setVersion(resolveVersion(parent.getGroupId(), parent.getArtifactId(), parent.getVersion()));
return resolveModel(parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}

@Override
public ModelSource resolveModel(Dependency dependency) throws UnresolvableModelException {

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-26.04 jdk-17-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-8-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 3.10.0

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-21-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / ubuntu-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated

Check warning on line 153 in src/main/java/org/codehaus/mojo/flatten/model/resolution/FlattenModelResolver.java

View workflow job for this annotation

GitHub Actions / Verify / Verify / macos-latest jdk-25-temurin 4.0.0-rc-7

org.apache.maven.model.building.ModelSource in org.apache.maven.model.building has been deprecated
dependency.setVersion(
resolveVersion(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()));
return resolveModel(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
Expand Down Expand Up @@ -183,6 +192,40 @@
* @since Apache-Maven-3.2.3 (MNG-5663)
*/
public void addRepository(Repository repository, boolean replace) {
// ignoring... artifact resolution via repository should already have happened before by maven core.
if (!repositoryIds.add(repository.getId())) {
if (!replace) {
return;
}
repositories.removeIf(r -> r.getId().equals(repository.getId()));
}

RemoteRepository remoteRepository = new RemoteRepository.Builder(
repository.getId(), repository.getLayout(), repository.getUrl())
.setReleasePolicy(toRepositoryPolicy(repository.getReleases()))
.setSnapshotPolicy(toRepositoryPolicy(repository.getSnapshots()))
.build();
// applies the mirrors, proxies and authentication of the settings
for (RemoteRepository resolutionRepository :
repositorySystem.newResolutionRepositories(session, Collections.singletonList(remoteRepository))) {
if (repositories.stream().noneMatch(r -> r.getId().equals(resolutionRepository.getId()))) {
repositories.add(resolutionRepository);
}
}
}

private static RepositoryPolicy toRepositoryPolicy(org.apache.maven.model.RepositoryPolicy policy) {
boolean enabled = true;
String updatePolicy = RepositoryPolicy.UPDATE_POLICY_DAILY;
String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_WARN;
if (policy != null) {
enabled = policy.isEnabled();
if (policy.getUpdatePolicy() != null) {
updatePolicy = policy.getUpdatePolicy();
}
if (policy.getChecksumPolicy() != null) {
checksumPolicy = policy.getChecksumPolicy();
}
}
return new RepositoryPolicy(enabled, updatePolicy, checksumPolicy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.flatten.model.resolution.FlattenModelResolver;
import org.codehaus.plexus.util.ReflectionUtils;
import org.eclipse.aether.RepositorySystem;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -55,6 +56,9 @@ class CreateEffectivePomTest {
@Inject
private MavenProject mavenProject;

@Inject
private RepositorySystem repositorySystem;

/**
* Tests method to create effective POM.
*
Expand All @@ -70,7 +74,7 @@ void createEffectivePom(FlattenMojo tested) throws Exception {

FlattenModelResolver resolver = new FlattenModelResolver(
session.getRepositorySession(),
null,
repositorySystem,
null,
null,
Collections.emptyList(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.codehaus.mojo.flatten.model.resolution;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.apache.maven.model.Repository;
import org.apache.maven.model.resolution.ModelResolver;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class FlattenModelResolverTest {

private final RepositorySystemSession session = mock(RepositorySystemSession.class);

private final RepositorySystem repositorySystem = mock(RepositorySystem.class);

private final FlattenModelResolver resolver = new FlattenModelResolver(
session,
repositorySystem,
null,
"flatten",
Collections.singletonList(new RemoteRepository.Builder("central", "default", "https://central").build()),
Collections.emptyList());

@BeforeEach
void setUp() throws Exception {
when(repositorySystem.newResolutionRepositories(any(), anyList()))
.thenAnswer(invocation -> invocation.getArgument(1));
when(repositorySystem.resolveArtifact(any(), any())).thenAnswer(invocation -> {
ArtifactRequest request = invocation.getArgument(1);
return new ArtifactResult(request).setArtifact(request.getArtifact().setFile(new File("pom.xml")));
});
}

@Test
void resolvesModelFromAddedRepository() throws Exception {
resolver.addRepository(repository("parent-repo", "https://parent"));

assertThat(repositoriesOfNextResolution(resolver))
.extracting(RemoteRepository::getId)
.containsExactly("central", "parent-repo");
}

@Test
void replacesRepositoryWithSameIdOnlyWhenAsked() throws Exception {
resolver.addRepository(repository("parent-repo", "https://first"));
resolver.addRepository(repository("parent-repo", "https://second"));

assertThat(repositoriesOfNextResolution(resolver))
.extracting(RemoteRepository::getUrl)
.containsExactly("https://central", "https://first");

resolver.addRepository(repository("parent-repo", "https://third"), true);

assertThat(repositoriesOfNextResolution(resolver))
.extracting(RemoteRepository::getUrl)
.containsExactly("https://central", "https://third");
}

@Test
void addsMirrorOfRepositoryOnlyOnce() throws Exception {
RemoteRepository mirror = new RemoteRepository.Builder("mirror", "default", "https://mirror").build();
when(repositorySystem.newResolutionRepositories(any(), anyList()))
.thenReturn(Collections.singletonList(mirror));

resolver.addRepository(repository("parent-repo", "https://parent"));
resolver.addRepository(repository("bom-repo", "https://bom"));

assertThat(repositoriesOfNextResolution(resolver))
.extracting(RemoteRepository::getId)
.containsExactly("central", "mirror");
}

@Test
void copyKeepsItsOwnRepositories() throws Exception {
resolver.addRepository(repository("parent-repo", "https://parent"));
ModelResolver copy = resolver.newCopy();
copy.addRepository(repository("bom-repo", "https://bom"));

assertThat(repositoriesOfNextResolution(copy))
.extracting(RemoteRepository::getId)
.containsExactly("central", "parent-repo", "bom-repo");
assertThat(repositoriesOfNextResolution(resolver))
.extracting(RemoteRepository::getId)
.containsExactly("central", "parent-repo");
}

private List<RemoteRepository> repositoriesOfNextResolution(ModelResolver modelResolver) throws Exception {
modelResolver.resolveModel("org.example", "example-parent", "1.0");
ArgumentCaptor<ArtifactRequest> request = ArgumentCaptor.forClass(ArtifactRequest.class);
verify(repositorySystem, atLeastOnce()).resolveArtifact(any(), request.capture());
return request.getValue().getRepositories();
}

private static Repository repository(String id, String url) {
Repository repository = new Repository();
repository.setId(id);
repository.setUrl(url);
return repository;
}
}
Loading