Skip to content

Commit b33aedc

Browse files
committed
Fixes ClasspathRegistry for multi maven modules
1 parent 49f44cc commit b33aedc

File tree

3 files changed

+207
-46
lines changed

3 files changed

+207
-46
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/java/impl/ClasspathRegistry.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.sbm.java.impl;
1717

1818
import lombok.extern.slf4j.Slf4j;
19-
import org.openrewrite.maven.internal.MavenPomDownloader;
2019
import org.openrewrite.maven.tree.ResolvedDependency;
2120
import org.openrewrite.maven.tree.Scope;
2221
import org.springframework.sbm.build.api.BuildFile;
@@ -96,7 +95,7 @@ private static class DependenciesRegistryHolder {
9695
public static final ClasspathRegistry INSTANCE = new ClasspathRegistry();
9796

9897
public static ClasspathRegistry initialDependencies(Set<ResolvedDependency> dependencies) {
99-
INSTANCE.initDependencies(dependencies);
98+
INSTANCE.setup(dependencies);
10099
return INSTANCE;
101100
}
102101

@@ -132,27 +131,40 @@ public Set<Path> getCurrentDependencies() {
132131
return new HashSet<>(currentDependencies.values());
133132
}
134133

135-
private void initDependencies(Set<ResolvedDependency> deps) {
134+
private void setup(Set<ResolvedDependency> deps) {
136135
initialDependencies.clear();
137136
currentDependencies.clear();
137+
initializeDepeendencies(deps);
138+
}
139+
140+
private void initializeDepeendencies(Set<ResolvedDependency> deps) {
138141
deps.forEach(dep -> {
139142
initDependency(dep, initialDependencies, currentDependencies);
140143
});
141144
}
142145

143146
private void initDependency(ResolvedDependency d, Map<ResolvedDependency, Path>... maps) {
144-
Path dependencyPath = new RewriteMavenArtifactDownloader().downloadArtifact(d);
145-
if(dependencyPath != null) {
146-
Stream.of(maps).forEach(m -> m.put(d, dependencyPath));
147+
if(isExternalDependency(d)) {
148+
Path dependencyPath = new RewriteMavenArtifactDownloader().downloadArtifact(d);
149+
if(dependencyPath != null) {
150+
Stream.of(maps).forEach(m -> m.put(d, dependencyPath));
151+
} else {
152+
System.out.println(d.getGav() + " has no jars. It has type " + d.getType());
153+
initializeDepeendencies(new HashSet<>(d.getDependencies()));
154+
}
147155
} else {
148-
System.out.println(d.getGav() + " has no jars. It has type " + d.getType());
149-
initDependencies(new HashSet<>(d.getDependencies()));
156+
initializeDepeendencies(new HashSet(d.getDependencies()));
150157
}
151158

159+
152160
// Optional<Path> dependencyPath = dependencyHelper.downloadArtifact(d);
153161
// if (dependencyPath.isPresent()) {
154162
// Stream.of(maps).forEach(m -> m.put(d, dependencyPath.get()));
155163
// }
156164
}
157165

166+
private boolean isExternalDependency(ResolvedDependency d) {
167+
return d.getRepository() != null;
168+
}
169+
158170
}

components/sbm-core/src/test/java/org/springframework/sbm/build/api/ApplicationModuleTest.java

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.sbm.build.api;
1717

18+
import org.intellij.lang.annotations.Language;
1819
import org.springframework.sbm.engine.context.ProjectContext;
1920
import org.springframework.sbm.project.resource.TestProjectContext;
2021
import org.junit.jupiter.api.Disabled;
@@ -35,58 +36,83 @@ void searchMainResources() {
3536

3637
}
3738

39+
/**
40+
* tests {@link ApplicationModules#getModule(Path)}
41+
*/
3842
@Test
39-
void testGetModuleResources() {
43+
void testGetModule() {
44+
@Language("xml")
4045
String parentPom =
41-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
42-
"<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\">\n" +
43-
" <modelVersion>4.0.0</modelVersion>\n" +
44-
" <groupId>com.acme</groupId>\n" +
45-
" <artifactId>dummy</artifactId>\n" +
46-
" <version>0.0.1-SNAPSHOT</version>\n" +
47-
" <packaging>pom</packaging>" +
48-
"</project>";
46+
"""
47+
<?xml version="1.0" encoding="UTF-8"?>
48+
<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">
49+
<modelVersion>4.0.0</modelVersion>
50+
<groupId>com.acme</groupId>
51+
<artifactId>dummy</artifactId>
52+
<version>0.0.1-SNAPSHOT</version>
53+
<packaging>pom</packaging>
54+
<modules>
55+
<module>pom1</module>
56+
<module>pom2</module>
57+
</modules>
58+
</project>
59+
""";
60+
61+
@Language("xml")
4962
String pom1 =
50-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
51-
"<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\">\n" +
52-
" <modelVersion>4.0.0</modelVersion>\n" +
53-
" <parent>\n" +
54-
" <groupId>com.acme</groupId>\n" +
55-
" <artifactId>dummy</artifactId>\n" +
56-
" <version>0.0.1-SNAPSHOT</version>\n" +
57-
" <relativePath>../</relativePath>\n" +
58-
" </parent>\n" +
59-
" <artifactId>pom1</artifactId>\n" +
60-
"</project>";
63+
"""
64+
<?xml version="1.0" encoding="UTF-8"?>
65+
<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">
66+
<modelVersion>4.0.0</modelVersion>
67+
<parent>
68+
<groupId>com.acme</groupId>
69+
<artifactId>dummy</artifactId>
70+
<version>0.0.1-SNAPSHOT</version>
71+
<relativePath>../</relativePath>
72+
</parent>
73+
<artifactId>pom1</artifactId>
74+
</project>
75+
""";
76+
77+
@Language("xml")
6178
String pom2 =
62-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
63-
"<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\">\n" +
64-
" <modelVersion>4.0.0</modelVersion>\n" +
65-
" <parent>\n" +
66-
" <groupId>com.acme</groupId>\n" +
67-
" <artifactId>dummy</artifactId>\n" +
68-
" <version>0.0.1-SNAPSHOT</version>\n" +
69-
" <relativePath>../</relativePath>\n" +
70-
" </parent>\n" +
71-
" <artifactId>pom2</artifactId>\n" +
72-
" <dependencies>\n" +
73-
" <dependency>\n" +
74-
" <groupId>com.acme</groupId>\n" +
75-
" <artifactId>pom1</artifactId>\n" +
76-
" <version>0.0.1-SNAPSHOT</version>\n" +
77-
" </dependency>\n" +
78-
" </dependencies>\n" +
79-
"</project>";
79+
"""
80+
<?xml version="1.0" encoding="UTF-8"?>
81+
<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">
82+
<modelVersion>4.0.0</modelVersion>
83+
<parent>
84+
<groupId>com.acme</groupId>
85+
<artifactId>dummy</artifactId>
86+
<version>0.0.1-SNAPSHOT</version>
87+
<relativePath>../</relativePath>
88+
</parent>
89+
<artifactId>pom2</artifactId>
90+
<dependencies>
91+
<dependency>
92+
<groupId>com.acme</groupId>
93+
<artifactId>pom1</artifactId>
94+
<version>0.0.1-SNAPSHOT</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>javax.validation</groupId>
98+
<artifactId>validation-api</artifactId>
99+
<version>2.0.1.Final</version>
100+
</dependency>
101+
</dependencies>
102+
</project>
103+
""";
80104

81105
String javaSource2 = "public class Module1 {}";
82106
String javaSource3 = "public class Module2 {}";
107+
String javaSource4 = "public class SomeTest {}";
83108

84109
ProjectContext context = TestProjectContext.buildProjectContext()
85110
.addProjectResource("pom.xml", parentPom)
86111
.addProjectResource("module1/pom.xml", pom1)
87112
.addProjectResource("module2/pom.xml", pom2)
88113
.addJavaSource("module1/src/main/java", javaSource2)
89114
.addJavaSource("module2/src/main/java", javaSource3)
115+
.addJavaSource("module2/src/test/java", javaSource4)
90116
.build();
91117

92118
ApplicationModule parentModule = context.getApplicationModules().getModule(Path.of(""));
@@ -100,6 +126,9 @@ void testGetModuleResources() {
100126

101127
assertThat(module2.getMainJavaSourceSet().list()).hasSize(1);
102128
assertThat(module2.getMainJavaSourceSet().list().get(0).print()).isEqualTo(javaSource3);
129+
130+
assertThat(module2.getTestJavaSourceSet().list()).hasSize(1);
131+
assertThat(module2.getTestJavaSourceSet().list().get(0).print()).isEqualTo(javaSource4);
103132
}
104133

105134
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.sbm.java.impl;
18+
19+
import org.intellij.lang.annotations.Language;
20+
import org.junit.jupiter.api.Test;
21+
import org.openrewrite.maven.tree.MavenResolutionResult;
22+
import org.openrewrite.maven.tree.ResolvedDependency;
23+
import org.openrewrite.maven.tree.Scope;
24+
import org.openrewrite.xml.tree.Xml;
25+
import org.springframework.sbm.build.impl.RewriteMavenParser;
26+
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.stream.Collectors;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
public class ClasspathRegistryTest {
34+
@Test
35+
void classpathRegistryShouldKeepOnlyExternalDependencies() {
36+
37+
@Language("xml")
38+
String parentPom =
39+
"""
40+
<?xml version="1.0" encoding="UTF-8"?>
41+
<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">
42+
<modelVersion>4.0.0</modelVersion>
43+
<groupId>com.acme</groupId>
44+
<artifactId>dummy</artifactId>
45+
<version>0.0.1-SNAPSHOT</version>
46+
<packaging>pom</packaging>
47+
<modules>
48+
<module>pom1</module>
49+
<module>pom2</module>
50+
</modules>
51+
</project>
52+
""";
53+
54+
@Language("xml")
55+
String pom1 =
56+
"""
57+
<?xml version="1.0" encoding="UTF-8"?>
58+
<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">
59+
<modelVersion>4.0.0</modelVersion>
60+
<parent>
61+
<groupId>com.acme</groupId>
62+
<artifactId>dummy</artifactId>
63+
<version>0.0.1-SNAPSHOT</version>
64+
<relativePath>../</relativePath>
65+
</parent>
66+
<artifactId>pom1</artifactId>
67+
</project>
68+
""";
69+
70+
@Language("xml")
71+
String pom2 =
72+
"""
73+
<?xml version="1.0" encoding="UTF-8"?>
74+
<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">
75+
<modelVersion>4.0.0</modelVersion>
76+
<parent>
77+
<groupId>com.acme</groupId>
78+
<artifactId>dummy</artifactId>
79+
<version>0.0.1-SNAPSHOT</version>
80+
<relativePath>../</relativePath>
81+
</parent>
82+
<artifactId>pom2</artifactId>
83+
<dependencies>
84+
<dependency>
85+
<groupId>com.acme</groupId>
86+
<artifactId>pom1</artifactId>
87+
<version>0.0.1-SNAPSHOT</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>javax.validation</groupId>
91+
<artifactId>validation-api</artifactId>
92+
<version>2.0.1.Final</version>
93+
</dependency>
94+
</dependencies>
95+
</project>
96+
""";
97+
98+
ClasspathRegistry sut = ClasspathRegistry.getInstance();
99+
sut.clear();
100+
101+
assertThat(sut.getCurrentDependencies()).isEmpty();
102+
assertThat(sut.getInitialDependencies()).isEmpty();
103+
104+
List<Xml.Document> poms = new RewriteMavenParser().parse(parentPom, pom1, pom2);
105+
106+
Set<ResolvedDependency> resolvedDependencies = poms
107+
.get(2)
108+
.getMarkers()
109+
.findFirst(MavenResolutionResult.class)
110+
.get()
111+
.getDependencies()
112+
.get(Scope.Compile)
113+
.stream()
114+
.collect(Collectors.toSet());
115+
116+
ClasspathRegistry registry = ClasspathRegistry.initialize(resolvedDependencies);
117+
assertThat(registry.getCurrentDependencies()).hasSize(1);
118+
assertThat(registry.getInitialDependencies()).hasSize(1);
119+
}
120+
}

0 commit comments

Comments
 (0)