Skip to content

Commit

Permalink
[quarkusio#8850] Generate uber jar for native image source jar if the…
Browse files Browse the repository at this point in the history
… uberJar option of quarkus-maven-plugin is enabled
  • Loading branch information
Mobe91 committed Apr 25, 2020
1 parent 6167fae commit 0314787
Showing 1 changed file with 95 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,40 @@ private JarBuildItem buildUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
.resolve(outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix + ".jar");
Files.deleteIfExists(runnerJar);

buildUberJar0(curateOutcomeBuildItem,
transformedClasses,
applicationArchivesBuildItem,
packageConfig,
applicationInfo,
generatedClasses,
generatedResources,
mainClassBuildItem,
runnerJar);

//for uberjars we move the original jar, so there is only a single jar in the output directory
final Path standardJar = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + ".jar");

final Path originalJar;
if (Files.exists(standardJar)) {
originalJar = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + ".jar.original");
} else {
originalJar = null;
}

return new JarBuildItem(runnerJar, originalJar, null);
}

private void buildUberJar0(CurateOutcomeBuildItem curateOutcomeBuildItem,
TransformedClassesBuildItem transformedClasses,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
PackageConfig packageConfig,
ApplicationInfoBuildItem applicationInfo,
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Path runnerJar) throws Exception {
try (FileSystem runnerZipFs = ZipUtils.newZip(runnerJar)) {

log.info("Building fat jar: " + runnerJar);
Expand Down Expand Up @@ -231,20 +265,6 @@ private JarBuildItem buildUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
}

runnerJar.toFile().setReadable(true, false);

//for uberjars we move the original jar, so there is only a single jar in the output directory
final Path standardJar = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + ".jar");

final Path originalJar;
if (Files.exists(standardJar)) {
originalJar = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + ".jar.original");
} else {
originalJar = null;
}

return new JarBuildItem(runnerJar, originalJar, null);
}

private boolean isAppDepAJar(AppArtifact artifact) {
Expand Down Expand Up @@ -352,34 +372,84 @@ public NativeImageSourceJarBuildItem buildNativeImageJar(CurateOutcomeBuildItem
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedNativeImageClassBuildItem> nativeImageResources,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem) throws Exception {
Path thinJarDirectory = outputTargetBuildItem.getOutputDirectory()
MainClassBuildItem mainClassBuildItem,
List<UberJarRequiredBuildItem> uberJarRequired) throws Exception {
Path targetDirectory = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + "-native-image-source-jar");
IoUtils.recursiveDelete(thinJarDirectory);
Files.createDirectories(thinJarDirectory);
copyJsonConfigFiles(applicationArchivesBuildItem, thinJarDirectory);

Path runnerJar = thinJarDirectory
.resolve(outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix + ".jar");
Path libDir = thinJarDirectory.resolve("lib");
Files.createDirectories(libDir);
IoUtils.recursiveDelete(targetDirectory);
Files.createDirectories(targetDirectory);

List<GeneratedClassBuildItem> allClasses = new ArrayList<>(generatedClasses);
allClasses.addAll(nativeImageResources.stream()
.map((s) -> new GeneratedClassBuildItem(true, s.getName(), s.getClassData()))
.collect(Collectors.toList()));

if (!uberJarRequired.isEmpty() || packageConfig.uberJar) {
return buildNativeImageUberJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem,
packageConfig, applicationInfo, allClasses, generatedResources, mainClassBuildItem, targetDirectory);
} else {
return buildNativeImageThinJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem,
applicationInfo, packageConfig, allClasses, generatedResources, mainClassBuildItem, targetDirectory);
}
}

private NativeImageSourceJarBuildItem buildNativeImageThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
OutputTargetBuildItem outputTargetBuildItem,
TransformedClassesBuildItem transformedClasses,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
ApplicationInfoBuildItem applicationInfo,
PackageConfig packageConfig,
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Path targetDirectory) throws Exception {
copyJsonConfigFiles(applicationArchivesBuildItem, targetDirectory);

Path runnerJar = targetDirectory
.resolve(outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix + ".jar");
Path libDir = targetDirectory.resolve("lib");
Files.createDirectories(libDir);

try (FileSystem runnerZipFs = ZipUtils.newZip(runnerJar)) {

log.info("Building native image source jar: " + runnerJar);

doThinJarGeneration(curateOutcomeBuildItem, transformedClasses, applicationArchivesBuildItem, applicationInfo,
packageConfig, generatedResources, libDir, allClasses, runnerZipFs, mainClassBuildItem);
packageConfig, generatedResources, libDir, generatedClasses, runnerZipFs, mainClassBuildItem);
}
runnerJar.toFile().setReadable(true, false);
return new NativeImageSourceJarBuildItem(runnerJar, libDir);
}

private NativeImageSourceJarBuildItem buildNativeImageUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
OutputTargetBuildItem outputTargetBuildItem,
TransformedClassesBuildItem transformedClasses,
ApplicationArchivesBuildItem applicationArchivesBuildItem,
PackageConfig packageConfig,
ApplicationInfoBuildItem applicationInfo,
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Path targetDirectory) throws Exception {
//we use the -runner jar name, unless we are building both types
Path runnerJar = targetDirectory
.resolve(outputTargetBuildItem.getBaseName() + packageConfig.runnerSuffix + ".jar");

buildUberJar0(curateOutcomeBuildItem,
transformedClasses,
applicationArchivesBuildItem,
packageConfig,
applicationInfo,
generatedClasses,
generatedResources,
mainClassBuildItem,
runnerJar);

return new NativeImageSourceJarBuildItem(runnerJar, null);
}

/**
* This is done in order to make application specific native image configuration files available to the native-image tool
* without the user needing to know any specific paths.
Expand Down

0 comments on commit 0314787

Please sign in to comment.