Skip to content

Commit

Permalink
Add ReachabilityMetadataCopyTask to Gradle Plugin
Browse files Browse the repository at this point in the history
Update the gradle plugin with a new `ReachabilityMetadataCopyTask`
class that can be used to copy hints obtained from the metadata
repository.
  • Loading branch information
philwebb committed Oct 6, 2022
1 parent fda6ab5 commit 73a945b
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 23 deletions.
26 changes: 26 additions & 0 deletions docs/src/docs/asciidoc/gradle-plugin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,32 @@ include::../snippets/gradle/groovy/build.gradle[tags=specify-metadata-version-fo
include::../snippets/gradle/kotlin/build.gradle.kts[tags=specify-metadata-version-for-library]
----

=== Including metadata repository files

By default, reachability metadata will be used only when your native image is generated.
In some situations, you may want a copy of the reachability metadata to use directly.

For example, copying the reachability metadata into your jar can be useful when some other process is responsible for converting your jar into a native image.
You might be generating a shaded jar and using a https://paketo.io/[Paketo buildpack] to convert it to a native image.

To download a copy of the metadata into the `build/native-reachability-metadata` directory you can the `collectReachabilityMetadata` task.
Files will be downloaded into `META-INF/native-image/<groupId>/<versionId>` subdirectories.

To include metadata repository inside your jar you can link to the task using the `jar` DSL `from` directive:

.Including metadata repository files
[source, groovy, role="multi-language-sample"]
----
include::../snippets/gradle/groovy/build.gradle[tags=include-metadata]
----

[source, kotlin, role="multi-language-sample"]
----
include::../snippets/gradle/kotlin/build.gradle.kts[tags=include-metadata]
----

For more advanced configurations you can declare a `org.graalvm.buildtools.gradle.tasks.CollectReachabilityMetadata` task and set the appropriate properties.

[[plugin-configurations]]
== Configurations defined by the plugin

Expand Down
6 changes: 6 additions & 0 deletions docs/src/docs/snippets/gradle/groovy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,9 @@ graalvmNative {
}
}
// end::specify-metadata-version-for-library[]

// tag::include-metadata[]
tasks.named("jar") {
from collectReachabilityMetadata
}
// end::include-metadata[]
6 changes: 6 additions & 0 deletions docs/src/docs/snippets/gradle/kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,9 @@ graalvmNative {
}
}
// end::specify-metadata-version-for-library[]

// tag::include-metadata[]
tasks.named("jar", Jar) {
from(collectReachabilityMetadata)
}
// end::include-metadata[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2022, 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.gradle.api.logging.LogLevel
import spock.lang.Unroll

