Skip to content

Commit 60fc4cd

Browse files
committed
RewriteMavenParser honors Maven settings.xml
1 parent 38dafc1 commit 60fc4cd

File tree

10 files changed

+234
-12
lines changed

10 files changed

+234
-12
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
package org.springframework.sbm.build.impl;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.maven.MavenExecutionContextView;
20+
import org.openrewrite.maven.MavenSettings;
21+
import org.springframework.stereotype.Component;
22+
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
26+
/**
27+
* @author Fabian Krüger
28+
*/
29+
@Component
30+
public class MavenSettingsInitializer {
31+
public void initializeMavenSettings(ExecutionContext executionContext) {
32+
// Read .m2/settings.xml
33+
// TODO: Add support for global Maven settings (${maven.home}/conf/settings.xml).
34+
MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext);
35+
Path mavenSettingsFile = Path.of(System.getProperty("user.home")).resolve(".m2/settings.xml");
36+
if (Files.exists(mavenSettingsFile)) {
37+
MavenSettings mavenSettings = MavenSettings.parse(mavenSettingsFile, mavenExecutionContextView);
38+
mavenExecutionContextView.setMavenSettings(mavenSettings);
39+
}
40+
}
41+
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,44 @@
1515
*/
1616
package org.springframework.sbm.build.impl;
1717

18+
import lombok.RequiredArgsConstructor;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.openrewrite.ExecutionContext;
2021
import org.openrewrite.Parser;
2122
import org.openrewrite.internal.lang.Nullable;
23+
import org.openrewrite.maven.MavenExecutionContextView;
2224
import org.openrewrite.maven.MavenParser;
25+
import org.openrewrite.maven.MavenSettings;
2326
import org.openrewrite.xml.tree.Xml;
2427
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
2528
import org.springframework.stereotype.Component;
2629

30+
import java.nio.file.Files;
2731
import java.nio.file.Path;
2832
import java.util.List;
2933
import java.util.Optional;
3034

