Skip to content

Support for .mvn/maven.config #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public String getDisplayName() {

private final RewriteExecutionContext executionContext;

public OpenRewriteMavenBuildFile(Path absoluteProjectPath, Xml.Document sourceFile, ApplicationEventPublisher eventPublisher, RewriteExecutionContext executionContext) {

public OpenRewriteMavenBuildFile(Path absoluteProjectPath,
Xml.Document sourceFile,
ApplicationEventPublisher eventPublisher,
RewriteExecutionContext executionContext) {
super(absoluteProjectPath, sourceFile);
this.eventPublisher = eventPublisher;
this.executionContext = executionContext;
Expand Down Expand Up @@ -264,14 +268,25 @@ public List<Dependency> getRequestedDependencies() {
.map(d -> mapDependency(d))
.map(d -> {
if(d.getType() == null || d.getClassifier() == null || d.getVersion() == null) {
List<ResolvedDependency> dependencies = getPom().findDependencies(d.getGroupId(), d.getArtifactId(),
d.getScope() != null ? Scope.fromName(d.getScope()) : null);

String groupId = evaluate(d.getGroupId());
String artifactId = evaluate(d.getArtifactId());
String version = evaluate(d.getVersion());

List<ResolvedDependency> dependencies = getPom().findDependencies(
groupId,
artifactId,
d.getScope() != null ? Scope.fromName(d.getScope()) : null
);
ResolvedDependency resolvedDependency = dependencies.get(0);
d.setVersion(resolvedDependency.getVersion());
d.setClassifier(resolvedDependency.getClassifier());
d.setType(resolvedDependency.getType());
d.setArtifactId(artifactId);
d.setGroupId(groupId);

if(d.getScope() == null ) {
String s = resolveScope(d.getGroupId(), d.getArtifactId(), d.getType(), d.getClassifier());
String s = resolveScope(groupId, artifactId, d.getType(), d.getClassifier());
d.setScope(s);
}
}
Expand Down Expand Up @@ -351,19 +366,6 @@ private String resolveScope(String groupId, String artifactId, @Nullable String
return managedScope != null ? managedScope.name().toLowerCase() : null;
}

private String calculateVersion(org.openrewrite.maven.tree.Dependency d) {
String version = null;
if (d.getVersion() != null && !d.getVersion().startsWith("${")) {
version = d.getVersion();
} else {
String managedVersion = getPom().getPom().getManagedVersion(d.getGroupId(), d.getArtifactId(), null, null);
if (managedVersion != null) {
version = managedVersion;
}
}
return version;
}

private org.springframework.sbm.build.api.Dependency mapDependency(Scope scope, ResolvedDependency d) {
return new Dependency(
d.getGroupId(),
Expand Down Expand Up @@ -617,9 +619,8 @@ public String getArtifactId() {

@Override
public String getVersion() {
return getPom().getPom().getVersion();
return evaluate(getPom().getPom().getVersion());
}

@Override
public String getCoordinates() {
return getGroupId() + ":" + getArtifactId() + ":" + getVersion();
Expand Down Expand Up @@ -796,4 +797,8 @@ public void removePlugins(String... coordinates) {
}
}


private String evaluate(String expression) {
return getPom().getPom().getValue(expression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import freemarker.template.Template;
import lombok.Setter;
import org.openrewrite.Parser;
import org.openrewrite.java.JavaParser;
import org.openrewrite.xml.tree.Xml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.sbm.build.impl.OpenRewriteMavenBuildFile;
Expand Down Expand Up @@ -65,7 +64,10 @@ public void apply(ProjectContext context) {
RewriteMavenParser rewriteMavenParser = new RewriteMavenParser();
Parser.Input input = new Parser.Input(Path.of("pom.xml"), () -> new ByteArrayInputStream(src.getBytes(StandardCharsets.UTF_8)));
Xml.Document maven = rewriteMavenParser.parseInputs(List.of(input), null, new RewriteExecutionContext(getEventPublisher())).get(0);
OpenRewriteMavenBuildFile rewriteMavenBuildFile = new OpenRewriteMavenBuildFile(context.getProjectRootDirectory(), maven, getEventPublisher(), new RewriteExecutionContext(getEventPublisher()));
OpenRewriteMavenBuildFile rewriteMavenBuildFile = new OpenRewriteMavenBuildFile(
context.getProjectRootDirectory(),
maven, getEventPublisher(), new RewriteExecutionContext(getEventPublisher())
);
context.getProjectResources().add(rewriteMavenBuildFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public class BuildFileResourceWrapper implements ProjectResourceWrapper<OpenRewriteMavenBuildFile> {

private final ApplicationEventPublisher eventPublisher;
private final JavaParser javaParser;

@Override
public boolean shouldHandle(RewriteSourceFileHolder<? extends SourceFile> rewriteSourceFileHolder) {
Expand All @@ -43,7 +42,12 @@ public boolean shouldHandle(RewriteSourceFileHolder<? extends SourceFile> rewrit
@Override
public OpenRewriteMavenBuildFile wrapRewriteSourceFileHolder(RewriteSourceFileHolder<? extends SourceFile> rewriteSourceFileHolder) {
Xml.Document maven = (Xml.Document) rewriteSourceFileHolder.getSourceFile();
return new OpenRewriteMavenBuildFile(rewriteSourceFileHolder.getAbsoluteProjectDir(), maven, eventPublisher, new RewriteExecutionContext(eventPublisher));

return new OpenRewriteMavenBuildFile(
rewriteSourceFileHolder.getAbsoluteProjectDir(),
maven, eventPublisher,
new RewriteExecutionContext(eventPublisher)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.sbm.project.parser;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class MavenConfigParser {
public Map<String, String> parse(List<String> mavenConfigs) {
if (mavenConfigs == null) {
return new HashMap<>();
}

Pattern envVarPattern = Pattern.compile("-D.*=.*");
return mavenConfigs
.stream()
.map(k -> Arrays.stream(k.split("\n")).collect(Collectors.toList()))
.flatMap(Collection::stream)
.filter(k -> envVarPattern.matcher(k).find())
.map(k -> k.replace("-D", ""))
.collect(Collectors.toMap(k -> k.split("=")[0], k -> k.split("=")[1]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a trim() so that the value of foo in foo = bar is 'bar' and not ' bar'

}
}
Loading