Skip to content

Commit

Permalink
Consistent lifecycle of PROCESS_TEST_CLASSES, polish
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jul 15, 2023
1 parent 1600c14 commit 220e46b
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 34 deletions.
6 changes: 1 addition & 5 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,18 @@

public abstract class AbstractRewriteMojo extends ConfigurableRewriteMojo {

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(defaultValue = "${project}", readonly = true, required = true)
protected MavenProject project;

@SuppressWarnings("NotNullFieldNotInitialized")
@Component
protected RuntimeInformation runtime;

@SuppressWarnings("NotNullFieldNotInitialized")
@Component
protected SettingsDecrypter settingsDecrypter;

@SuppressWarnings("NotNullFieldNotInitialized")
@Component
protected RepositorySystem repositorySystem;

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession mavenSession;

Expand All @@ -102,6 +97,7 @@ Config getConfig() throws IOException {
URI uri = new URI(configLocation);
if (uri.getScheme() != null && uri.getScheme().startsWith("http")) {
HttpSender httpSender = new HttpUrlConnectionSender();
//noinspection resource
return new Config(httpSender.get(configLocation).send().getBody(), uri);
}
} catch (URISyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ private static void writeAfter(Path root, Result result, ExecutionContext ctx) {
} catch (IOException e) {
throw new UncheckedIOException("Unable to rewrite source files", e);
}
} else if (result.getAfter() instanceof Quark) {
} else if (!(result.getAfter() instanceof Quark)) {
// Don't attempt to write to a Quark; it has already been logged as change that has been made
} else {
Charset charset = result.getAfter().getCharset() == null ? StandardCharsets.UTF_8 : result.getAfter().getCharset();
try (BufferedWriter sourceFileWriter = Files.newBufferedWriter(targetPath, charset)) {
sourceFileWriter.write(result.getAfter().printAll(new PrintOutputCapture<>(0, new SanitizedMarkerPrinter())));
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/openrewrite/maven/ArtifactResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@

public class ArtifactResolver {
private final RepositorySystem repositorySystem;

private final RepositorySystemSession repositorySystemSession;

private final List<RemoteRepository> remoteRepositories;

public ArtifactResolver(RepositorySystem repositorySystem, MavenSession session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
@SuppressWarnings("FieldMayBeFinal")
public abstract class ConfigurableRewriteMojo extends AbstractMojo {

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(property = "rewrite.configLocation", alias = "configLocation", defaultValue = "${maven.multiModuleProjectDirectory}/rewrite.yml")
protected String configLocation;

Expand Down Expand Up @@ -193,6 +192,7 @@ protected Set<String> getActiveRecipes() {
}
}

//noinspection ConstantConditions
return computedRecipes;
}

Expand All @@ -209,6 +209,7 @@ protected Set<String> getActiveStyles() {
}
}

//noinspection ConstantConditions
return computedStyles;
}

Expand Down Expand Up @@ -256,6 +257,7 @@ protected Set<String> getRecipeArtifactCoordinates() {
}
}

//noinspection ConstantConditions
return computedRecipeArtifactCoordinates;
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/openrewrite/maven/ConfigureMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@
@SuppressWarnings("unused")
public class ConfigureMojo extends AbstractRewriteMojo {

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(property = "groupId", defaultValue = "org.openrewrite.maven")
protected String groupId;

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(property = "artifactId", defaultValue = "rewrite-maven-plugin")
protected String artifactId;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/openrewrite/maven/CycloneDxBomMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.xml.tree.Xml;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
Expand All @@ -35,11 +36,10 @@
* Generate a CycloneDx bill of materials outlining the project's dependencies, including transitive dependencies.
*/
@Mojo(name = "cyclonedx", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true,
defaultPhase = LifecyclePhase.PACKAGE)
defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES)
@SuppressWarnings("unused")
public class CycloneDxBomMojo extends AbstractRewriteMojo {

@SuppressWarnings("NotNullFieldNotInitialized")
@Component
private MavenProjectHelper projectHelper;

Expand All @@ -66,7 +66,8 @@ private File buildCycloneDxBom(Xml.Document pomAst) {
cycloneDxBom.getParentFile().mkdirs();

Files.write(cycloneDxBom.toPath(), PrintMavenAsCycloneDxBom.print(pomAst)
.getBytes(pomAst.getCharset()));
.getBytes(pomAst.getCharset() == null ?
StandardCharsets.UTF_8 : pomAst.getCharset()));

return cycloneDxBom;
} catch (Throwable t) {
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/openrewrite/maven/InitMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@
@SuppressWarnings("unused")
public class InitMojo extends AbstractRewriteMojo {

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(property = "groupId", defaultValue = "org.openrewrite.maven")
protected String groupId;

@SuppressWarnings("NotNullFieldNotInitialized")
@Parameter(property = "artifactId", defaultValue = "rewrite-maven-plugin")
protected String artifactId;

Expand Down Expand Up @@ -140,10 +138,10 @@ protected String getConfiguration() {
Set<String> activeRecipes = getActiveRecipes();
if (configuration == null && !activeRecipes.isEmpty()) {
configuration = "<configuration>\n<activeRecipes>\n" +
activeRecipes.stream()
.map(it -> "<recipe>" + it + "</recipe>")
.collect(Collectors.joining("\n"))
+ "</activeRecipes>\n</configuration>";
activeRecipes.stream()
.map(it -> "<recipe>" + it + "</recipe>")
.collect(Collectors.joining("\n"))
+ "</activeRecipes>\n</configuration>";
}
return configuration;
}
Expand All @@ -153,12 +151,12 @@ protected String getExecutions() {
String executions = null;
if (executionPhase != null && executionGoals != null) {
executions = "<executions>\n<execution>\n" +
"<phase>" + executionPhase + "</phase>\n"
+ "<goals>\n"
+ Arrays.stream(executionGoals.split(","))
.map(it -> "<goal>" + it + "</goal>")
.collect(Collectors.joining("\n"))
+ "\n</goals>\n</execution>\n</executions>";
"<phase>" + executionPhase + "</phase>\n"
+ "<goals>\n"
+ Arrays.stream(executionGoals.split(","))
.map(it -> "<goal>" + it + "</goal>")
.collect(Collectors.joining("\n"))
+ "\n</goals>\n</execution>\n</executions>";
}
return executions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/maven/RewriteDryRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* Generate warnings to the console for any recipe that would make changes, but do not make changes.
*
* <p/>
* This variant of rewrite:dryRun will fork the maven life cycle and can be run as a "stand-alone" goal. It will
* execute the maven build up to the process-test-classes phase.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* Generate warnings to the console for any recipe that would make changes, but do not make changes.
*
* <p/>
* This variant of rewrite:dryRun will not fork the maven life cycle and can be used (along with other goals) without
* triggering repeated life-cycle events. It will execute the maven build up to the process-test-classes phase.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/openrewrite/maven/RewriteRunMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@

/**
* Run the configured recipes and apply the changes locally.
*
* <p/>
* This variant of rewrite:run will fork the maven life cycle and can be run as a "stand-alone" goal. It will
* execute the maven build up to the process-test-classes phase.
*/
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true,
defaultPhase = LifecyclePhase.COMPILE)
@Execute(phase = LifecyclePhase.COMPILE)
defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES)
@Execute(phase = LifecyclePhase.PROCESS_TEST_CLASSES)
public class RewriteRunMojo extends AbstractRewriteRunMojo {
}
4 changes: 2 additions & 2 deletions src/main/java/org/openrewrite/maven/RewriteRunNoForkMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

/**
* Run the configured recipes and apply the changes locally.
*
* <p/>
* This variant of rewrite:run will not fork the maven life cycle and can be used (along with other goals) without
* triggering repeated life-cycle events.
*/
@Mojo(name = "runNoFork", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true,
defaultPhase = LifecyclePhase.COMPILE)
defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES)
public class RewriteRunNoForkMojo extends AbstractRewriteRunMojo {
}

0 comments on commit 220e46b

Please sign in to comment.