1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.FileNotFoundException;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.util.Arrays;
28
29 import org.apache.maven.artifact.repository.ArtifactRepository;
30 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
31 import org.apache.maven.model.building.ModelBuildingException;
32 import org.apache.maven.model.building.ModelProblem;
33 import org.apache.maven.repository.RepositorySystem;
34 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
35 import org.codehaus.plexus.ContainerConfiguration;
36 import org.codehaus.plexus.PlexusContainer;
37 import org.codehaus.plexus.testing.PlexusTestConfiguration;
38 import org.eclipse.aether.DefaultRepositorySystemSession;
39 import org.junit.jupiter.api.BeforeEach;
40
41 import static org.junit.jupiter.api.Assertions.fail;
42
43
44
45
46 public abstract class AbstractMavenProjectTestCase implements PlexusTestConfiguration {
47
48 protected ProjectBuilder projectBuilder;
49
50 @Inject
51 protected RepositorySystem repositorySystem;
52
53 @Inject
54 protected PlexusContainer container;
55
56 @Inject
57 private ArtifactRepositoryLayout repoLayout;
58
59 @BeforeEach
60 protected void setUp() throws Exception {
61
62 if (container.hasComponent(ProjectBuilder.class, "test")) {
63 projectBuilder = container.lookup(ProjectBuilder.class, "test");
64 } else {
65
66 projectBuilder = container.lookup(ProjectBuilder.class);
67 }
68 }
69
70 @Override
71 public void customizeConfiguration(ContainerConfiguration containerConfiguration) {
72 String name = AbstractMavenProjectTestCase.class.getName().replace('.', '/') + ".xml";
73 containerConfiguration.setContainerConfiguration(name);
74 }
75
76
77
78
79
80 protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
81 File markerFile = getFileForClasspathResource("local-repo/marker.txt");
82
83 return markerFile.getAbsoluteFile().getParentFile();
84 }
85
86 protected static File getFileForClasspathResource(String resource)
87 throws FileNotFoundException, URISyntaxException {
88 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
89
90 URL resourceUrl = cloader.getResource(resource);
91
92 if (resourceUrl == null) {
93 throw new FileNotFoundException("Unable to find: " + resource);
94 }
95
96 return new File(resourceUrl.toURI());
97 }
98
99 private ArtifactRepository getLocalRepository() throws Exception {
100
101 ArtifactRepository r = repositorySystem.createArtifactRepository(
102 "local", "file://" + getLocalRepositoryPath().getAbsolutePath(), repoLayout, null, null);
103 return r;
104 }
105
106
107
108
109
110 protected MavenProject getProjectWithDependencies(File pom) throws Exception {
111 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
112 configuration.setLocalRepository(getLocalRepository());
113 configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
114 configuration.setProcessPlugins(false);
115 configuration.setResolveDependencies(true);
116 initRepoSession(configuration);
117
118 try {
119 return projectBuilder.build(pom, configuration).getProject();
120 } catch (Exception e) {
121 Throwable cause = e.getCause();
122 if (cause instanceof ModelBuildingException) {
123 String message = "In: " + pom + "\n\n";
124 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
125 message += problem + "\n";
126 }
127 System.out.println(message);
128 fail(message);
129 }
130
131 throw e;
132 }
133 }
134
135 protected MavenProject getProject(File pom) throws Exception {
136 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
137 configuration.setLocalRepository(getLocalRepository());
138 initRepoSession(configuration);
139
140 return projectBuilder.build(pom, configuration).getProject();
141 }
142
143 private void initRepoSession(ProjectBuildingRequest request) {
144 File localRepo = new File(request.getLocalRepository().getBasedir());
145 DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
146 session.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
147 request.setRepositorySession(session);
148 }
149 }