class ReachabilityMetadataFunctionalTest extends AbstractFunctionalTest {

def "the application runs when using the official metadata repository"() {
given:
withSample("metadata-repo-integration")

when:
run 'jar', "-D${NativeImagePlugin.CONFIG_REPO_LOGLEVEL}=${LogLevel.LIFECYCLE}"

then:
tasks {
succeeded ':jar', ':reachabilityMetadata'
}

and: "has copied metadata file"
file("build/native-reachability-metadata/META-INF/native-image/com.h2database/h2/2.1.210/resource-config.json").text.trim() == '''{
"bundles": [],
"resources": {
"includes": [
{
"condition": {
"typeReachable": "org.h2.util.Utils"
},
"pattern": "\\\\Qorg/h2/util/data.zip\\\\E"
}
]
}
}'''
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.graalvm.buildtools.gradle.internal.NativeConfigurations;
import org.graalvm.buildtools.gradle.internal.agent.AgentConfigurationFactory;
import org.graalvm.buildtools.gradle.tasks.BuildNativeImageTask;
import org.graalvm.buildtools.gradle.tasks.CollectReachabilityMetadata;
import org.graalvm.buildtools.gradle.tasks.GenerateResourcesConfigFile;
import org.graalvm.buildtools.gradle.tasks.MetadataCopyTask;
import org.graalvm.buildtools.gradle.tasks.NativeRunTask;
Expand Down Expand Up @@ -99,6 +100,7 @@
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.TaskContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
Expand Down Expand Up @@ -271,6 +273,26 @@ private void configureJavaProject(Project project, Provider<NativeImageService>
task.getMergeWithExisting().set(graalExtension.getAgent().getMetadataCopy().getMergeWithExisting());
task.getToolchainDetection().set(graalExtension.getToolchainDetection());
});

project.getTasks().register("collectReachabilityMetadata", CollectReachabilityMetadata.class, task -> {
task.setGroup(LifecycleBasePlugin.BUILD_GROUP);
task.setDescription("Obtains native reachability metdata for the runtime classpath configuration");
});

GraalVMReachabilityMetadataRepositoryExtension metadataRepositoryExtension = reachabilityExtensionOn(graalExtension);
TaskCollection<CollectReachabilityMetadata> reachabilityMetadataCopyTasks = project.getTasks()
.withType(CollectReachabilityMetadata.class);
reachabilityMetadataCopyTasks.configureEach(task -> {
Provider<GraalVMReachabilityMetadataService> reachabilityMetadataService = graalVMReachabilityMetadataService(
project, metadataRepositoryExtension);
task.getMetadataService().set(reachabilityMetadataService);
task.usesService(reachabilityMetadataService);
task.getUri().convention(task.getVersion().map(this::getReachabilityMetadataRepositoryUrlForVersion)
.orElse(metadataRepositoryExtension.getUri()));
task.getExcludedModules().convention(metadataRepositoryExtension.getExcludedModules());
task.getModuleToConfigVersion().convention(metadataRepositoryExtension.getModuleToConfigVersion());
task.getInto().convention(project.getLayout().getBuildDirectory().dir("native-reachability-metadata"));
});
}

