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.bridge.MavenRepositorySystem;
31 import org.apache.maven.model.building.ModelBuildingException;
32 import org.apache.maven.model.building.ModelProblem;
33 import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
34 import org.codehaus.plexus.PlexusContainer;
35 import org.codehaus.plexus.testing.PlexusTest;
36 import org.eclipse.aether.DefaultRepositoryCache;
37 import org.eclipse.aether.DefaultRepositorySystemSession;
38 import org.junit.jupiter.api.BeforeEach;
39
40
41
42
43 @PlexusTest
44 public abstract class AbstractMavenProjectTestCase {
45 protected ProjectBuilder projectBuilder;
46
47 @Inject
48 protected MavenRepositorySystem repositorySystem;
49
50 @Inject
51 protected PlexusContainer container;
52
53 public PlexusContainer getContainer() {
54 return container;
55 }
56
57 @BeforeEach
58 public void setUp() throws Exception {
59 if (getContainer().hasComponent(ProjectBuilder.class, "test")) {
60 projectBuilder = getContainer().lookup(ProjectBuilder.class, "test");
61 } else {
62
63 projectBuilder = getContainer().lookup(ProjectBuilder.class);
64 }
65 }
66
67 protected ProjectBuilder getProjectBuilder() {
68 return projectBuilder;
69 }
70
71
72
73
74
75 protected File getLocalRepositoryPath() throws FileNotFoundException, URISyntaxException {
76 File markerFile = getFileForClasspathResource("local-repo/marker.txt");
77
78 return markerFile.getAbsoluteFile().getParentFile();
79 }
80
81 protected static File getFileForClasspathResource(String resource)
82 throws FileNotFoundException, URISyntaxException {
83 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
84
85 URL resourceUrl = cloader.getResource(resource);
86
87 if (resourceUrl == null) {
88 throw new FileNotFoundException("Unable to find: " + resource);
89 }
90
91 return new File(resourceUrl.toURI());
92 }
93
94 protected ArtifactRepository getLocalRepository() throws Exception {
95 return repositorySystem.createLocalRepository(getLocalRepositoryPath());
96 }
97
98
99
100
101
102 protected MavenProject getProjectWithDependencies(File pom) throws Exception {
103 ProjectBuildingRequest configuration = newBuildingRequest();
104 configuration.setRemoteRepositories(Arrays.asList(new ArtifactRepository[] {}));
105 configuration.setProcessPlugins(false);
106 configuration.setResolveDependencies(true);
107
108 try {
109 return projectBuilder.build(pom, configuration).getProject();
110 } catch (Exception e) {
111 Throwable cause = e.getCause();
112 if (cause instanceof ModelBuildingException) {
113 String message = "In: " + pom + "\n\n";
114 for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
115 message += problem + "\n";
116 }
117 System.out.println(message);
118 }
119
120 throw e;
121 }
122 }
123
124 protected MavenProject getProject(File pom) throws Exception {
125 ProjectBuildingRequest configuration = newBuildingRequest();
126
127 return projectBuilder.build(pom, configuration).getProject();
128 }
129
130 protected MavenProject getProjectFromRemoteRepository(final File pom) throws Exception {
131 final ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
132 configuration.setLocalRepository(this.getLocalRepository());
133 configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
134 initRepoSession(configuration);
135
136 return projectBuilder.build(pom, configuration).getProject();
137 }
138
139 protected ProjectBuildingRequest newBuildingRequest() throws Exception {
140 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
141 configuration.setLocalRepository(getLocalRepository());
142 configuration.setRemoteRepositories(Arrays.asList(this.repositorySystem.createDefaultRemoteRepository()));
143 initRepoSession(configuration);
144 return configuration;
145 }
146
147 protected void initRepoSession(ProjectBuildingRequest request) {
148 File localRepo = new File(request.getLocalRepository().getBasedir());
149 DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
150 repoSession.setCache(new DefaultRepositoryCache());
151 repoSession.setLocalRepositoryManager(new LegacyLocalRepositoryManager(localRepo));
152 request.setRepositorySession(repoSession);
153 }
154 }