Skip to content

Commit

Permalink
Make sure the generate-code goal invoked from the dev goal bootstraps…
Browse files Browse the repository at this point in the history
… the app in the dev mode; Make DevMojo re-use the app model resolved as part of the generate-code goal
  • Loading branch information
aloubyansky committed Oct 3, 2021
1 parent 8073bad commit 870cfb7
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 109 deletions.
73 changes: 46 additions & 27 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import io.quarkus.maven.components.MavenVersionEnforcer;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.GACTV;
import io.quarkus.runtime.LaunchMode;

/**
* The dev mojo, that runs a quarkus app in a forked process. A background compilation process is launched and any changes are
Expand Down Expand Up @@ -512,12 +513,13 @@ private void handleAutoCompile() throws MojoExecutionException {
}

private void initToolchain() throws MojoExecutionException {
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_TOOLCHAINS_PLUGIN, "toolchain");
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_TOOLCHAINS_PLUGIN, "toolchain", Collections.emptyMap());
}

private void triggerPrepare() throws MojoExecutionException {
final PluginDescriptor pluginDescr = getPluginDescriptor();
executeIfConfigured(pluginDescr.getGroupId(), pluginDescr.getArtifactId(), QUARKUS_GENERATE_CODE_GOAL);
executeIfConfigured(pluginDescr.getGroupId(), pluginDescr.getArtifactId(), QUARKUS_GENERATE_CODE_GOAL,
Collections.singletonMap("mode", LaunchMode.DEVELOPMENT.name()));
}

private PluginDescriptor getPluginDescriptor() {
Expand All @@ -528,10 +530,12 @@ private void triggerCompile(boolean test) throws MojoExecutionException {
handleResources(test);

// compile the Kotlin sources if needed
executeIfConfigured(ORG_JETBRAINS_KOTLIN, KOTLIN_MAVEN_PLUGIN, test ? "test-compile" : "compile");
executeIfConfigured(ORG_JETBRAINS_KOTLIN, KOTLIN_MAVEN_PLUGIN, test ? "test-compile" : "compile",
Collections.emptyMap());

// Compile the Java sources if needed
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_COMPILER_PLUGIN, test ? "testCompile" : "compile");
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_COMPILER_PLUGIN, test ? "testCompile" : "compile",
Collections.emptyMap());
}

/**
Expand All @@ -542,24 +546,26 @@ private void handleResources(boolean test) throws MojoExecutionException {
if (resources.isEmpty()) {
return;
}
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_RESOURCES_PLUGIN, test ? "testResources" : "resources");
executeIfConfigured(ORG_APACHE_MAVEN_PLUGINS, MAVEN_RESOURCES_PLUGIN, test ? "testResources" : "resources",
Collections.emptyMap());
}

private void executeIfConfigured(String pluginGroupId, String pluginArtifactId, String goal) throws MojoExecutionException {
private void executeIfConfigured(String pluginGroupId, String pluginArtifactId, String goal, Map<String, String> params)
throws MojoExecutionException {
final Plugin plugin = getConfiguredPluginOrNull(pluginGroupId, pluginArtifactId);
if (!isGoalConfigured(plugin, goal)) {
return;
}
getLog().info("Invoking " + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion() + ":" + goal
+ " @ " + project.getArtifactId());
+ ") @ " + project.getArtifactId());
executeMojo(
plugin(
groupId(pluginGroupId),
artifactId(pluginArtifactId),
version(plugin.getVersion()),
plugin.getDependencies()),
goal(goal),
getPluginConfig(plugin, goal),
getPluginConfig(plugin, goal, params),
executionEnvironment(
project,
session,
Expand All @@ -578,7 +584,7 @@ public boolean isGoalConfigured(Plugin plugin, String goal) {
return false;
}

private Xpp3Dom getPluginConfig(Plugin plugin, String goal) throws MojoExecutionException {
private Xpp3Dom getPluginConfig(Plugin plugin, String goal, Map<String, String> params) throws MojoExecutionException {
Xpp3Dom mergedConfig = null;
if (!plugin.getExecutions().isEmpty()) {
for (PluginExecution exec : plugin.getExecutions()) {
Expand Down Expand Up @@ -611,6 +617,12 @@ private Xpp3Dom getPluginConfig(Plugin plugin, String goal) throws MojoExecution
}
}

for (Map.Entry<String, String> param : params.entrySet()) {
final Xpp3Dom p = new Xpp3Dom(param.getKey());
p.setValue(param.getValue());
configuration.addChild(p);
}

return configuration;
}

Expand Down Expand Up @@ -889,28 +901,35 @@ private QuarkusDevModeLauncher newLauncher() throws Exception {

setKotlinSpecificFlags(builder);

final MavenArtifactResolver.Builder resolverBuilder = MavenArtifactResolver.builder()
.setRepositorySystem(repoSystem)
.setRemoteRepositories(repos)
.setRemoteRepositoryManager(remoteRepositoryManager)
.setWorkspaceDiscovery(true);

// path to the serialized application model
final Path appModelLocation = resolveSerializedModelLocation();
// if it already exists, it may be a reload triggered by a change in a POM
// in which case we should not be using the original Maven session
boolean reinitializeMavenSession = Files.exists(appModelLocation);
if (reinitializeMavenSession) {
Files.delete(appModelLocation);

ApplicationModel appModel = bootstrapProvider
.getResolvedApplicationModel(QuarkusBootstrapProvider.getProjectId(project), LaunchMode.DEVELOPMENT);
if (appModel != null) {
bootstrapProvider.close();
} else {
// we can re-use the original Maven session
resolverBuilder.setRepositorySystemSession(repoSession);
}
final MavenArtifactResolver.Builder resolverBuilder = MavenArtifactResolver.builder()
.setRepositorySystem(repoSystem)
.setRemoteRepositories(repos)
.setRemoteRepositoryManager(remoteRepositoryManager)
.setWorkspaceDiscovery(true);

// if it already exists, it may be a reload triggered by a change in a POM
// in which case we should not be using the original Maven session
boolean reinitializeMavenSession = Files.exists(appModelLocation);
if (reinitializeMavenSession) {
Files.delete(appModelLocation);
} else {
// we can re-use the original Maven session
resolverBuilder.setRepositorySystemSession(repoSession);
}

final ApplicationModel appModel = new BootstrapAppModelResolver(resolverBuilder.build())
.setDevMode(true)
.setCollectReloadableDependencies(!noDeps)
.resolveModel(new GACTV(project.getGroupId(), project.getArtifactId(), null, "jar", project.getVersion()));
appModel = new BootstrapAppModelResolver(resolverBuilder.build())
.setDevMode(true)
.setCollectReloadableDependencies(!noDeps)
.resolveModel(new GACTV(project.getGroupId(), project.getArtifactId(), null, "jar", project.getVersion()));
}

// serialize the app model to avoid re-resolving it in the dev process
BootstrapUtils.serializeAppModel(appModel, appModelLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.runtime.LaunchMode;

@Mojo(name = "generate-code", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true)
public class GenerateCodeMojo extends QuarkusBootstrapMojo {
Expand All @@ -27,6 +28,9 @@ public class GenerateCodeMojo extends QuarkusBootstrapMojo {
@Parameter(defaultValue = "false", property = "quarkus.generate-code.skip", alias = "quarkus.prepare.skip")
private boolean skipSourceGeneration = false;

@Parameter(defaultValue = "NORMAL", property = "launchMode")
String mode;

@Override
protected boolean beforeExecute() throws MojoExecutionException, MojoFailureException {
if (mavenProject().getPackaging().equals("pom")) {
Expand All @@ -51,10 +55,15 @@ void generateCode(Path sourcesDir,
Consumer<Path> sourceRegistrar,
boolean test) throws MojoFailureException, MojoExecutionException {

final LaunchMode launchMode = test ? LaunchMode.TEST : LaunchMode.valueOf(mode);
if (getLog().isDebugEnabled()) {
getLog().debug("Bootstrapping Quarkus application in mode " + launchMode);
}

ClassLoader originalTccl = Thread.currentThread().getContextClassLoader();
try {

final CuratedApplication curatedApplication = bootstrapApplication();
final CuratedApplication curatedApplication = bootstrapApplication(launchMode);

QuarkusClassLoader deploymentClassLoader = curatedApplication.createDeploymentClassLoader();
Thread.currentThread().setContextClassLoader(deploymentClassLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import org.eclipse.aether.repository.RemoteRepository;

import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.quarkus.maven.dependency.GACT;
import io.quarkus.maven.dependency.ArtifactKey;
import io.quarkus.runtime.LaunchMode;

public abstract class QuarkusBootstrapMojo extends AbstractMojo {

Expand Down Expand Up @@ -114,7 +113,7 @@ public abstract class QuarkusBootstrapMojo extends AbstractMojo {
@Parameter(defaultValue = "${mojoExecution}", readonly = true, required = true)
private MojoExecution mojoExecution;

private GACT projectId;
private ArtifactKey projectId;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Expand Down Expand Up @@ -199,19 +198,15 @@ protected String executionId() {
return mojoExecution.getExecutionId();
}

protected GACT projectId() {
return projectId == null ? projectId = new GACT(project.getGroupId(), project.getArtifactId()) : projectId;
protected ArtifactKey projectId() {
return projectId == null ? projectId = QuarkusBootstrapProvider.getProjectId(project) : projectId;
}

protected MavenArtifactResolver artifactResolver() throws MojoExecutionException {
return bootstrapProvider.artifactResolver(this);
}

protected QuarkusBootstrap bootstrapQuarkus() throws MojoExecutionException {
return bootstrapProvider.bootstrapQuarkus(this);
protected CuratedApplication bootstrapApplication() throws MojoExecutionException {
return bootstrapApplication(LaunchMode.NORMAL);
}

protected CuratedApplication bootstrapApplication() throws MojoExecutionException {
return bootstrapProvider.bootstrapApplication(this);
protected CuratedApplication bootstrapApplication(LaunchMode mode) throws MojoExecutionException {
return bootstrapProvider.bootstrapApplication(this, mode);
}
}
Loading

0 comments on commit 870cfb7

Please sign in to comment.