From 87c5a6807e7aa20746e33ef613725c2e93581a54 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Fri, 30 Aug 2024 06:34:31 -0300 Subject: [PATCH] fix: Explicitly set remote repositories before resolving poms Fixes #22 --- pomchecker-cli/pom.xml | 6 ++++ .../maven/checker/cli/internal/PomParser.java | 19 +++++++--- .../checker/cli/internal/PomParserTest.java | 36 +++++++++++++++++++ .../src/test/resources/test-pom.xml | 34 ++++++++++++++++++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 pomchecker-cli/src/test/java/org/kordamp/maven/checker/cli/internal/PomParserTest.java create mode 100644 pomchecker-cli/src/test/resources/test-pom.xml diff --git a/pomchecker-cli/pom.xml b/pomchecker-cli/pom.xml index 14f9a9c0..68a4bd3b 100644 --- a/pomchecker-cli/pom.xml +++ b/pomchecker-cli/pom.xml @@ -145,6 +145,12 @@ slf4j-simple runtime + + org.junit.jupiter + junit-jupiter + 5.11.0 + test + diff --git a/pomchecker-cli/src/main/java/org/kordamp/maven/checker/cli/internal/PomParser.java b/pomchecker-cli/src/main/java/org/kordamp/maven/checker/cli/internal/PomParser.java index df3c3900..848994d1 100644 --- a/pomchecker-cli/src/main/java/org/kordamp/maven/checker/cli/internal/PomParser.java +++ b/pomchecker-cli/src/main/java/org/kordamp/maven/checker/cli/internal/PomParser.java @@ -22,6 +22,8 @@ import eu.maveniverse.maven.mima.context.Context; import eu.maveniverse.maven.mima.context.ContextOverrides; import eu.maveniverse.maven.mima.context.Runtimes; +import org.apache.maven.artifact.repository.MavenArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; import org.apache.maven.execution.DefaultMavenExecutionRequest; import org.apache.maven.execution.MavenExecutionRequest; import org.apache.maven.project.MavenProject; @@ -37,12 +39,12 @@ import org.codehaus.plexus.PlexusContainerException; import org.codehaus.plexus.classworlds.ClassWorld; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.repository.RemoteRepository; import java.io.File; import java.util.Locale; import java.util.Properties; - -import static java.util.Objects.requireNonNull; +import java.util.stream.Collectors; /** * @author Andres Almiray @@ -83,7 +85,8 @@ private static MavenProject createMavenProject(File pomFile, Context context, Pl mavenExecutionRequest.getProjectBuildingRequest(); projectBuildingRequest.setRepositorySession(context.repositorySystemSession()); - + projectBuildingRequest.setRemoteRepositories(context.remoteRepositories() + .stream().map(PomParser::toArtifactRepository).collect(Collectors.toList())); // Profile activation needs properties such as JDK version Properties properties = new Properties(); // allowing duplicate entries properties.putAll(projectBuildingRequest.getSystemProperties()); @@ -138,4 +141,12 @@ private static String osDetectedArch() { return "x86_32"; } } -} \ No newline at end of file + + private static MavenArtifactRepository toArtifactRepository(RemoteRepository remoteRepository) { + MavenArtifactRepository mavenArtifactRepository = new MavenArtifactRepository(); + mavenArtifactRepository.setId(remoteRepository.getId()); + mavenArtifactRepository.setUrl(remoteRepository.getUrl()); + mavenArtifactRepository.setLayout(new DefaultRepositoryLayout()); + return mavenArtifactRepository; + } +} diff --git a/pomchecker-cli/src/test/java/org/kordamp/maven/checker/cli/internal/PomParserTest.java b/pomchecker-cli/src/test/java/org/kordamp/maven/checker/cli/internal/PomParserTest.java new file mode 100644 index 00000000..24e8314b --- /dev/null +++ b/pomchecker-cli/src/test/java/org/kordamp/maven/checker/cli/internal/PomParserTest.java @@ -0,0 +1,36 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2020-2024 Andres Almiray. + * + * Licensed 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 + * + * https://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. + */ +package org.kordamp.maven.checker.cli.internal; + +import org.apache.maven.project.MavenProject; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URL; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PomParserTest { + + @Test + void parse() throws Exception { + URL resource = getClass().getClassLoader().getResource("test-pom.xml"); + MavenProject mavenProject = PomParser.createMavenProject(new File(resource.toURI())); + assertEquals("quarkus-slack-parent",mavenProject.getArtifactId()); + } +} diff --git a/pomchecker-cli/src/test/resources/test-pom.xml b/pomchecker-cli/src/test/resources/test-pom.xml new file mode 100644 index 00000000..f5fdf65c --- /dev/null +++ b/pomchecker-cli/src/test/resources/test-pom.xml @@ -0,0 +1,34 @@ + + + + 4.0.0 + + + io.quarkiverse + quarkiverse-parent + 17 + + io.quarkiverse.slack + quarkus-slack-parent + 0.0.2 + pom + +