Skip to content

Experimental support for layered images #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ repositories {
group = "org.graalvm.buildtools"

extensions.findByType<VersionCatalogsExtension>()?.also { catalogs ->
val versionFromCatalog = catalogs.named("libs")
if (catalogs.find("libs").isPresent) {
val versionFromCatalog = catalogs.named("libs")
.findVersion("nativeBuildTools")
if (versionFromCatalog.isPresent()) {
version = versionFromCatalog.get().requiredVersion
if (versionFromCatalog.isPresent()) {
version = versionFromCatalog.get().requiredVersion
} else {
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
}
} else {
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
version = "undefined"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.graalvm.buildtools.utils;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class JarMetadata {
private final List<String> packageList;

public JarMetadata(List<String> packageList) {
this.packageList = packageList;
}

public List<String> getPackageList() {
return packageList;
}

public static JarMetadata readFrom(Path propertiesFile) {
Properties props = new Properties();
try (InputStream is = Files.newInputStream(propertiesFile)) {
props.load(is);
} catch (Exception e) {
throw new RuntimeException("Unable to read metadata from properties file " + propertiesFile, e);
}
String packages = (String) props.get("packages");
List<String> packageList = packages == null ? List.of() : Arrays.asList(packages.split(","));
return new JarMetadata(packageList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2003-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.graalvm.buildtools.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;

/**
* Performs scanning of a JAR file and extracts some metadata in the
* form of a properties file. For now this type only extracts the list
* of packages from a jar.
*/
public class JarScanner {
/**
* Scans a jar and creates a properties file with metadata about the jar contents.
* @param inputJar the input jar
* @param outputFile the output file
* @throws IOException
*/
public static void scanJar(Path inputJar, Path outputFile) throws IOException {
try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) {
Set<String> packageList = new TreeSet<>();
try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, null)) {
Path root = jarFileSystem.getPath("/");
try (Stream<Path> files = Files.walk(root)) {
files.forEach(path -> {
if (path.toString().endsWith(".class") && !path.toString().contains("META-INF")) {
Path relativePath = root.relativize(path);
String className = relativePath.toString()
.replace('/', '.')
.replace('\\', '.')
.replaceAll("[.]class$", "");
var lastDot = className.lastIndexOf(".");
if (lastDot > 0) {
var packageName = className.substring(0, lastDot);
packageList.add(packageName);
}
}
});
}
}
writer.println("packages=" + String.join(",", packageList));
} catch (IOException ex) {
throw new RuntimeException("Unable to write JAR analysis", ex);
}
}
}
6 changes: 6 additions & 0 deletions docs/src/docs/asciidoc/changelog.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[[changelog]]
== Changelog

== Release 0.10.7

=== Gradle plugin

- Added experimental support for layered images

== Release 0.10.6

=== Gradle plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import org.graalvm.buildtools.gradle.fixtures.AbstractFunctionalTest

class JUnitFunctionalTests extends AbstractFunctionalTest {
def "test if JUint support works with various annotations, reflection and resources"() {

debug=true
given:
withSample("junit-tests")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.graalvm.buildtools.gradle

import org.graalvm.buildtools.gradle.fixtures.AbstractFunctionalTest
import org.graalvm.buildtools.gradle.fixtures.GraalVMSupport
import org.graalvm.buildtools.utils.NativeImageUtils
import spock.lang.Requires
import spock.util.concurrent.PollingConditions

import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.charset.StandardCharsets

@Requires(
{ NativeImageUtils.getMajorJDKVersion(GraalVMSupport.getGraalVMHomeVersionString()) >= 25 }
)
class LayeredApplicationFunctionalTest extends AbstractFunctionalTest {
def "can build a native image using layers"() {
def nativeApp = getExecutableFile("build/native/nativeCompile/layered-java-application")

given:
withSample("layered-java-application")

when:
run 'nativeLibdependenciesCompile'

then:
tasks {
succeeded ':nativeLibdependenciesCompile'
}
outputContains "'-H:LayerCreate' (origin(s): command line)"

when:
run 'nativeRun', '-Pmessage="Hello, layered images!"'

then:
tasks {
upToDate ':nativeLibdependenciesCompile'
succeeded ':nativeCompile'
}
nativeApp.exists()

and:
outputContains "- '-H:LayerUse' (origin(s): command line)"
outputContains "Hello, layered images!"

when: "Updating the application without changing the dependencies"
file("src/main/java/org/graalvm/demo/Application.java").text = """
package org.graalvm.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
LOGGER.info("App started with args {}", String.join(", ", args));
}

}

"""
run 'nativeRun', '-Pmessage="Hello, layered images!"'

then:
tasks {
// Base layer is not rebuilt
upToDate ':nativeLibdependenciesCompile'
// Application layer is recompiled
succeeded ':nativeCompile'
}

outputContains "- '-H:LayerUse' (origin(s): command line)"
outputContains "Hello, layered images!"
}

def "can build a layered Micronaut application"() {
given:
withSample("layered-mn-application")

when:
run 'nativeCompile'

then:
tasks {
succeeded ':nativeLibdependenciesCompile', ':nativeCompile'
}

when:
def builder = new ProcessBuilder()
.directory(testDirectory.toFile())
.inheritIO()
.command("build/native/nativeCompile/layered-mn-app${IS_WINDOWS?".exe":""}")
def env = builder.environment()
env["LD_LIBRARY_PATH"] = testDirectory.resolve("build/native/nativeLibdependenciesCompile").toString()
def process = builder.start()
def client = HttpClient.newHttpClient()
def request = HttpRequest.newBuilder()
.GET()
.uri(new URI("http://localhost:8080/"))
.build()
def conditions = new PollingConditions()

then:
conditions.within(10) {
def response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)).body()
response == "Hello, layered images!"
}

cleanup:
process.destroy()
}
}
Loading
Loading