Skip to content

Commit

Permalink
Perform Quarkus-build in build/
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Apr 11, 2023
1 parent 717e2f2 commit 1fc06e4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ protected Map<String, File> getBuildOutputDirectories() {
case LEGACY_JAR:
case LEGACY:
outputs.put("fast-jar", fastJar());
outputs.put("legacy-lib", buildDir.toPath().resolve("lib").toFile());
outputs.put("legacy-lib", gradleBuildDir().resolve("lib").toFile());
break;
case NATIVE:
outputs.put("native-source", nativeSources());
outputs.put("fast-jar", fastJar());
break;
case JAR:
case FAST_JAR:
case NATIVE:
outputs.put("fast-jar", fastJar());
break;
case MUTABLE_JAR:
Expand All @@ -136,23 +139,20 @@ protected Map<String, File> getBuildOutputFiles() {
Map<String, File> outputs = new HashMap<>();
PackageConfig.BuiltInType packageType = packageType();
switch (packageType) {
case UBER_JAR:
case LEGACY_JAR:
case LEGACY:
outputs.put("legacy-jar", runnerJar());
outputs.put("artifact-properties", artifactProperties());
break;
case JAR:
case FAST_JAR:
case MUTABLE_JAR:
case NATIVE_SOURCES:
outputs.put("runner-jar", runnerJar());
outputs.put("artifact-properties", artifactProperties());
break;
case NATIVE:
outputs.put("native-runner", nativeRunner());
outputs.put("artifact-properties", artifactProperties());
break;
case UBER_JAR:
outputs.put("uber-jar", runnerJar());
case JAR:
case FAST_JAR:
case MUTABLE_JAR:
case NATIVE_SOURCES:
outputs.put("artifact-properties", artifactProperties());
break;
default:
Expand Down Expand Up @@ -196,7 +196,7 @@ private void runnerAndArtifactsInputs(Consumer<File> buildInputs, Path sourceDir
buildInputs.accept(sourceDir.resolve(nativeRunnerFileName()).toFile());
buildInputs.accept(sourceDir.resolve(runnerJarFileName()).toFile());
// TODO jib-image* ??
buildInputs.accept(sourceDir.resolve(runnerBaseName() + "-native-image-source-jar").toFile());
buildInputs.accept(sourceDir.resolve(nativeImageSourceJarDirName()).toFile());
}

@TaskAction
Expand Down Expand Up @@ -320,7 +320,7 @@ private void copyRunnersAndArtifactProperties(Path sourceDir) {
runnerJarFileName(),
"jib-image*",
NATIVE_SOURCES,
runnerBaseName() + "-native-image-source-jar/**"));
nativeImageSourceJarDirName() + "/**"));
}