3135
@Component
3236
public class RewriteMavenParser implements Parser<Xml.Document> {
3337

34-
private final MavenParser parser;
38+
private MavenParser parser;
39+
private final MavenSettingsInitializer mavenSettingsInitializer;
3540

36-
// FIXME: #7 This does not work for singleton Spring bean, also profiles and cache cannot be changed
37-
public RewriteMavenParser(Path projectRoot) {
38-
this.parser = initMavenParser(new RewriteExecutionContext(), Optional.ofNullable(projectRoot));
41+
public RewriteMavenParser(MavenSettingsInitializer mavenSettingsInitializer, Path projectRoot) {
42+
this.mavenSettingsInitializer = mavenSettingsInitializer;
43+
parser = initMavenParser(new RewriteExecutionContext(), Optional.ofNullable(projectRoot));
3944
}
4045

41-
public RewriteMavenParser() {
42-
this.parser = initMavenParser(new RewriteExecutionContext(), Optional.empty());
46+
public RewriteMavenParser(MavenSettingsInitializer mavenSettingsInitializer) {
47+
this.mavenSettingsInitializer = mavenSettingsInitializer;
48+
parser = initMavenParser(new RewriteExecutionContext(), Optional.empty());
4349
}
4450

4551
@NotNull
46-
private MavenParser initMavenParser(RewriteExecutionContext executionContext, Optional<Path> projectRoot) {
52+
private MavenParser initMavenParser(ExecutionContext executionContext, Optional<Path> projectRoot) {
53+
54+
mavenSettingsInitializer.initializeMavenSettings(executionContext);
55+
4756
MavenParser.Builder builder = MavenParser.builder();
4857
if(projectRoot.isPresent()) {
4958
builder.mavenConfig(projectRoot.get().resolve(".mvn/maven.config"));
@@ -68,6 +77,7 @@ public List<Xml.Document> parse(String... sources) {
6877

6978
@Override
7079
public List<Xml.Document> parseInputs(Iterable<Input> sources, @Nullable Path relativeTo, ExecutionContext ctx) {
80+
mavenSettingsInitializer.initializeMavenSettings(ctx);
7181
return parser.parseInputs(sources, relativeTo, ctx);
7282
}
7383

components/sbm-core/src/main/java/org/springframework/sbm/build/migration/actions/AddMinimalPomXml.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class AddMinimalPomXml extends AbstractAction {
4343
@Setter
4444
private Configuration configuration;
4545

46+
@Autowired
47+
private RewriteMavenParser rewriteMavenParser;
48+
4649
@Override
4750
public void apply(ProjectContext context) {
4851
String projectDir = context.getProjectRootDirectory().toString();
@@ -61,9 +64,9 @@ public void apply(ProjectContext context) {
6164
}
6265

6366
String src = writer.toString();
64-
RewriteMavenParser rewriteMavenParser = new RewriteMavenParser();
6567
Parser.Input input = new Parser.Input(Path.of("pom.xml"), () -> new ByteArrayInputStream(src.getBytes(StandardCharsets.UTF_8)));
66-
Xml.Document maven = rewriteMavenParser.parseInputs(List.of(input), null, new RewriteExecutionContext(getEventPublisher())).get(0);
68+
Xml.Document maven = rewriteMavenParser
69+
.parseInputs(List.of(input), null, new RewriteExecutionContext(getEventPublisher())).get(0);
6770
OpenRewriteMavenBuildFile rewriteMavenBuildFile = new OpenRewriteMavenBuildFile(
6871
context.getProjectRootDirectory(),
6972
maven, getEventPublisher(), new RewriteExecutionContext(getEventPublisher())
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
package org.springframework.sbm.build.impl;
17+
18+
import org.junit.jupiter.api.DisplayName;
19+
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.maven.MavenExecutionContextView;
21+
import org.openrewrite.maven.tree.MavenRepository;
22+
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
23+
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
import java.nio.file.Path;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.junit.jupiter.api.Assertions.*;
30+
31+
/**
32+
* @author Fabian Krüger
33+
*/
34+
class MavenSettingsInitializerTest {
35+
36+
@Test
37+
@DisplayName("Verify that ")
38+
void mavenParserMustAdhereToSettingsXmlTest() throws URISyntaxException {
39+
Path userHome = Path.of("./testcode/project-with-maven-settings/user-home").toAbsolutePath().normalize();
40+
String actualUserHome = System.getProperty("user.home");
41+
System.setProperty("user.home", userHome.toString());
42+
43+
RewriteExecutionContext executionContext = new RewriteExecutionContext();
44+
MavenSettingsInitializer sut = new MavenSettingsInitializer();
45+
sut.initializeMavenSettings(executionContext);
46+
MavenExecutionContextView mavenExecutionContextView = MavenExecutionContextView.view(executionContext);
47+
48+
assertThat(mavenExecutionContextView.getRepositories()).hasSize(1);
49+
MavenRepository mavenRepository = mavenExecutionContextView.getRepositories().get(0);
50+
51+
assertThat(mavenRepository.getId()).isEqualTo("central");
52+
assertThat(mavenRepository.getUri()).isEqualTo("https://jcenter.bintray.com");
53+
assertThat(mavenRepository.isReleases()).isTrue();
54+
assertThat(mavenRepository.isSnapshots()).isFalse();
55+
56+
MavenRepository localRepository = mavenExecutionContextView.getLocalRepository();
57+
assertThat(localRepository.isSnapshots()).isTrue();
58+
assertThat(localRepository.getUri()).isEqualTo("file:" + userHome + "/.m2/repository");
59+
assertThat(localRepository.isSnapshots()).isTrue();
60+
assertThat(localRepository.isKnownToExist()).isTrue();
61+
assertThat(localRepository.getUsername()).isNull();
62+
assertThat(localRepository.getPassword()).isNull();
63+
64+
System.setProperty("user.home", actualUserHome);
65+
}
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
package org.springframework.sbm.build.impl;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
import org.mockito.InjectMocks;
21+
import org.mockito.Mock;
22+
import org.mockito.junit.jupiter.MockitoExtension;
23+
import org.openrewrite.ExecutionContext;
24+
import org.springframework.sbm.build.util.PomBuilder;
25+
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
26+
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.Mockito.verify;
29+
30+
/**
31+
* @author Fabian Krüger
32+
*/
33+
@ExtendWith(MockitoExtension.class)
34+
class RewriteMavenParserTest {
35+
36+
@Mock
37+
MavenSettingsInitializer mavenSettingsInitializer;
38+
@InjectMocks
39+
RewriteMavenParser sut;
40+
41+
@Test
42+
void noExecutionContextGiven() {
43+
verify(mavenSettingsInitializer).initializeMavenSettings(any(ExecutionContext.class));
44+
}
45+
46+
@Test
47+
void customExecutionContextGiven() {
48+
String pom = PomBuilder.buiildPom("com.example:project:1.0").build();
49+
ExecutionContext ctx = new RewriteExecutionContext();
50+
sut.parse(ctx, pom);
51+
verify(mavenSettingsInitializer).initializeMavenSettings(ctx);
52+
}
53+
54+
}

components/sbm-core/src/test/java/org/springframework/sbm/java/impl/ClasspathRegistryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.maven.tree.ResolvedDependency;
2323
import org.openrewrite.maven.tree.Scope;
2424
import org.openrewrite.xml.tree.Xml;
25+
import org.springframework.sbm.build.impl.MavenSettingsInitializer;
2526
import org.springframework.sbm.build.impl.RewriteMavenParser;
2627

2728
import java.util.List;
@@ -101,7 +102,7 @@ void classpathRegistryShouldKeepOnlyExternalDependencies() {
101102
assertThat(sut.getCurrentDependencies()).isEmpty();
102103
assertThat(sut.getInitialDependencies()).isEmpty();
103104

104-
List<Xml.Document> poms = new RewriteMavenParser().parse(parentPom, pom1, pom2);
105+
List<Xml.Document> poms = new RewriteMavenParser(new MavenSettingsInitializer()).parse(parentPom, pom1, pom2);
105106

106107
Set<ResolvedDependency> resolvedDependencies = poms
107108
.get(2)

components/sbm-core/src/test/java/org/springframework/sbm/project/parser/JavaProvenanceMarkerFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openrewrite.marker.BuildTool;
2424
import org.openrewrite.marker.Marker;
2525
import org.openrewrite.xml.tree.Xml;
26+
import org.springframework.sbm.build.impl.MavenSettingsInitializer;
2627
import org.springframework.sbm.build.impl.RewriteMavenParser;
2728
import org.springframework.sbm.openrewrite.RewriteExecutionContext;
2829

@@ -63,7 +64,7 @@ void test() {
6364
"</project>";
6465

6566
Path projectDirectory = Path.of("./faked-project-dir/pom.xml");
66-
Xml.Document maven = new RewriteMavenParser().parse(pomXmlSource).get(0).withSourcePath(Path.of("pom.xml"));
67+
Xml.Document maven = new RewriteMavenParser(new MavenSettingsInitializer()).parse(pomXmlSource).get(0).withSourcePath(Path.of("pom.xml"));
6768

6869
List<Marker> javaProvenanceMarkers = sut.createJavaProvenanceMarkers(maven, projectDirectory, new RewriteExecutionContext());
6970

components/sbm-core/src/test/java/org/springframework/sbm/project/parser/ProjectContextInitializerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.context.ApplicationEventPublisher;
3030
import org.springframework.core.io.Resource;
3131
import org.springframework.core.io.ResourceLoader;
32+
import org.springframework.sbm.build.impl.MavenSettingsInitializer;
3233
import org.springframework.sbm.build.impl.RewriteMavenArtifactDownloader;
3334
import org.springframework.sbm.build.impl.RewriteMavenParser;
3435
import org.springframework.sbm.build.migration.MavenPomCacheProvider;
@@ -78,6 +79,7 @@
7879
RewritePropertiesParser.class,
7980
MavenProjectParser.class,
8081
RewriteMavenParser.class,
82+
MavenSettingsInitializer.class,
8183
RewriteXmlParser.class,
8284
ResourceHelper.class,
8385
ResourceLoader.class,

components/sbm-core/src/test/java/org/springframework/sbm/project/resource/TestProjectContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.core.annotation.Order;
2424
import org.springframework.core.io.DefaultResourceLoader;
2525
import org.springframework.core.io.Resource;
26+
import org.springframework.sbm.build.impl.MavenSettingsInitializer;
2627
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
2728
import org.springframework.sbm.build.impl.RewriteMavenArtifactDownloader;
2829
import org.springframework.sbm.build.impl.RewriteMavenParser;
@@ -551,7 +552,7 @@ private ProjectContextInitializer createProjectContextInitializer(ProjectContext
551552
new ResourceParser.ResourceFilter(),
552553
eventPublisher);
553554

554-
RewriteMavenParser mavenParser = new RewriteMavenParser();
555+
RewriteMavenParser mavenParser = new RewriteMavenParser(new MavenSettingsInitializer());
555556

556557
MavenArtifactDownloader artifactDownloader = new RewriteMavenArtifactDownloader();
557558

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<settings xsi:schemaLocation='http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd'
3+
xmlns='http://maven.apache.org/SETTINGS/1.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
4+
5+
<profiles>
6+
<profile>
7+
<activeByDefault>true</activeByDefault>
8+
<repositories>
9+
<repository>
10+
<snapshots>
11+
<enabled>false</enabled>
12+
</snapshots>
13+
<id>central</id>
14+
<name>bintray</name>
15+
<url>https://jcenter.bintray.com</url>
16+
</repository>
17+
</repositories>
18+
<pluginRepositories>
19+
<pluginRepository>
20+
<snapshots>
21+
<enabled>false</enabled>
22+
</snapshots>
23+
<id>central</id>
24+
<name>bintray-plugins</name>
25+
<url>https://jcenter.bintray.com</url>
26+
</pluginRepository>
27+
</pluginRepositories>
28+
<id>bintray</id>
29+
</profile>
30+
</profiles>
31+
<activeProfiles>
32+
<activeProfile>bintray</activeProfile>
33+
</activeProfiles>
34+
<proxies>
35+
<proxy>
36+
<id>my-proxy</id>
37+
<active>true</active>
38+
<protocol>http</protocol>
39+
<host>my-corporate-proxy-host</host>
40+
<port>80</port>
41+
</proxy>
42+
</proxies>
43+
</settings>

0 commit comments

Comments
 (0)