private void configureAutomaticTaskCreation(Project project,
Expand Down Expand Up @@ -320,14 +342,7 @@ private void configureAutomaticTaskCreation(Project project,

private void configureJvmReachabilityConfigurationDirectories(Project project, GraalVMExtension graalExtension, NativeImageOptions options, SourceSet sourceSet) {
GraalVMReachabilityMetadataRepositoryExtension repositoryExtension = reachabilityExtensionOn(graalExtension);
Provider<GraalVMReachabilityMetadataService> serviceProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("nativeConfigurationService", GraalVMReachabilityMetadataService.class, spec -> {
LogLevel logLevel = determineLogLevel();
spec.getParameters().getLogLevel().set(logLevel);
spec.getParameters().getUri().set(repositoryExtension.getUri());
spec.getParameters().getCacheDir().set(new File(project.getGradle().getGradleUserHomeDir(), "native-build-tools/repositories"));
});
Provider<GraalVMReachabilityMetadataService> serviceProvider = graalVMReachabilityMetadataService(project, repositoryExtension);
options.getConfigurationFileDirectories().from(repositoryExtension.getEnabled().flatMap(enabled -> {
if (enabled) {
if (repositoryExtension.getUri().isPresent()) {
Expand Down Expand Up @@ -358,14 +373,7 @@ private void configureJvmReachabilityConfigurationDirectories(Project project, G

private void configureJvmReachabilityExcludeConfigArgs(Project project, GraalVMExtension graalExtension, NativeImageOptions options, SourceSet sourceSet) {
GraalVMReachabilityMetadataRepositoryExtension repositoryExtension = reachabilityExtensionOn(graalExtension);
Provider<GraalVMReachabilityMetadataService> serviceProvider = project.getGradle()
.getSharedServices()
.registerIfAbsent("nativeConfigurationService", GraalVMReachabilityMetadataService.class, spec -> {
LogLevel logLevel = determineLogLevel();
spec.getParameters().getLogLevel().set(logLevel);
spec.getParameters().getUri().set(repositoryExtension.getUri());
spec.getParameters().getCacheDir().set(new File(project.getGradle().getGradleUserHomeDir(), "native-build-tools/repositories"));
});
Provider<GraalVMReachabilityMetadataService> serviceProvider = graalVMReachabilityMetadataService(project, repositoryExtension);
options.getExcludeConfig().putAll(repositoryExtension.getEnabled().flatMap(enabled -> {
if (enabled) {
if (repositoryExtension.getUri().isPresent()) {
Expand Down Expand Up @@ -397,6 +405,18 @@ private void configureJvmReachabilityExcludeConfigArgs(Project project, GraalVME
}));
}

private Provider<GraalVMReachabilityMetadataService> graalVMReachabilityMetadataService(Project project,
GraalVMReachabilityMetadataRepositoryExtension repositoryExtension) {
return project.getGradle()
.getSharedServices()
.registerIfAbsent("nativeConfigurationService", GraalVMReachabilityMetadataService.class, spec -> {
LogLevel logLevel = determineLogLevel();
spec.getParameters().getLogLevel().set(logLevel);
spec.getParameters().getUri().set(repositoryExtension.getUri());
spec.getParameters().getCacheDir().set(new File(project.getGradle().getGradleUserHomeDir(), "native-build-tools/repositories"));
});
}

private static LogLevel determineLogLevel() {
LogLevel logLevel = LogLevel.DEBUG;
String loggingProperty = System.getProperty(CONFIG_REPO_LOGLEVEL);
Expand Down Expand Up @@ -475,17 +495,19 @@ private void configureNativeConfigurationRepo(ExtensionAware graalvmNative) {
GraalVMReachabilityMetadataRepositoryExtension configurationRepository = graalvmNative.getExtensions().create("metadataRepository", GraalVMReachabilityMetadataRepositoryExtension.class);
configurationRepository.getEnabled().convention(false);
configurationRepository.getVersion().convention(SharedConstants.METADATA_REPO_DEFAULT_VERSION);
configurationRepository.getUri().convention(configurationRepository.getVersion().map(v -> {
try {
return new URI(String.format(METADATA_REPO_URL_TEMPLATE, v));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}));
configurationRepository.getUri().convention(configurationRepository.getVersion().map(this::getReachabilityMetadataRepositoryUrlForVersion));
configurationRepository.getExcludedModules().convention(Collections.emptySet());
configurationRepository.getModuleToConfigVersion().convention(Collections.emptyMap());
}

private URI getReachabilityMetadataRepositoryUrlForVersion(String version) {
try {
return new URI(String.format(METADATA_REPO_URL_TEMPLATE, version));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

private TaskProvider<GenerateResourcesConfigFile> registerResourcesConfigTask(Provider<Directory> generatedDir,
NativeImageOptions options,
TaskContainer tasks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.graalvm.reachability.GraalVMReachabilityMetadataRepository;
import org.graalvm.reachability.Query;
import org.graalvm.reachability.internal.FileSystemRepository;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.file.ArchiveOperations;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
Expand All @@ -66,6 +67,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -208,4 +211,20 @@ public Set<DirectoryConfiguration> findConfigurationsFor(String gavCoordinates)
public Set<DirectoryConfiguration> findConfigurationsFor(Collection<String> modules) {
return repository.findConfigurationsFor(modules);
}

public Set<DirectoryConfiguration> findConfigurationsFor(Set<String> excludedModules, Map<String, String> forcedVersions, ModuleVersionIdentifier moduleVersion) {
Objects.requireNonNull(moduleVersion);
String groupAndArtifact = moduleVersion.getGroup() + ":" + moduleVersion.getName();
return findConfigurationsFor(query -> {
if (!excludedModules.contains(groupAndArtifact)) {
query.forArtifact(artifact -> {
artifact.gav(groupAndArtifact + ":" + moduleVersion.getVersion());
if (forcedVersions.containsKey(groupAndArtifact)) {
artifact.forceConfigVersion(forcedVersions.get(groupAndArtifact));
}
});
}
query.useLatestConfigWhenVersionIsUntested();
});
}
}
Loading

0 comments on commit 73a945b

Please sign in to comment.