Skip to content

Commit

Permalink
Add check for deps not in bom.pom (#5291)
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored May 16, 2023
1 parent 2445c46 commit d60da24
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 45 deletions.
6 changes: 6 additions & 0 deletions tests/release-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
<version>${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
Expand Down Expand Up @@ -51,60 +52,97 @@ public class DownloadBomPomDependencies extends AbstractMojoTestCase {

@Test
public void testDownloadBomPomDependencies() throws Exception {
// RepositorySystem repositorySystem = (RepositorySystem) lookup(RepositorySystem.class.getName());
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
RepositorySystem repositorySystem = locator.getService(RepositorySystem.class);

RepositorySystemSession repoSession = getRepoSession(repositorySystem);
List<RemoteRepository> remoteRepos = getRemoteRepositories(repoSession);

Properties properties = MavenUtil.getMavenProperties();
String jerseyVersion = MavenUtil.getJerseyVersion(properties);
MavenEnvironment mavenEnvironment = new MavenEnvironment();
List<Dependency> memberDeps = MavenUtil.streamJerseyJars().collect(Collectors.toList());
for (Dependency member : memberDeps) {
member.setVersion(jerseyVersion);
Artifact m = DependencyResolver.resolveArtifact(member, remoteRepos, repositorySystem, repoSession);
Artifact m = mavenEnvironment.resolveArtifact(member);
System.out.append("Resolved ").append(member.getGroupId()).append(":").append(member.getArtifactId()).append(":")
.append(member.getVersion()).append(" to ").println(m.getFile().getName());
}
}

private List<RemoteRepository> getRemoteRepositories(RepositorySystemSession session) throws Exception {
File pom = lookupResourcesPom("/release-test-pom.xml");
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setPom(pom);
request.addActiveProfile("staging");
ProjectBuildingRequest buildingRequest = request
.getProjectBuildingRequest()
.setRepositorySession(session)
.setResolveDependencies(true);

ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
ProjectBuildingResult projectBuildingResult = projectBuilder.build(pom, buildingRequest);
MavenProject project = projectBuildingResult.getProject();

List<RemoteRepository> remoteArtifactRepositories = project.getRemoteProjectRepositories();
return remoteArtifactRepositories;
}
@Test
public void testDownloadNonBomPomDependencies() throws Exception {
MavenEnvironment mavenEnvironment = new MavenEnvironment();
MavenProject project = mavenEnvironment.getMavenProjectForResourceFile("/non-bom-pom-deps.xml");
for (Dependency dependency : project.getDependencies()) {
if (dependency.getArtifactId().contains("jackson1") && mavenEnvironment.jerseyVersion.startsWith("3")) {
continue;
}

private static RepositorySystemSession getRepoSession(RepositorySystem repositorySystem) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository(MavenUtil.getLocalMavenRepository().getAbsolutePath());
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepo));
return session;
Artifact m = mavenEnvironment.resolveArtifact(dependency);
System.out.append("Resolved ").append(dependency.getGroupId()).append(":")
.append(dependency.getArtifactId()).append(":")
.append(dependency.getVersion()).append(" to ").println(m.getFile().getName());
}
}

private static File lookupResourcesPom(String pomFile) throws URISyntaxException {
URL resource = DownloadBomPomDependencies.class.getResource(pomFile);
if (resource == null) {
throw new IllegalStateException("Pom file " + pomFile + " was not located on classpath!");
private class MavenEnvironment {
private final RepositorySystem repositorySystem;
private final RepositorySystemSession repoSession;
private final List<RemoteRepository> remoteRepos;
private final String jerseyVersion;

MavenEnvironment() throws Exception {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);

repositorySystem = locator.getService(RepositorySystem.class);
repoSession = getRepoSession();

remoteRepos = getRemoteRepositories();

Properties properties = MavenUtil.getMavenProperties();
jerseyVersion = MavenUtil.getJerseyVersion(properties);
}

Artifact resolveArtifact(Dependency dependency) throws ArtifactResolutionException {
dependency.setVersion(jerseyVersion);
return DependencyResolver.resolveArtifact(dependency, remoteRepos, repositorySystem, repoSession);
}

private List<RemoteRepository> getRemoteRepositories() throws Exception {
MavenProject project = getMavenProjectForResourceFile("/release-test-pom.xml");
List<RemoteRepository> remoteArtifactRepositories = project.getRemoteProjectRepositories();
return remoteArtifactRepositories;
}

private MavenProject getMavenProjectForResourceFile(String resourceFile)
throws Exception {
File pom = lookupResourcesPom(resourceFile);
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setPom(pom);
request.addActiveProfile("staging");
ProjectBuildingRequest buildingRequest = request
.getProjectBuildingRequest()
.setRepositorySession(repoSession)
.setResolveDependencies(true);

ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
ProjectBuildingResult projectBuildingResult = projectBuilder.build(pom, buildingRequest);
MavenProject project = projectBuildingResult.getProject();

return project;
}
File file = new File(resource.toURI());
if (!file.exists()) {
throw new IllegalStateException("Cannot locate test pom xml file!");

private RepositorySystemSession getRepoSession() {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository(MavenUtil.getLocalMavenRepository().getAbsolutePath());
session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepo));
return session;
}

private File lookupResourcesPom(String pomFile) throws URISyntaxException {
URL resource = DownloadBomPomDependencies.class.getResource(pomFile);
if (resource == null) {
throw new IllegalStateException("Pom file " + pomFile + " was not located on classpath!");
}
File file = new File(resource.toURI());
if (!file.exists()) {
throw new IllegalStateException("Cannot locate test pom xml file!");
}
return file;
}
return file;
}
}
77 changes: 77 additions & 0 deletions tests/release-test/src/test/resources/non-bom-pom-deps.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.ee4j</groupId>
<artifactId>project</artifactId>
<version>1.0.8</version>
</parent>

<groupId>org.glassfish.jersey.tests</groupId>
<artifactId>release-test-test</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson1</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-client</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-signature</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.incubator</groupId>
<artifactId>jersey-injectless-client</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.incubator</groupId>
<artifactId>jersey-open-tracing</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-kryo</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-declarative-linking</artifactId>
<version>2.27</version>
</dependency>
</dependencies>

</project>
4 changes: 2 additions & 2 deletions tests/release-test/src/test/resources/release-test-pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2022, 2023 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,7 +24,7 @@
<parent>
<groupId>org.eclipse.ee4j</groupId>
<artifactId>project</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
</parent>

<groupId>org.glassfish.jersey.tests</groupId>
Expand Down

0 comments on commit d60da24

Please sign in to comment.