Skip to content

Commit b4af886

Browse files
authored
Merge pull request #23 from steve-s/ss/gradle-maven-24.2
Update Gradle and Maven GraalPy plugin usages for 24.2.0
2 parents 5ba9363 + bb3e9d9 commit b4af886

File tree

21 files changed

+167
-220
lines changed

21 files changed

+167
-220
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ node_modules/
1616
Thumbs.db
1717
.sdkmanrc
1818
settings.json
19+
graalpy/graalpy-javase-guide/python-resources
20+
**/.mvn/wrapper/maven-wrapper.jar

graalpy/graalpy-freeze-dependencies-guide/README.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ For Maven, add dependency on GraalPy runtime, and configure the GraalPy Maven pl
8181
<groupId>org.graalvm.python</groupId>
8282
<artifactId>graalpy-maven-plugin</artifactId>
8383
<version>24.2.0</version>
84+
<configuration>
85+
<packages> <!---->
86+
<package>vaderSentiment==3.3.2</package>
87+
</packages>
88+
</configuration>
8489
<executions>
8590
<execution>
86-
<configuration>
87-
<packages> <!---->
88-
<package>vaderSentiment==3.3.2</package>
89-
</packages>
90-
</configuration>
9191
<goals>
9292
<goal>process-graalpy-resources</goal>
9393
</goals>
@@ -113,10 +113,6 @@ plugins {
113113
graalPy {
114114
packages = setOf("vaderSentiment==3.3.2") //
115115
}
116-
117-
dependencies {
118-
implementation("org.graalvm.python:python:24.2.0")
119-
}
120116
```
121117

122118
❶ The `packages` section lists all Python packages optionally with [requirement specifiers](https://pip.pypa.io/en/stable/reference/requirement-specifiers/).
@@ -183,18 +179,18 @@ If you use Maven, paste them in the `pom.xml` section of the packages and wrap t
183179
<groupId>org.graalvm.python</groupId>
184180
<artifactId>graalpy-maven-plugin</artifactId>
185181
<version>24.2.0</version>
182+
<configuration>
183+
<packages> <!---->
184+
<package>vaderSentiment==3.3.2</package>
185+
<package>certifi==2024.8.30</package>
186+
<package>charset-normalizer==3.1.0</package>
187+
<package>idna==3.8</package>
188+
<package>requests==2.32.3</package>
189+
<package>urllib3==2.2.2</package>
190+
</packages>
191+
</configuration>
186192
<executions>
187193
<execution>
188-
<configuration>
189-
<packages> <!---->
190-
<package>vaderSentiment==3.3.2</package>
191-
<package>certifi==2024.8.30</package>
192-
<package>charset-normalizer==3.1.0</package>
193-
<package>idna==3.8</package>
194-
<package>requests==2.32.3</package>
195-
<package>urllib3==2.2.2</package>
196-
</packages>
197-
</configuration>
198194
<goals>
199195
<goal>process-graalpy-resources</goal>
200196
</goals>

graalpy/graalpy-freeze-dependencies-guide/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ if ("true".equals(System.getProperty("no.transitive.dependencies"))) {
1111
graalPy {
1212
packages = setOf("vaderSentiment==3.3.2") //
1313
}
14-
dependencies {
15-
implementation("org.graalvm.python:python:24.2.0")
16-
}
1714
} else {
1815
// The default profile shows the end result: all our transitive
1916
// dependencies are explicitly pinned to a specific version.
@@ -27,9 +24,6 @@ if ("true".equals(System.getProperty("no.transitive.dependencies"))) {
2724
"urllib3==2.2.2"
2825
)
2926
}
30-
dependencies {
31-
implementation("org.graalvm.python:python:24.2.0")
32-
}
3327
}
3428

3529
repositories {

graalpy/graalpy-freeze-dependencies-guide/pom.xml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@
4545
<groupId>org.graalvm.python</groupId>
4646
<artifactId>graalpy-maven-plugin</artifactId>
4747
<version>24.2.0</version>
48+
<configuration>
49+
<packages> <!---->
50+
<package>vaderSentiment==3.3.2</package>
51+
<package>certifi==2024.8.30</package>
52+
<package>charset-normalizer==3.1.0</package>
53+
<package>idna==3.8</package>
54+
<package>requests==2.32.3</package>
55+
<package>urllib3==2.2.2</package>
56+
</packages>
57+
</configuration>
4858
<executions>
4959
<execution>
50-
<configuration>
51-
<packages> <!---->
52-
<package>vaderSentiment==3.3.2</package>
53-
<package>certifi==2024.8.30</package>
54-
<package>charset-normalizer==3.1.0</package>
55-
<package>idna==3.8</package>
56-
<package>requests==2.32.3</package>
57-
<package>urllib3==2.2.2</package>
58-
</packages>
59-
</configuration>
6060
<goals>
6161
<goal>process-graalpy-resources</goal>
6262
</goals>
@@ -81,13 +81,13 @@
8181
<groupId>org.graalvm.python</groupId>
8282
<artifactId>graalpy-maven-plugin</artifactId>
8383
<version>24.2.0</version>
84+
<configuration>
85+
<packages> <!---->
86+
<package>vaderSentiment==3.3.2</package>
87+
</packages>
88+
</configuration>
8489
<executions>
8590
<execution>
86-
<configuration>
87-
<packages> <!---->
88-
<package>vaderSentiment==3.3.2</package>
89-
</packages>
90-
</configuration>
9191
<goals>
9292
<goal>process-graalpy-resources</goal>
9393
</goals>

graalpy/graalpy-javase-guide/README.md

Lines changed: 37 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ gradle init --type java-application --dsl kotlin --test-framework junit-jupiter
5252

5353
## 4.1 Dependency configuration
5454

55-
Add the required dependencies for GraalPy in the `<dependencies>` section of the POM or to the `dependencies` block in the `build.gradle.kts` file.
55+
Add the required dependencies for GraalPy in the `<dependencies>` section of the POM file for Maven.
56+
For Gradle, the GraalPy Gradle plugin that we will add in the next section will inject these
57+
dependencies automatically.
5658

5759
`pom.xml`
5860
```xml
@@ -72,14 +74,6 @@ Add the required dependencies for GraalPy in the `<dependencies>` section of the
7274
</dependencies>
7375
```
7476

75-
`build.gradle.kts`
76-
```kotlin
77-
dependencies {
78-
implementation("org.graalvm.python:python:24.2.0") //
79-
implementation("org.graalvm.python:python-embedding:24.2.0") //
80-
}
81-
```
82-
8377
❶ The `python` dependency is a meta-package that transitively depends on all resources and libraries to run GraalPy.
8478

8579
❷ Note that the `python` package is not a JAR - it is simply a `pom` that declares more dependencies.
@@ -94,37 +88,30 @@ You can use the GraalPy plugins for Maven or Gradle to manage Python packages fo
9488

9589
`pom.xml`
9690
```xml
97-
<build>
98-
<plugins>
99-
<plugin>
100-
<groupId>org.graalvm.python</groupId>
101-
<artifactId>graalpy-maven-plugin</artifactId>
102-
<version>24.2.0</version>
103-
<executions>
104-
<execution>
105-
<configuration>
106-
<packages> <!---->
107-
<package>qrcode==7.4.2</package>
108-
</packages>
109-
<pythonHome> <!---->
110-
<includes>
111-
</includes>
112-
<excludes>
113-
<exclude>.*</exclude>
114-
</excludes>
115-
</pythonHome>
116-
<pythonResourcesDirectory> <!---->
117-
${project.basedir}/python-resources
118-
</pythonResourcesDirectory>
119-
</configuration>
120-
<goals>
121-
<goal>process-graalpy-resources</goal>
122-
</goals>
123-
</execution>
124-
</executions>
125-
</plugin>
126-
</plugins>
127-
</build>
91+
<build>
92+
<plugins>
93+
<plugin>
94+
<groupId>org.graalvm.python</groupId>
95+
<artifactId>graalpy-maven-plugin</artifactId>
96+
<version>24.2.0</version>
97+
<configuration>
98+
<packages> <!---->
99+
<package>qrcode==7.4.2</package>
100+
</packages>
101+
<externalDirectory> <!---->
102+
${project.basedir}/python-resources
103+
</externalDirectory>
104+
</configuration>
105+
<executions>
106+
<execution>
107+
<goals>
108+
<goal>process-graalpy-resources</goal>
109+
</goals>
110+
</execution>
111+
</executions>
112+
</plugin>
113+
</plugins>
114+
</build>
128115
```
129116

130117
`build.gradle.kts`
@@ -136,21 +123,15 @@ plugins {
136123

137124
graalPy {
138125
packages = setOf("qrcode==7.4.2") //
139-
pythonHome { includes = setOf(); excludes = setOf(".*") } //
140-
pythonResourcesDirectory = file("${project.projectDir}/python-resources") //
126+
externalDirectory = file("${project.projectDir}/python-resources") //
141127
}
142128
```
143129

144130
❶ The `packages` section lists all Python packages optionally with [requirement specifiers](https://pip.pypa.io/en/stable/reference/requirement-specifiers/).
145131
In this case, we install the `qrcode` package and pin it to version `7.4.2`.
146132

147-
<a name="external-or-embedded-stdlib-pom"></a>
148-
❷ The GraalPy plugin can copy the Python standard library resources.
149-
This is mainly useful when creating a [GraalVM Native Image](https://www.graalvm.org/latest/reference-manual/native-image/), a use-case that we are not going to cover right now.
150-
We disable this by specifying that we want to exclude all standard library files matching the regular expression `.*`, i.e., all of them, from the included Python home.
151-
152133
<a name="external-or-embedded-python-code-pom"></a>
153-
We can specify where the plugin should place Python files for packages and the standard library that the application will use.
134+
We can specify where the plugin should place Python files for packages that the application will use.
154135
Omit this section if you want to include the Python packages into the Java resources (and, for example, ship them in the Jar).
155136
[Later in the Java code](#external-or-embedded-python-code-java) we can configure the GraalPy runtime to load the package from the filesystem or from resources.
156137

@@ -177,27 +158,21 @@ public class GraalPy {
177158
static VirtualFileSystem vfs;
178159

179160
public static Context createPythonContext(String pythonResourcesDirectory) { //
180-
return GraalPyResources.contextBuilder(Path.of(pythonResourcesDirectory))
181-
.option("python.PythonHome", "") //
182-
.build();
161+
return GraalPyResources.contextBuilder(Path.of(pythonResourcesDirectory)).build();
183162
}
184163

185164
public static Context createPythonContextFromResources() {
186-
if (vfs == null) { //
165+
if (vfs == null) { //
187166
vfs = VirtualFileSystem.newBuilder().allowHostIO(VirtualFileSystem.HostIO.READ).build();
188167
}
189-
return GraalPyResources.contextBuilder(vfs).option("python.PythonHome", "").build();
168+
return GraalPyResources.contextBuilder(vfs).build();
190169
}
191170
}
192171
```
193172

194173
[If we set the `pythonResourcesDirectory` property](#external-or-embedded-python-code-pom) in our build config, we use this factory method to tell GraalPy where that folder is at runtime.
195174

196-
❷ We [excluded](#external-or-embedded-stdlib-pom) all of the Python standard library from the resources in our build config.
197-
The GraalPy VirtualFileSystem is set up to ship even the standard library in the resources.
198-
Since we did not include any standard library, we set the `"python.PythonHome"` option to an empty string.
199-
200-
[If we do not set the `pythonResourcesDirectory` property](#external-or-embedded-python-code-pom), the GraalPy Maven plugin will place the packages inside the Java resources.
175+
[If we do not set the `externalDirectory` property](#external-or-embedded-python-code-pom), the GraalPy Maven or Gradle plugin will place the packages inside the Java resources.
201176
Because Python libraries assume they are running from a filesystem, not a resource location, GraalPy provides the `VirtualFileSystem`, and API to make Java resource locations available to Python code as if it were in the real filesystem.
202177
VirtualFileSystem instances can be configured to allow different levels of through-access to the underlying host filesystem.
203178
In this demo we use the same VirtualFileSystem instance in multiple Python contexts.
@@ -250,11 +225,12 @@ import javax.swing.*;
250225

251226
public class App {
252227
public static void main(String[] args) throws IOException {
253-
if (System.getProperty("graalpy.resources") == null) {
228+
String path = System.getProperty("graalpy.resources");
229+
if (path == null || path.isBlank() || path.equals("null")) {
254230
System.err.println("Please provide 'graalpy.resources' system property.");
255231
System.exit(1);
256232
}
257-
try (var context = GraalPy.createPythonContext(System.getProperty("graalpy.resources"))) { //
233+
try (var context = GraalPy.createPythonContext(path)) { //
258234
QRCode qrCode = context.eval("python", "import qrcode; qrcode").as(QRCode.class); //
259235
IO io = context.eval("python", "import io; io").as(IO.class);
260236

@@ -308,7 +284,7 @@ Run from command line:
308284

309285
```shell
310286
./gradlew assemble
311-
./gradlew run
287+
./gradlew -Dgraalpy.resources=./python-resources run
312288
```
313289

314290
## 6. Next steps

graalpy/graalpy-javase-guide/build.gradle.kts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ plugins {
55

66
graalPy {
77
packages = setOf("qrcode==7.4.2") //
8-
pythonHome { includes = setOf(); excludes = setOf(".*") } //
9-
pythonResourcesDirectory = file("${project.projectDir}/python-resources") //
8+
externalDirectory = file("${project.projectDir}/python-resources") //
109
}
1110

1211
repositories {
@@ -16,11 +15,6 @@ repositories {
1615
}
1716
}
1817

19-
dependencies {
20-
implementation("org.graalvm.python:python:24.2.0") //
21-
implementation("org.graalvm.python:python-embedding:24.2.0") //
22-
}
23-
2418
group = "org.example"
2519
version = "1.0-SNAPSHOT"
2620
description = "javase"

graalpy/graalpy-javase-guide/pom.xml

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,16 @@
3535
<groupId>org.graalvm.python</groupId>
3636
<artifactId>graalpy-maven-plugin</artifactId>
3737
<version>24.2.0</version>
38+
<configuration>
39+
<packages> <!---->
40+
<package>qrcode==7.4.2</package>
41+
</packages>
42+
<externalDirectory> <!---->
43+
${project.basedir}/python-resources
44+
</externalDirectory>
45+
</configuration>
3846
<executions>
3947
<execution>
40-
<configuration>
41-
<packages> <!---->
42-
<package>qrcode==7.4.2</package>
43-
</packages>
44-
<pythonHome> <!---->
45-
<includes>
46-
</includes>
47-
<excludes>
48-
<exclude>.*</exclude>
49-
</excludes>
50-
</pythonHome>
51-
<pythonResourcesDirectory> <!---->
52-
${project.basedir}/python-resources
53-
</pythonResourcesDirectory>
54-
</configuration>
5548
<goals>
5649
<goal>process-graalpy-resources</goal>
5750
</goals>

graalpy/graalpy-javase-guide/src/main/java/org/example/App.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
public class App {
1414
public static void main(String[] args) throws IOException {
15-
if (System.getProperty("graalpy.resources") == null) {
15+
String path = System.getProperty("graalpy.resources");
16+
if (path == null || path.isBlank() || path.equals("null")) {
1617
System.err.println("Please provide 'graalpy.resources' system property.");
1718
System.exit(1);
1819
}
19-
try (var context = GraalPy.createPythonContext(System.getProperty("graalpy.resources"))) { // ①
20+
try (var context = GraalPy.createPythonContext(path)) { // ①
2021
QRCode qrCode = context.eval("python", "import qrcode; qrcode").as(QRCode.class); // ②
2122
IO io = context.eval("python", "import io; io").as(IO.class);
2223

graalpy/graalpy-javase-guide/src/main/java/org/example/GraalPy.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ public class GraalPy {
1515
static VirtualFileSystem vfs;
1616

1717
public static Context createPythonContext(String pythonResourcesDirectory) { // ①
18-
return GraalPyResources.contextBuilder(Path.of(pythonResourcesDirectory))
19-
.option("python.PythonHome", "") // ②
20-
.build();
18+
return GraalPyResources.contextBuilder(Path.of(pythonResourcesDirectory)).build();
2119
}
2220

2321
public static Context createPythonContextFromResources() {
24-
if (vfs == null) { //
22+
if (vfs == null) { //
2523
vfs = VirtualFileSystem.newBuilder().allowHostIO(VirtualFileSystem.HostIO.READ).build();
2624
}
27-
return GraalPyResources.contextBuilder(vfs).option("python.PythonHome", "").build();
25+
return GraalPyResources.contextBuilder(vfs).build();
2826
}
2927
}

0 commit comments

Comments
 (0)