Skip to content

Commit

Permalink
Use LocalRepositoryManager for resolving artifacts paths in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Feb 17, 2024
1 parent d8e98dd commit ca3fee0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 70 deletions.
10 changes: 0 additions & 10 deletions plexus-compiler-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-settings-builder</artifactId>
<version>${mavenVersion}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,27 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.properties.internal.SystemProperties;
import org.apache.maven.settings.Settings;
import org.apache.maven.settings.building.DefaultSettingsBuilderFactory;
import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
import org.apache.maven.settings.building.SettingsBuildingRequest;
import org.codehaus.plexus.testing.PlexusTest;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.impl.LocalRepositoryProvider;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.LocalRepositoryManager;
import org.hamcrest.io.FileMatchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

/**
*
Expand All @@ -72,34 +71,25 @@ public abstract class AbstractCompilerTest {
private Map<String, Compiler> compilers;

@Inject
private ArtifactRepositoryLayout repositoryLayout;
private LocalRepositoryProvider localRepositoryProvider;

private ArtifactRepository localRepository;
private LocalRepositoryManager localRepositoryManager;

protected abstract String getRoleHint();

@BeforeEach
final void setUpLocalRepo() throws Exception {
String localRepo = System.getProperty("maven.repo.local");
assertThat("system property maven.repo.local", localRepo, notNullValue());

if (localRepo == null) {
File settingsFile = new File(System.getProperty("user.home"), ".m2/settings.xml");
if (settingsFile.exists()) {
SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
request.setUserSettingsFile(settingsFile);
request.setSystemProperties(SystemProperties.getSystemProperties());
Settings settings = new DefaultSettingsBuilderFactory()
.newInstance()
.build(request)
.getEffectiveSettings();
localRepo = settings.getLocalRepository();
}
}
if (localRepo == null) {
localRepo = System.getProperty("user.home") + "/.m2/repository";
}
LocalRepository localRepository = new LocalRepository(localRepo);
assertThat(
"test prerequisite: local repository path: " + localRepository.getBasedir(),
localRepository.getBasedir(),
FileMatchers.aReadableFile());

localRepository = new DefaultArtifactRepository("local", "file://" + localRepo, repositoryLayout);
RepositorySystemSession session = new DefaultRepositorySystemSession();
localRepositoryManager = localRepositoryProvider.newLocalRepositoryManager(session, localRepository);
}

protected void setCompilerDebug(boolean flag) {
Expand Down Expand Up @@ -338,6 +328,8 @@ protected String getJavaVersion() {
}

protected File getLocalArtifactPath(Artifact artifact) {
return new File(localRepository.getBasedir(), localRepository.pathOf(artifact));
return new File(
localRepositoryManager.getRepository().getBasedir(),
localRepositoryManager.getPathForLocalArtifact(RepositoryUtils.toArtifact(artifact)));
}
}
35 changes: 3 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>${redirectTestOutputToFile}</redirectTestOutputToFile>
<systemPropertyVariables>
<maven.repo.local>${settings.localRepository}</maven.repo.local>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -206,36 +209,4 @@
</plugins>
</build>

<profiles>
<profile>
<!--
Make sure maven.repo.local is passed through to the tests if set or the tests will fail to resolve artifacts
(c.f. SUREFIRE-491).
-->
<id>maven.repo.local</id>
<activation>
<property>
<name>maven.repo.local</name>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties combine.children="append">
<property>
<name>maven.repo.local</name>
<value>${maven.repo.local}</value>
</property>
</systemProperties>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
</project>

0 comments on commit ca3fee0

Please sign in to comment.