Skip to content

Commit

Permalink
quarkus:run gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Mar 21, 2023
1 parent acb6963 commit a0c467b
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import io.quarkus.gradle.tasks.QuarkusListPlatforms;
import io.quarkus.gradle.tasks.QuarkusRemoteDev;
import io.quarkus.gradle.tasks.QuarkusRemoveExtension;
import io.quarkus.gradle.tasks.QuarkusRun;
import io.quarkus.gradle.tasks.QuarkusTest;
import io.quarkus.gradle.tasks.QuarkusTestConfig;
import io.quarkus.gradle.tasks.QuarkusUpdate;
Expand All @@ -69,6 +70,7 @@ public class QuarkusPlugin implements Plugin<Project> {
public static final String QUARKUS_GENERATE_CODE_TESTS_TASK_NAME = "quarkusGenerateCodeTests";
public static final String QUARKUS_BUILD_TASK_NAME = "quarkusBuild";
public static final String QUARKUS_DEV_TASK_NAME = "quarkusDev";
public static final String QUARKUS_RUN_TASK_NAME = "quarkusRun";
public static final String QUARKUS_REMOTE_DEV_TASK_NAME = "quarkusRemoteDev";
public static final String QUARKUS_TEST_TASK_NAME = "quarkusTest";
public static final String QUARKUS_GO_OFFLINE_TASK_NAME = "quarkusGoOffline";
Expand Down Expand Up @@ -174,6 +176,7 @@ private void registerTasks(Project project, QuarkusPluginExtension quarkusExt) {

TaskProvider<QuarkusDev> quarkusDev = tasks.register(QUARKUS_DEV_TASK_NAME, QuarkusDev.class, devRuntimeDependencies,
quarkusExt);
TaskProvider<QuarkusRun> quarkusRun = tasks.register(QUARKUS_RUN_TASK_NAME, QuarkusRun.class);
TaskProvider<QuarkusRemoteDev> quarkusRemoteDev = tasks.register(QUARKUS_REMOTE_DEV_TASK_NAME, QuarkusRemoteDev.class,
devRuntimeDependencies, quarkusExt);
TaskProvider<QuarkusTest> quarkusTest = tasks.register(QUARKUS_TEST_TASK_NAME, QuarkusTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package io.quarkus.gradle.tasks;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.gradle.api.GradleException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskAction;

import io.quarkus.bootstrap.BootstrapException;
import io.quarkus.bootstrap.app.AugmentAction;
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.QuarkusBootstrap;
import io.quarkus.bootstrap.model.ApplicationModel;
import io.quarkus.bootstrap.resolver.AppModelResolverException;
import io.quarkus.deployment.run.RunCommandHandler;
import io.quarkus.deployment.run.RunCommandLaunchResultBuildItem;
import io.quarkus.gradle.extension.QuarkusPluginExtension;
import io.quarkus.maven.dependency.GACTV;

public class QuarkusRun extends QuarkusTask {
private final Property<File> workingDirectory;
private final SourceSet mainSourceSet;

@Inject
public QuarkusRun() {
this("Quarkus runs target application");
}

public QuarkusRun(String description) {
super(description);
mainSourceSet = getProject().getExtensions().getByType(SourceSetContainer.class)
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);

final ObjectFactory objectFactory = getProject().getObjects();

workingDirectory = objectFactory.property(File.class);
workingDirectory.convention(getProject().provider(() -> QuarkusPluginExtension.getLastFile(getCompilationOutput())));

}

/**
* The JVM classes directory (compilation output)
*/
@Optional
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public FileCollection getCompilationOutput() {
return mainSourceSet.getOutput().getClassesDirs();
}

@Input
public Property<File> getWorkingDirectory() {
return workingDirectory;
}

/**
* @deprecated See {@link #workingDirectory}
*/
@Deprecated
public void setWorkingDir(String workingDir) {
workingDirectory.set(getProject().file(workingDir));
}

@Classpath
public FileCollection getClasspath() {
SourceSet mainSourceSet = QuarkusGradleUtils.getSourceSet(getProject(), SourceSet.MAIN_SOURCE_SET_NAME);
return mainSourceSet.getCompileClasspath().plus(mainSourceSet.getRuntimeClasspath())
.plus(mainSourceSet.getAnnotationProcessorPath())
.plus(mainSourceSet.getResources());
}

@Input
public Map<Object, Object> getQuarkusBuildSystemProperties() {
Map<Object, Object> quarkusSystemProperties = new HashMap<>();
for (Map.Entry<Object, Object> systemProperty : System.getProperties().entrySet()) {
if (systemProperty.getKey().toString().startsWith("quarkus.") &&
systemProperty.getValue() instanceof Serializable) {
quarkusSystemProperties.put(systemProperty.getKey(), systemProperty.getValue());
}
}
return quarkusSystemProperties;
}

@Input
public Map<String, String> getQuarkusBuildEnvProperties() {
Map<String, String> quarkusEnvProperties = new HashMap<>();
for (Map.Entry<String, String> systemProperty : System.getenv().entrySet()) {
if (systemProperty.getKey() != null && systemProperty.getKey().startsWith("QUARKUS_")) {
quarkusEnvProperties.put(systemProperty.getKey(), systemProperty.getValue());
}
}
return quarkusEnvProperties;
}

@TaskAction
public void runQuarkus() {
final ApplicationModel appModel;

try {
appModel = extension().getAppModelResolver().resolveModel(new GACTV(getProject().getGroup().toString(),
getProject().getName(), getProject().getVersion().toString()));
} catch (AppModelResolverException e) {
throw new GradleException("Failed to resolve Quarkus application model for " + getProject().getPath(), e);
}

final Properties effectiveProperties = getBuildSystemProperties(appModel.getAppArtifact());
try (CuratedApplication curatedApplication = QuarkusBootstrap.builder()
.setBaseClassLoader(getClass().getClassLoader())
.setExistingModel(appModel)
.setTargetDirectory(getProject().getBuildDir().toPath())
.setBaseName(extension().finalName())
.setBuildSystemProperties(effectiveProperties)
.setAppArtifact(appModel.getAppArtifact())
.setLocalProjectDiscovery(false)
.setIsolateDeployment(true)
.build().bootstrap()) {

AugmentAction action = curatedApplication.createAugmentor();
AtomicReference<Boolean> exists = new AtomicReference<>();
AtomicReference<String> tooMany = new AtomicReference<>();
String target = System.getProperty("quarkus.run.target");
action.performCustomBuild(RunCommandHandler.class.getName(), new Consumer<Map<String, List>>() {
@Override
public void accept(Map<String, List> cmds) {
List cmd = null;
if (target != null) {
cmd = cmds.get(target);
if (cmd == null) {
exists.set(false);
return;
}
} else if (cmds.size() == 1) { // defaults to pure java run
cmd = cmds.values().iterator().next();
} else if (cmds.size() == 2) { // choose not default
for (Map.Entry<String, List> entry : cmds.entrySet()) {
if (entry.getKey().equals("java"))
continue;
cmd = entry.getValue();
break;
}
} else if (cmds.size() > 2) {
tooMany.set(cmds.keySet().stream().collect(Collectors.joining(" ")));
return;
} else {
throw new RuntimeException("Should never reach this!");
}
List<String> args = (List<String>) cmd.get(0);
getProject().getLogger().info("Executing \"" + String.join(" ", args) + "\"");
Path wd = (Path) cmd.get(1);
File wdir = wd != null ? wd.toFile() : workingDirectory.get();
getProject().exec(action -> {
action.commandLine(args).workingDir(wdir);
action.setStandardInput(System.in)
.setErrorOutput(System.out)
.setStandardOutput(System.out);
});
}
},
RunCommandLaunchResultBuildItem.class.getName());
if (target != null && !exists.get()) {
getProject().getLogger().error("quarkus.run.target " + target + " is not found");
return;
}
if (tooMany.get() != null) {
getProject().getLogger().error(
"Too many installed extensions support quarkus:run. Use -Dquarkus.run.target=<target> to choose");
getProject().getLogger().error("Extensions: " + tooMany.get());
}
} catch (BootstrapException e) {
throw new GradleException("Failed to run application", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</property>
</appSettings>
</configuration>
<!--
<executions>
<execution>
<id>package-functions</id>
Expand All @@ -49,6 +50,7 @@
</goals>
</execution>
</executions>
-->
</plugin>
<!--Remove obj folder generated by .NET SDK in maven clean-->
<plugin>
Expand Down

0 comments on commit a0c467b

Please sign in to comment.