-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acb6963
commit a0c467b
Showing
3 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
...ls/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusRun.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters