Skip to content

Commit 020ebcf

Browse files
ammachadotimtebeek
andauthored
Fix download snapshot warnings (#882)
* [WIP] Fix download snapshot warnings * Fix download snapshot warnings * Inline `sourceSet` function * Remove (now) unused gav parameter * Define a unit test that triggers the SNAPSHOT handling * Update src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java Co-authored-by: Tim te Beek <[email protected]> * Tidy up --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]>
1 parent 3a23f61 commit 020ebcf

File tree

5 files changed

+102
-32
lines changed

5 files changed

+102
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ pom.xml.versionsBackup
99
.classpath
1010
.project
1111
.settings
12+
.vscode/

src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected Set<String> getExclusions() {
111111
private LinkedHashSet<String> plainTextMasks;
112112

113113
/**
114-
* Allows to add additional plain text masks without overriding
114+
* Allows adding additional plain text masks without overriding
115115
* the defaults.
116116
*/
117117
@Nullable
@@ -169,7 +169,7 @@ protected Set<String> getPlainTextMasks() {
169169
* Whether to throw an exception if an activeRecipe fails configuration validation.
170170
* This may happen if the activeRecipe is improperly configured, or any downstream recipes are improperly configured.
171171
* <p>
172-
* For the time, this default is "false" to prevent one improperly recipe from failing the build.
172+
* For the time, this default is "false" to prevent one improper recipe from failing the build.
173173
* In the future, this default may be changed to "true" to be more restrictive.
174174
*/
175175
@Parameter(property = "rewrite.failOnInvalidActiveRecipes", alias = "failOnInvalidActiveRecipes", defaultValue = "false")

src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@
4747
import org.openrewrite.marker.ci.BuildEnvironment;
4848
import org.openrewrite.maven.cache.InMemoryMavenPomCache;
4949
import org.openrewrite.maven.cache.MavenPomCache;
50+
import org.openrewrite.maven.internal.RawPom;
5051
import org.openrewrite.maven.internal.RawRepositories;
52+
import org.openrewrite.maven.tree.Pom;
5153
import org.openrewrite.maven.tree.ProfileActivation;
54+
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
5255
import org.openrewrite.maven.utilities.MavenWrapper;
5356
import org.openrewrite.style.NamedStyles;
5457
import org.openrewrite.xml.tree.Xml;
5558

5659
import java.io.File;
5760
import java.io.IOException;
61+
import java.io.InputStream;
5862
import java.nio.charset.Charset;
5963
import java.nio.file.*;
6064
import java.nio.file.attribute.BasicFileAttributes;
@@ -88,6 +92,7 @@ public class MavenMojoProjectParser {
8892

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

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

218223
private static Optional<Charset> getCharset(MavenProject mavenProject) {
219-
String compilerPluginKey = "org.apache.maven.plugins:maven-compiler-plugin";
224+
String compilerPluginKey = MAVEN_COMPILER_PLUGIN;
220225
Plugin plugin = Optional.ofNullable(mavenProject.getPlugin(compilerPluginKey))
221226
.orElseGet(() -> mavenProject.getPluginManagement().getPluginsAsMap().get(compilerPluginKey));
222227
if (plugin != null && plugin.getConfiguration() instanceof Xpp3Dom) {
@@ -267,7 +272,7 @@ private static JavaVersion getSrcMainJavaVersion(MavenProject mavenProject) {
267272
String sourceCompatibility = null;
268273
String targetCompatibility = null;
269274

270-
Plugin compilerPlugin = mavenProject.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
275+
Plugin compilerPlugin = mavenProject.getPlugin(MAVEN_COMPILER_PLUGIN);
271276
if (compilerPlugin != null && compilerPlugin.getConfiguration() instanceof Xpp3Dom) {
272277
Xpp3Dom dom = (Xpp3Dom) compilerPlugin.getConfiguration();
273278
Xpp3Dom release = dom.getChild("release");
@@ -310,7 +315,7 @@ static JavaVersion getSrcTestJavaVersion(MavenProject mavenProject) {
310315
String sourceCompatibility = null;
311316
String targetCompatibility = null;
312317

313-
Plugin compilerPlugin = mavenProject.getPlugin("org.apache.maven.plugins:maven-compiler-plugin");
318+
Plugin compilerPlugin = mavenProject.getPlugin(MAVEN_COMPILER_PLUGIN);
314319
if (compilerPlugin != null && compilerPlugin.getConfiguration() instanceof Xpp3Dom) {
315320
Xpp3Dom dom = (Xpp3Dom) compilerPlugin.getConfiguration();
316321
Xpp3Dom release = dom.getChild("testRelease");
@@ -417,7 +422,7 @@ private Stream<SourceFile> processMainSources(
417422
}
418423

419424
List<Marker> mainProjectProvenance = new ArrayList<>(projectProvenance);
420-
mainProjectProvenance.add(sourceSet("main", dependencies, typeCache));
425+
mainProjectProvenance.add(JavaSourceSet.build("main", dependencies));
421426
mainProjectProvenance.add(getSrcMainJavaVersion(mavenProject));
422427

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

476481
List<Marker> markers = new ArrayList<>(projectProvenance);
477-
markers.add(sourceSet("test", testDependencies, typeCache));
482+
markers.add(JavaSourceSet.build("test", testDependencies));
478483
markers.add(getSrcTestJavaVersion(mavenProject));
479484

480485
// Any resources parsed from "test/resources" should also have the test source set added to them.
@@ -506,10 +511,6 @@ private Stream<SourceFile> processTestSources(
506511
return null;
507512
}
508513

509-
private static JavaSourceSet sourceSet(String name, List<Path> dependencies, JavaTypeCache typeCache) {
510-
return JavaSourceSet.build(name, dependencies);
511-
}
512-
513514
public Xml.@Nullable Document parseMaven(MavenProject mavenProject, List<Marker> projectProvenance, ExecutionContext ctx) throws MojoFailureException {
514515
return parseMaven(singletonList(mavenProject), singletonMap(mavenProject, projectProvenance), ctx).get(mavenProject);
515516
}
@@ -520,30 +521,29 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
520521
return emptyMap();
521522
}
522523

523-
MavenProject topLevelProject = mavenSession.getTopLevelProject();
524-
logInfo(topLevelProject, "Resolving Poms...");
525-
526-
Set<Path> allPoms = new LinkedHashSet<>();
527-
mavenProjects.forEach(p -> collectPoms(p, allPoms));
528-
for (MavenProject mavenProject : mavenProjects) {
529-
mavenSession.getProjectDependencyGraph().getUpstreamProjects(mavenProject, true).forEach(p -> collectPoms(p, allPoms));
530-
}
531-
MavenParser.Builder mavenParserBuilder = MavenParser.builder().mavenConfig(baseDir.resolve(MVN_MAVEN_CONFIG));
532-
533524
MavenSettings settings = buildSettings();
534525
MavenExecutionContextView mavenExecutionContext = MavenExecutionContextView.view(ctx);
535526
mavenExecutionContext.setMavenSettings(settings);
536527
mavenExecutionContext.setResolutionListener(new MavenLoggingResolutionEventListener(logger));
537528

538-
if (pomCacheEnabled) {
539-
//The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
540-
//If the flag is set to false, only the default, in-memory cache is used.
541-
mavenExecutionContext.setPomCache(getPomCache(pomCacheDirectory, logger));
529+
// The default pom cache is enabled as a two-layer cache L1 == in-memory and L2 == RocksDb
530+
// If the flag is set to false, only the default, in-memory cache is used.
531+
MavenPomCache pomCache = pomCacheEnabled ? getPomCache(pomCacheDirectory, logger) : mavenExecutionContext.getPomCache();
532+
mavenExecutionContext.setPomCache(pomCache);
533+
534+
MavenProject topLevelProject = mavenSession.getTopLevelProject();
535+
logInfo(topLevelProject, "Resolving Poms...");
536+
537+
Set<Path> allPoms = new LinkedHashSet<>();
538+
mavenProjects.forEach(p -> collectPoms(p, allPoms, mavenExecutionContext));
539+
for (MavenProject mavenProject : mavenProjects) {
540+
mavenSession.getProjectDependencyGraph().getUpstreamProjects(mavenProject, true).forEach(p -> collectPoms(p, allPoms, mavenExecutionContext));
542541
}
543542

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

549549
List<SourceFile> mavens = mavenParserBuilder.build()
@@ -611,16 +611,22 @@ public Map<MavenProject, Xml.Document> parseMaven(List<MavenProject> mavenProjec
611611
*
612612
* @param project A maven project to examine for any children/parent poms.
613613
* @param paths A list of paths to poms that have been collected so far.
614+
* @param ctx The execution context for the current project.
614615
*/
615-
private void collectPoms(MavenProject project, Set<Path> paths) {
616-
paths.add(pomPath(project));
616+
private void collectPoms(MavenProject project, Set<Path> paths, MavenExecutionContextView ctx) {
617+
if (!paths.add(pomPath(project))) {
618+
return;
619+
}
620+
621+
ResolvedGroupArtifactVersion gav = createResolvedGAV(project, ctx);
622+
ctx.getPomCache().putPom(gav, createPom(project));
617623

618624
// children
619625
if (project.getCollectedProjects() != null) {
620626
for (MavenProject child : project.getCollectedProjects()) {
621627
Path path = pomPath(child);
622628
if (!paths.contains(path)) {
623-
collectPoms(child, paths);
629+
collectPoms(child, paths, ctx);
624630
}
625631
}
626632
}
@@ -629,7 +635,7 @@ private void collectPoms(MavenProject project, Set<Path> paths) {
629635
while (parent != null && parent.getFile() != null) {
630636
Path path = pomPath(parent);
631637
if (!paths.contains(path)) {
632-
collectPoms(parent, paths);
638+
collectPoms(parent, paths, ctx);
633639
}
634640
parent = parent.getParent();
635641
}
@@ -644,7 +650,7 @@ private static Path pomPath(MavenProject mavenProject) {
644650
// org.eclipse.tycho:tycho-packaging-plugin:update-consumer-pom produces a synthetic pom
645651
if (pomPath.endsWith(".tycho-consumer-pom.xml")) {
646652
Path normalPom = mavenProject.getBasedir().toPath().resolve("pom.xml");
647-
// check for existence of the POM, since Tycho can work pom-less
653+
// check for the existence of the POM, since Tycho can work pom-less
648654
if (Files.isReadable(normalPom) && Files.isRegularFile(normalPom)) {
649655
return normalPom;
650656
}
@@ -807,4 +813,24 @@ private void logDebug(MavenProject mavenProject, String message) {
807813
private static <E extends Throwable> E sneakyThrow(Throwable e) throws E {
808814
return (E) e;
809815
}
816+
817+
private static ResolvedGroupArtifactVersion createResolvedGAV(MavenProject project, MavenExecutionContextView ctx) {
818+
return new ResolvedGroupArtifactVersion(
819+
ctx.getLocalRepository().getUri(),
820+
project.getGroupId(),
821+
project.getArtifactId(),
822+
project.getVersion(),
823+
project.getVersion().endsWith("-SNAPSHOT") ? null : project.getVersion()
824+
);
825+
}
826+
827+
private static @Nullable Pom createPom(MavenProject project) {
828+
Path pomPath = project.getFile().toPath();
829+
try (InputStream is = Files.newInputStream(pomPath)) {
830+
RawPom rawPom = RawPom.parse(is, null);
831+
return rawPom.toPom(project.getBasedir().toPath().relativize(pomPath), null);
832+
} catch (IOException e) {
833+
return null;
834+
}
835+
}
810836
}

src/test/java/org/openrewrite/maven/BasicIT.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,15 @@ void resolves_settings(MavenExecutionResult result) {
8080
.allSatisfy(line -> assertThat(line).doesNotContain("Illegal character in path at index 1"));
8181
}
8282

83+
@MavenTest
84+
@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:dryRun")
85+
void snapshot_ok(MavenExecutionResult result) {
86+
assertThat(result)
87+
.isSuccessful()
88+
.out()
89+
.warn()
90+
.isEmpty();
91+
assertThat(result).out().info().contains("Running recipe(s)...");
92+
}
93+
8394
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.openrewrite.maven</groupId>
7+
<artifactId>snapshot_ok</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>BasicIT#snapshot_ok</name>
11+
12+
<properties>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>@project.groupId@</groupId>
22+
<artifactId>@project.artifactId@</artifactId>
23+
<version>@project.version@</version>
24+
<configuration>
25+
<activeRecipes>
26+
<recipe>org.openrewrite.java.format.AutoFormat</recipe>
27+
</activeRecipes>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
</project>

0 commit comments

Comments
 (0)