Skip to content

Commit 2e0dd49

Browse files
Add a sharedLibrary configuration option for Gradle plugin
This commit adds `sharedLibrary` configuration option to the Gradle plugin as it was a requested feature. `java-library` test sample is also included as well as corresponding functional test.
1 parent d0564b4 commit 2e0dd49

File tree

11 files changed

+354
-0
lines changed

11 files changed

+354
-0
lines changed

docs/src/docs/asciidoc/gradle-plugin.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ nativeBuild {
146146
verbose = true // Add verbose output, defaults to false
147147
fallback = true // Sets the fallback mode of native-image, defaults to false
148148
server = true // Sets the server mode, defaults to false
149+
sharedLibrary = false // Determines if image is a shared library, defaults to false if `java-library` plugin isn't included
150+
149151
systemProperties = [name1: 'value1', name2: 'value2'] // Sets the system properties to use for the native image builder
150152
configurationFileDirectories.from(file('src/my-config')) // Adds a native image configuration file directory, containing files like reflection configuration
151153

@@ -171,6 +173,9 @@ nativeBuild {
171173
verbose.set(true) // Add verbose output, defaults to false
172174
fallback.set(true) // Sets the fallback mode of native-image, defaults to false
173175
server.set(true) // Sets the server mode, defaults to false
176+
sharedLibrary.set(false) // Determines if image is a shared library, defaults to false if `java-library` plugin isn't included
177+
178+
174179
systemProperties.putAll(mapOf(name1 to "value1", name2 to "value2")) // Sets the system properties to use for the native image builder
175180
configurationFileDirectories.from(file("src/my-config")) // Adds a native image configuration file directory, containing files like reflection configuration
176181

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
package org.graalvm.buildtools.gradle
43+
44+
import org.graalvm.buildtools.gradle.fixtures.AbstractFunctionalTest
45+
46+
class JavaLibraryFunctionalTest extends AbstractFunctionalTest {
47+
def "can build a native image for a simple library"() {
48+
gradleVersion = version
49+
debug = true
50+
51+
given:
52+
withSample("java-library")
53+
54+
when:
55+
run 'nativeBuild'
56+
57+
then:
58+
tasks {
59+
succeeded ':jar', ':nativeBuild'
60+
doesNotContain ':build'
61+
}
62+
63+
outputContains "-H:+SharedLibrary"
64+
65+
where:
66+
version << TESTED_GRADLE_VERSIONS
67+
}
68+
}

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/NativeImagePlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ private void configureJavaProject(Project project, Provider<NativeImageService>
134134
project.getExtensions().findByType(JavaApplication.class).getMainClass()
135135
));
136136

137+
project.getPlugins().withId("java-library", p -> buildExtension.getSharedLibrary().convention(true));
138+
137139
registerServiceProvider(project, nativeImageServiceProvider);
138140

139141
// Register Native Image tasks

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/dsl/NativeImageOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ public abstract class NativeImageOptions {
177177
@Input
178178
public abstract Property<Boolean> getAgent();
179179

180+
/**
181+
* Gets the value which determines if shared library is being built.
182+
*
183+
* @return The value which determines if shared library is being built.
184+
*/
185+
@Input
186+
@Optional
187+
public abstract Property<Boolean> getSharedLibrary();
188+
180189
/**
181190
* Returns the toolchain used to invoke native-image. Currently pointing
182191
* to a Java launcher due to Gradle limitations.

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/internal/NativeImageCommandLineProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public List<String> asArguments() {
134134
if (options.getMainClass().isPresent()) {
135135
cliArgs.add("-H:Class=" + options.getMainClass().get());
136136
}
137+
if (options.getSharedLibrary().getOrElse(false)) {
138+
cliArgs.add("--shared");
139+
}
140+
137141
cliArgs.addAll(options.getBuildArgs().get());
138142
return Collections.unmodifiableList(cliArgs);
139143
}

samples/java-library/build.gradle

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
plugins {
43+
id 'java-library'
44+
id 'org.graalvm.buildtools.native'
45+
}
46+
47+
repositories {
48+
mavenCentral()
49+
}
50+
51+
def junitVersion = providers.gradleProperty('junit.jupiter.version')
52+
.forUseAtConfigurationTime()
53+
.get()
54+
55+
dependencies {
56+
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
57+
testImplementation('org.junit.jupiter:junit-jupiter')
58+
}
59+
60+
nativeBuild {
61+
verbose=true
62+
}
63+
64+
test {
65+
useJUnitPlatform()
66+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
native.gradle.plugin.version = 0.9.3-SNAPSHOT
2+
junit.jupiter.version = 5.7.2
3+
junit.platform.version = 1.7.2

samples/java-library/pom.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
6+
The Universal Permissive License (UPL), Version 1.0
7+
8+
Subject to the condition set forth below, permission is hereby granted to any
9+
person obtaining a copy of this software, associated documentation and/or
10+
data (collectively the "Software"), free of charge and under any and all
11+
copyright rights in the Software, and any and all patent rights owned or
12+
freely licensable by each licensor hereunder covering either (i) the
13+
unmodified Software as contributed to or provided by such licensor, or (ii)
14+
the Larger Works (as defined below), to deal in both
15+
16+
(a) the Software, and
17+
18+
(b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
19+
one is included with the Software each a "Larger Work" to which the Software
20+
is contributed by such licensors),
21+
22+
without restriction, including without limitation the rights to copy, create
23+
derivative works of, display, perform, and distribute the Software and make,
24+
use, sell, offer for sale, import, export, have made, and have sold the
25+
Software and the Larger Work(s), and to sublicense the foregoing rights on
26+
either these or other terms.
27+
28+
This license is subject to the following condition:
29+
30+
The above copyright notice and either this complete permission notice or at a
31+
minimum a reference to the UPL must be included in all copies or substantial
32+
portions of the Software.
33+
34+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40+
SOFTWARE.
41+
-->
42+
43+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
44+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
45+
<modelVersion>4.0.0</modelVersion>
46+
47+
<groupId>org.graalvm.buildtools.examples</groupId>
48+
<artifactId>maven</artifactId>
49+
<version>1.0.0-SNAPSHOT</version>
50+
51+
<properties>
52+
<java.version>1.8</java.version>
53+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
54+
<native.maven.plugin.version>0.9.3-SNAPSHOT</native.maven.plugin.version>
55+
<junit.platform.native.version>0.9.3-SNAPSHOT</junit.platform.native.version>
56+
<imageName>java-library</imageName>
57+
</properties>
58+
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.graalvm.buildtools</groupId>
62+
<artifactId>junit-platform-native</artifactId>
63+
<version>${junit.platform.native.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
68+
<profiles>
69+
<profile>
70+
<id>native</id>
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.graalvm.buildtools</groupId>
75+
<artifactId>native-maven-plugin</artifactId>
76+
<version>${native.maven.plugin.version}</version>
77+
<executions>
78+
<execution>
79+
<id>build-native</id>
80+
<goals>
81+
<goal>build</goal>
82+
</goals>
83+
<phase>package</phase>
84+
</execution>
85+
</executions>
86+
<configuration>
87+
<skip>false</skip>
88+
<imageName>${imageName}</imageName>
89+
<buildArgs>
90+
<buildArg>--no-fallback</buildArg>
91+
<buildArg>--shared</buildArg>
92+
</buildArgs>
93+
</configuration>
94+
</plugin>
95+
</plugins>
96+
</build>
97+
</profile>
98+
</profiles>
99+
100+
<build>
101+
<finalName>${project.artifactId}</finalName>
102+
<plugins>
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<version>3.0.0-M5</version>
107+
</plugin>
108+
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<version>3.8.1</version>
113+
<configuration>
114+
<source>${java.version}</source>
115+
<target>1.8</target>
116+
<archive>
117+
<manifest>
118+
<addClasspath>true</addClasspath>
119+
<mainClass>${mainClass}</mainClass>
120+
</manifest>
121+
</archive>
122+
</configuration>
123+
</plugin>
124+
</plugins>
125+
</build>
126+
127+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
42+
pluginManagement {
43+
plugins {
44+
id 'org.graalvm.buildtools.native' version getProperty('native.gradle.plugin.version')
45+
}
46+
repositories {
47+
mavenCentral()
48+
gradlePluginPortal()
49+
}
50+
}
51+
52+
rootProject.name = 'java-library'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.graalvm.demo;
2+
3+
public class Library {
4+
public boolean someLibraryMethod() {
5+
return true;
6+
}
7+
}

0 commit comments

Comments
 (0)