private String expandConfigurationKey(String shortKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.quarkus.gradle.tasks;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -61,16 +59,20 @@ PackageConfig.BuiltInType packageType() {
return extension().baseConfig().packageType();
}

Path gradleBuildDir() {
return buildDir.toPath();
}

Path genBuildDir() {
return buildDir.toPath().resolve(QUARKUS_BUILD_GEN_DIR);
return gradleBuildDir().resolve(QUARKUS_BUILD_GEN_DIR);
}

Path appBuildDir() {
return buildDir.toPath().resolve(QUARKUS_BUILD_APP_DIR);
return gradleBuildDir().resolve(QUARKUS_BUILD_APP_DIR);
}

Path depBuildDir() {
return buildDir.toPath().resolve(QUARKUS_BUILD_DEP_DIR);
return gradleBuildDir().resolve(QUARKUS_BUILD_DEP_DIR);
}

File artifactProperties() {
Expand Down Expand Up @@ -114,6 +116,10 @@ String runnerName() {
return runnerBaseName() + runnerSuffix();
}

String nativeImageSourceJarDirName() {
return runnerBaseName() + "-native-image-source-jar";
}

String runnerBaseName() {
BaseConfig baseConfig = extension().baseConfig();
return baseConfig.packageConfig().outputName.orElseGet(() -> extension().finalName());
Expand All @@ -139,19 +145,54 @@ ApplicationModel resolveAppModelForBuild() {
return appModel;
}

/**
* Runs the Quarkus-build in the "well known" location, Gradle's {@code build/} directory.
*
* <p>
* It would be easier to run the Quarkus-build directly in {@code build/quarkus-build/gen} to have a "clean
* target directory", but that breaks already existing Gradle builds for users, which have for example
* {@code Dockerfile}s that rely on the fact that build artifacts are present in {@code build/}.
*
* <p>
* This requires this method to
* <ol>
* <li>"properly" clean the directories that are going to be populated by the Quarkus build, then
* <li>run the Quarkus build with the target directory {@code build/} and then
* <li>populate the
* </ol>
*/
void generateBuild() {
Path buildDir = gradleBuildDir();
Path genDir = genBuildDir();
PackageConfig.BuiltInType packageType = packageType();
getLogger().info("Building Quarkus app for package type {} in {}", packageType, genDir);

// Caching and "up-to-date" checks depend on the inputs, this 'delete()' should ensure that the up-to-date
// checks work against "clean" outputs, considering that the outputs depend on the package-type.
getFileSystemOperations().delete(delete -> delete.delete(genDir));
try {
Files.createDirectories(genDir);
} catch (IOException e) {
throw new GradleException("Could not create directory " + genDir, e);
}
getFileSystemOperations().delete(delete -> {
// Caching and "up-to-date" checks depend on the inputs, this 'delete()' should ensure that the up-to-date
// checks work against "clean" outputs, considering that the outputs depend on the package-type.
delete.delete(genDir);

// Delete directories inside Gradle's build/ dir that are going to be populated by the Quarkus build.
switch (packageType) {
case JAR:
case FAST_JAR:
delete.delete(buildDir.resolve(nativeImageSourceJarDirName()));
// fall through
case NATIVE:
case NATIVE_SOURCES:
delete.delete(fastJar());
break;
case LEGACY_JAR:
case LEGACY:
delete.delete(buildDir.resolve("lib"));
break;
case MUTABLE_JAR:
case UBER_JAR:
break;
default:
throw new GradleException("Unsupported package type " + packageType);
}
});

ApplicationModel appModel = resolveAppModelForBuild();
Map<String, String> configMap = extension().buildEffectiveConfiguration(appModel.getAppArtifact()).configMap();
Expand All @@ -161,7 +202,7 @@ void generateBuild() {
if (getLogger().isEnabled(LogLevel.INFO)) {
getLogger().info("Effective properties: {}",
configMap.entrySet().stream()
.filter(e -> e.getKey().startsWith("quarkus.")).map(e -> "" + e)
.filter(e -> e.getKey().startsWith("quarkus.")).map(Object::toString)
.sorted()
.collect(Collectors.joining("\n ", "\n ", "")));
}
Expand All @@ -171,10 +212,42 @@ void generateBuild() {
workQueue.submit(BuildWorker.class, params -> {
params.getBuildSystemProperties().putAll(configMap);
params.getBaseName().set(extension().finalName());
params.getTargetDirectory().set(genDir.toFile());
params.getTargetDirectory().set(buildDir.toFile());
params.getAppModel().set(appModel);
});

workQueue.await();

// Copy built artifacts from `build/` into `build/quarkus-build/gen/`
getFileSystemOperations().copy(copy -> {
copy.from(buildDir);
copy.into(genDir);
switch (packageType) {
case NATIVE:
copy.include(nativeRunnerFileName());
copy.include(nativeImageSourceJarDirName() + "/**");
// fall through
case JAR:
case FAST_JAR:
copy.include(outputDirectory() + "/**");
copy.include(QUARKUS_ARTIFACT_PROPERTIES);
break;
case LEGACY_JAR:
case LEGACY:
copy.include("lib/**");
// fall through
case MUTABLE_JAR:
case UBER_JAR:
copy.include(QUARKUS_ARTIFACT_PROPERTIES);
copy.include(runnerJarFileName());
break;
case NATIVE_SOURCES:
copy.include(QUARKUS_ARTIFACT_PROPERTIES);
copy.include(nativeImageSourceJarDirName() + "/**");
break;
default:
throw new GradleException("Unsupported package type " + packageType);
}
});
}
}

0 comments on commit 1fc06e4

Please sign in to comment.