Skip to content

Commit 30e239c

Browse files
committed
Proof of concept support for native image layers
1 parent bda62f8 commit 30e239c

File tree

13 files changed

+697
-134
lines changed

13 files changed

+697
-134
lines changed

build-logic/common-plugins/src/main/kotlin/org.graalvm.build.java.gradle.kts

+8-4
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ repositories {
6464
group = "org.graalvm.buildtools"
6565

6666
extensions.findByType<VersionCatalogsExtension>()?.also { catalogs ->
67-
val versionFromCatalog = catalogs.named("libs")
67+
if (catalogs.find("libs").isPresent) {
68+
val versionFromCatalog = catalogs.named("libs")
6869
.findVersion("nativeBuildTools")
69-
if (versionFromCatalog.isPresent()) {
70-
version = versionFromCatalog.get().requiredVersion
70+
if (versionFromCatalog.isPresent()) {
71+
version = versionFromCatalog.get().requiredVersion
72+
} else {
73+
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
74+
}
7175
} else {
72-
throw GradleException("Version catalog doesn't define project version 'nativeBuildTools'")
76+
version = "undefined"
7377
}
7478
}
7579

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2003-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.graalvm.buildtools.utils;
17+
18+
import java.io.InputStream;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.Properties;
24+
25+
public class JarMetadata {
26+
private final List<String> packageList;
27+
28+
public JarMetadata(List<String> packageList) {
29+
this.packageList = packageList;
30+
}
31+
32+
public List<String> getPackageList() {
33+
return packageList;
34+
}
35+
36+
public static JarMetadata readFrom(Path propertiesFile) {
37+
Properties props = new Properties();
38+
try (InputStream is = Files.newInputStream(propertiesFile)) {
39+
props.load(is);
40+
} catch (Exception e) {
41+
throw new RuntimeException("Unable to read metadata from properties file " + propertiesFile, e);
42+
}
43+
String packages = (String) props.get("packages");
44+
List<String> packageList = packages == null ? List.of() : Arrays.asList(packages.split(","));
45+
return new JarMetadata(packageList);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2003-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.graalvm.buildtools.utils;
17+
18+
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import java.io.Writer;
21+
import java.nio.file.FileSystem;
22+
import java.nio.file.FileSystems;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.Set;
26+
import java.util.TreeSet;
27+
import java.util.stream.Stream;
28+
29+
/**
30+
* Performs scanning of a JAR file and extracts some metadata in the
31+
* form of a properties file. For now this type only extracts the list
32+
* of packages from a jar.
33+
*/
34+
public class JarScanner {
35+
/**
36+
* Scans a jar and creates a properties file with metadata about the jar contents.
37+
* @param inputJar the input jar
38+
* @param outputFile the output file
39+
* @throws IOException
40+
*/
41+
public static void scanJar(Path inputJar, Path outputFile) throws IOException {
42+
try (Writer fileWriter = Files.newBufferedWriter(outputFile); PrintWriter writer = new PrintWriter(fileWriter)) {
43+
Set<String> packageList = new TreeSet<>();
44+
try (FileSystem jarFileSystem = FileSystems.newFileSystem(inputJar, null)) {
45+
Path root = jarFileSystem.getPath("/");
46+
try (Stream<Path> files = Files.walk(root)) {
47+
files.forEach(path -> {
48+
if (path.toString().endsWith(".class") && !path.toString().contains("META-INF")) {
49+
Path relativePath = root.relativize(path);
50+
String className = relativePath.toString()
51+
.replace('/', '.')
52+
.replace('\\', '.')
53+
.replaceAll("[.]class$", "");
54+
var lastDot = className.lastIndexOf(".");
55+
if (lastDot > 0) {
56+
var packageName = className.substring(0, lastDot);
57+
packageList.add(packageName);
58+
}
59+
}
60+
});
61+
}
62+
}
63+
writer.println("packages=" + String.join(",", packageList));
64+
} catch (IOException ex) {
65+
throw new RuntimeException("Unable to write JAR analysis", ex);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)