Skip to content

Commit

Permalink
[WIP] Fix download snapshot warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Oct 27, 2024
1 parent 524cadb commit fca4935
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected Set<String> getExclusions() {
private LinkedHashSet<String> plainTextMasks;

/**
* Allows to add additional plain text masks without overriding
* Allows adding additional plain text masks without overriding
* the defaults.
*/
@Nullable
Expand Down Expand Up @@ -171,7 +171,7 @@ protected Set<String> getPlainTextMasks() {
* Whether to throw an exception if an activeRecipe fails configuration validation.
* This may happen if the activeRecipe is improperly configured, or any downstream recipes are improperly configured.
* <p>
* For the time, this default is "false" to prevent one improperly recipe from failing the build.
* For the time, this default is "false" to prevent one improper recipe from failing the build.
* In the future, this default may be changed to "true" to be more restrictive.
*/
@Parameter(property = "rewrite.failOnInvalidActiveRecipes", alias = "failOnInvalidActiveRecipes", defaultValue = "false")
Expand Down
74 changes: 48 additions & 26 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
import org.openrewrite.maven.cache.MavenPomCache;
import org.openrewrite.maven.internal.RawRepositories;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ProfileActivation;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
import org.openrewrite.maven.utilities.MavenWrapper;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.xml.tree.Xml;
Expand Down Expand Up @@ -88,6 +90,7 @@ public class MavenMojoProjectParser {

private static final String MVN_JVM_CONFIG = ".mvn/jvm.config";
private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config";
private static final String MAVEN_COMPILER_PLUGIN = "org.apache.maven.plugins:maven-compiler-plugin";

@Nullable
public static MavenPomCache POM_CACHE;
Expand Down Expand Up @@ -137,7 +140,7 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, List<NamedS
Xml.Document maven = parseMaven(mavenProject, projectProvenance, ctx);
return listSourceFiles(mavenProject, maven, projectProvenance, styles, ctx);
} else {
//If running across all project, iterate and parse source files from each project
//If running across all projects, iterate and parse source files from each project
Map<MavenProject, List<Marker>> projectProvenances = mavenSession.getProjects().stream()
.collect(Collectors.toMap(Function.identity(), this::generateProvenance));
Map<MavenProject, Xml.Document> projectMap = parseMaven(mavenSession.getProjects(), projectProvenances, ctx);
Expand Down Expand Up @@ -216,7 +219,7 @@ public Stream<SourceFile> listSourceFiles(MavenProject mavenProject, Xml.@Nullab
}

private static Optional<Charset> getCharset(MavenProject mavenProject) {
String compilerPluginKey = "org.apache.maven.plugins:maven-compiler-plugin";
String compilerPluginKey = MAVEN_COMPILER_PLUGIN;
Plugin plugin = Optional.ofNullable(mavenProject.getPlugin(compilerPluginKey))
.orElseGet(() -> mavenProject.getPluginManagement().getPluginsAsMap().get(compilerPluginKey));
if (plugin != null && plugin.getConfiguration() instanceof Xpp3Dom) {
Expand Down Expand Up @@ -267,7 +270,7 @@ private static JavaVersion getSrcMainJavaVersion(MavenProject mavenProject) {
String sourceCompatibility = null;
String targetCompatibility = null;

Plugin compilerPlugin = mavenProject.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
Plugin compilerPlugin = mavenProject.getPlugin(MAVEN_COMPILER_PLUGIN);
if (compilerPlugin != null && compilerPlugin.getConfiguration() instanceof Xpp3Dom) {
Xpp3Dom dom = (Xpp3Dom) compilerPlugin.getConfiguration();
Xpp3Dom release = dom.getChild("release");
Expand Down Expand Up @@ -310,7 +313,7 @@ static JavaVersion getSrcTestJavaVersion(MavenProject mavenProject) {
String sourceCompatibility = null;
String targetCompatibility = null;

Plugin compilerPlugin = mavenProject.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
Plugin compilerPlugin = mavenProject.getPlugin(MAVEN_COMPILER_PLUGIN);
if (compilerPlugin != null && compilerPlugin.getConfiguration() instanceof Xpp3Dom) {
Xpp3Dom dom = (Xpp3Dom) compilerPlugin.getConfiguration();
Xpp3Dom release = dom.getChild("testRelease");
Expand Down Expand Up @@ -417,7 +420,7 @@ private Stream<SourceFile> processMainSources(
}

List<Marker> mainProjectProvenance = new ArrayList<>(projectProvenance);
mainProjectProvenance.add(sourceSet("main", dependencies, typeCache));
mainProjectProvenance.add(sourceSet("main", dependencies));
mainProjectProvenance.add(getSrcMainJavaVersion(mavenProject));

//Filter out any generated source files from the returned list, as we do not want to apply the recipe to the
Expand Down Expand Up @@ -474,7 +477,7 @@ private Stream<SourceFile> processTestSources(
}

List<Marker> markers = new ArrayList<>(projectProvenance);
markers.add(sourceSet("test", testDependencies, typeCache));
markers.add(sourceSet("test", testDependencies));
markers.add(getSrcTestJavaVersion(mavenProject));

// Any resources parsed from "test/resources" should also have the test source set added to them.
Expand Down Expand Up @@ -506,7 +509,7 @@ private Stream<SourceFile> processTestSources(
return null;
}

private static JavaSourceSet sourceSet(String name, List<Path> dependencies, JavaTypeCache typeCache) {
private static JavaSourceSet sourceSet(String name, List<Path> dependencies) {
return JavaSourceSet.build(name, dependencies);
}

Expand All @@ -520,30 +523,29 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
return emptyMap();
}

MavenProject topLevelProject = mavenSession.getTopLevelProject();
logInfo(topLevelProject, "Resolving Poms...");

Set<Path> allPoms = new LinkedHashSet<>();
mavenProjects.forEach(p -> collectPoms(p, allPoms));
for (MavenProject mavenProject : mavenProjects) {
mavenSession.getProjectDependencyGraph().getUpstreamProjects(mavenProject, true).forEach(p -> collectPoms(p, allPoms));
}
MavenParser.Builder mavenParserBuilder = MavenParser.builder().mavenConfig(baseDir.resolve(MVN_MAVEN_CONFIG));

MavenSettings settings = buildSettings();
MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView.view(ctx);
mavenExecutionContext.setMavenSettings(settings);
mavenExecutionContext.setResolutionListener(new MavenLoggingResolutionEventListener(logger));

if (pomCacheEnabled) {
//The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
//If the flag is set to false, only the default, in-memory cache is used.
mavenExecutionContext.setPomCache(getPomCache(pomCacheDirectory, logger));
// The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
// If the flag is set to false, only the default, in-memory cache is used.
MavenPomCache pomCache = pomCacheEnabled ? getPomCache(pomCacheDirectory, logger) : mavenExecutionContext.getPomCache();
mavenExecutionContext.setPomCache(pomCache);

MavenProject topLevelProject = mavenSession.getTopLevelProject();
logInfo(topLevelProject, "Resolving Poms...");

Set<Path> allPoms = new LinkedHashSet<>();
mavenProjects.forEach(p -> collectPoms(p, allPoms, mavenExecutionContext));
for (MavenProject mavenProject : mavenProjects) {
mavenSession.getProjectDependencyGraph().getUpstreamProjects(mavenProject, true).forEach(p -> collectPoms(p, allPoms, mavenExecutionContext));
}

MavenParser.Builder mavenParserBuilder = MavenParser.builder().mavenConfig(baseDir.resolve(MVN_MAVEN_CONFIG));
List<String> activeProfiles = topLevelProject.getActiveProfiles().stream().map(Profile::getId).collect(toList());
if (!activeProfiles.isEmpty()) {
mavenParserBuilder.activeProfiles(activeProfiles.toArray(new String[]{}));
mavenParserBuilder.activeProfiles(activeProfiles.toArray(new String[0]));
}

List<SourceFile> mavens = mavenParserBuilder.build()
Expand Down Expand Up @@ -612,15 +614,17 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
* @param project A maven project to examine for any children/parent poms.
* @param paths A list of paths to poms that have been collected so far.
*/
private void collectPoms(MavenProject project, Set<Path> paths) {
private void collectPoms(MavenProject project, Set<Path> paths, MavenExecutionContextView ctx) {
paths.add(pomPath(project));
ResolvedGroupArtifactVersion gav = createResolvedGAV(project, ctx);
ctx.getPomCache().putPom(gav, createPom(gav, project));

// children
if (project.getCollectedProjects() != null) {
for (MavenProject child : project.getCollectedProjects()) {
Path path = pomPath(child);
if (!paths.contains(path)) {
collectPoms(child, paths);
collectPoms(child, paths, ctx);
}
}
}
Expand All @@ -629,7 +633,7 @@ private void collectPoms(MavenProject project, Set<Path> paths) {
while (parent != null && parent.getFile() != null) {
Path path = pomPath(parent);
if (!paths.contains(path)) {
collectPoms(parent, paths);
collectPoms(parent, paths, ctx);
}
parent = parent.getParent();
}
Expand All @@ -644,7 +648,7 @@ private static Path pomPath(MavenProject mavenProject) {
// org.eclipse.tycho:tycho-packaging-plugin:update-consumer-pom produces a synthetic pom
if (pomPath.endsWith(".tycho-consumer-pom.xml")) {
Path normalPom = mavenProject.getBasedir().toPath().resolve("pom.xml");
// check for existence of the POM, since Tycho can work pom-less
// check for the existence of the POM, since Tycho can work pom-less
if (Files.isReadable(normalPom) && Files.isRegularFile(normalPom)) {
return normalPom;
}
Expand Down Expand Up @@ -807,4 +811,22 @@ private void logDebug(MavenProject mavenProject, String message) {
private static <E extends Throwable> E sneakyThrow(Throwable e) throws E {
return (E) e;
}

private static ResolvedGroupArtifactVersion createResolvedGAV(MavenProject project, MavenExecutionContextView ctx) {
return new ResolvedGroupArtifactVersion(
ctx.getLocalRepository().getUri(),
project.getGroupId(),
project.getArtifactId(),
project.getVersion(),
project.getVersion().endsWith("-SNAPSHOT") ? null : project.getVersion()
);
}

private static Pom createPom(ResolvedGroupArtifactVersion gav, MavenProject project) {
Pom.PomBuilder builder = Pom.builder()
.gav(gav)
;
// TODO: complete mapping
return builder.build();
}
}

0 comments on commit fca4935

Please sign in to comment.