Skip to content

Commit 1958c9a

Browse files
authored
Merge pull request #10 from graalvm/tim/add-gradle-demos
Add gradle plugin to GraalPy demos
2 parents b005b79 + e1bb7bb commit 1958c9a

37 files changed

+1959
-47
lines changed

.github/workflows/graalpy-freeze-dependencies-guide.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ jobs:
2828
shell: bash
2929
run: |
3030
cd graalpy/graalpy-freeze-dependencies-guide
31+
git clean -fdx
3132
./mvnw --no-transfer-progress compile
3233
./mvnw --no-transfer-progress exec:java -Dexec.mainClass=org.example.App | tee /tmp/output
33-
grep darent /tmp/output
34+
grep darent /tmp/output
35+
- name: Build, test, and run 'graalpy-freeze-dependencies-guide' using Gradle
36+
shell: bash
37+
run: |
38+
cd graalpy/graalpy-freeze-dependencies-guide
39+
git clean -fdx
40+
./gradlew build
41+
./gradlew run | tee /tmp/output
42+
grep darent /tmp/output

.github/workflows/graalpy-micronaut-guide.yml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
run:
1818
name: 'graalpy-micronaut-guide'
1919
runs-on: ubuntu-latest
20-
timeout-minutes: 30
20+
timeout-minutes: 45
2121
steps:
2222
- uses: actions/checkout@v4
2323
- uses: graalvm/setup-graalvm@v1
@@ -30,18 +30,40 @@ jobs:
3030
- name: Build, test, and run 'graalpy-micronaut-guide' using Maven
3131
run: |
3232
cd graalpy/graalpy-micronaut-guide
33+
git clean -fdx
3334
./mvnw --no-transfer-progress clean test -Dmicronaut.http.client.read-timeout=1m
3435
./mvnw --no-transfer-progress mn:run &
3536
mnpid="$!"
3637
sleep 30
3738
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
3839
kill $mnpid
40+
- name: Build, test, and run 'graalpy-micronaut-guide' using Gradle
41+
run: |
42+
cd graalpy/graalpy-micronaut-guide
43+
git clean -fdx
44+
./gradlew test -Dmicronaut.http.client.read-timeout=1m
45+
./gradlew run &
46+
mnpid="$!"
47+
sleep 30
48+
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
49+
kill $mnpid
3950
- name: Build and run native 'graalpy-micronaut-guide' using Maven
4051
run: |
4152
cd graalpy/graalpy-micronaut-guide
53+
git clean -fdx
4254
./mvnw --no-transfer-progress clean package -DskipTests -Dpackaging=native-image
4355
./target/graalpy-micronaut &
4456
mnpid="$!"
4557
sleep 20
4658
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
4759
kill $mnpid
60+
- name: Build and run native 'graalpy-micronaut-guide' using Gradle
61+
run: |
62+
cd graalpy/graalpy-micronaut-guide
63+
git clean -fdx
64+
./gradlew nativeCompile
65+
./build/native/nativeCompile/demo &
66+
mnpid="$!"
67+
sleep 20
68+
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
69+
kill $mnpid

.github/workflows/graalpy-spring-boot-guide.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
run:
1818
name: 'graalpy-spring-boot-guide'
1919
runs-on: ubuntu-latest
20-
timeout-minutes: 30
20+
timeout-minutes: 45
2121
steps:
2222
- uses: actions/checkout@v4
2323
- uses: graalvm/setup-graalvm@v1
@@ -30,15 +30,27 @@ jobs:
3030
- name: Build, test, and run 'graalpy-spring-boot-guide' using Maven
3131
run: |
3232
cd graalpy/graalpy-spring-boot-guide
33+
git clean -fdx
3334
./mvnw --no-transfer-progress clean test -Dspring.mvc.async.request-timeout=60000
3435
./mvnw --no-transfer-progress spring-boot:run &
3536
sbpid="$!"
3637
sleep 30
3738
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
3839
kill $sbpid
40+
- name: Build, test, and run 'graalpy-spring-boot-guide' using Gradle
41+
run: |
42+
cd graalpy/graalpy-spring-boot-guide
43+
git clean -fdx
44+
./gradlew test -Dspring.mvc.async.request-timeout=60000
45+
./gradlew bootRun &
46+
sbpid="$!"
47+
sleep 30
48+
curl --fail-with-body --silent --dump-header - -o /dev/null http://localhost:8080/
49+
kill $sbpid
3950
- name: Build and run native 'graalpy-spring-boot-guide' using Maven
4051
run: |
4152
cd graalpy/graalpy-spring-boot-guide
53+
git clean -fdx
4254
./mvnw --no-transfer-progress clean -DskipTests -Pnative native:compile
4355
./target/graalpy-springboot &
4456
sbpid="$!"

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

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ However, you can go right to the [completed example](https://github.com/graalvm/
3636

3737
## 3. Writing the application
3838

39-
You can start with any Maven application that runs on JDK 17 or newer.
40-
We will use a default Maven application [generated](https://maven.apache.org/archetypes/maven-archetype-quickstart/) from an archetype.
39+
You can start with any Maven or Gradle application that runs on JDK 17 or newer.
40+
We will demonstrate on both build systems.
41+
A default Maven application [generated](https://maven.apache.org/archetypes/maven-archetype-quickstart/) from an archetype.
4142

4243
```shell
4344
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes \
@@ -46,15 +47,35 @@ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes \
4647
-Dversion=1.0-SNAPSHOT -DinteractiveMode=false
4748
```
4849

50+
And a default Gradle Java application [generated](https://docs.gradle.org/current/samples/sample_building_java_applications.html#run_the_init_task) with the init task.
51+
52+
```shell
53+
gradle init --type java-application --dsl kotlin --test-framework junit-jupiter \
54+
--package org.example --project-name example --java-version 17 \
55+
--no-split-project --no-incubating
56+
```
57+
4958
## 4. Adding packages
5059

5160
Most Python packages are hosted on [PyPI](https://pypi.org) and can be installed via the `pip` tool.
5261
The Python ecosystem has conventions about the filesystem layout of installed packages that need to be kept in mind when embedding into Java.
53-
You can use the GraalPy plugin to manage Python packages for you.
62+
You can use the GraalPy plugins for Maven or Gradle to manage Python packages for you.
63+
64+
For Maven, add dependency on GraalPy runtime, and configure the GraalPy Maven plugin:
65+
66+
`pom.xml`
67+
```xml
68+
<dependency>
69+
<groupId>org.graalvm.polyglot</groupId>
70+
<artifactId>python</artifactId>
71+
<version>24.1.1</version>
72+
<type>pom</type>
73+
</dependency>
74+
```
5475

5576
`pom.xml`
5677
```xml
57-
<build>
78+
<build>
5879
<plugins>
5980
<plugin>
6081
<groupId>org.graalvm.python</groupId>
@@ -77,25 +98,72 @@ You can use the GraalPy plugin to manage Python packages for you.
7798
</build>
7899
```
79100

101+
For Gradle, add the GraalPy plugin, configure it, and add the dependency on the GraalPy runtime:
102+
103+
`build.gradle.kts`
104+
```kotlin
105+
plugins {
106+
application
107+
id("org.graalvm.python") version "24.1.1"
108+
}
109+
```
110+
111+
`build.gradle.kts`
112+
```kotlin
113+
graalPy {
114+
packages = setOf("vaderSentiment==3.3.2") //
115+
}
116+
117+
dependencies {
118+
implementation("org.graalvm.python:python:24.1.1")
119+
}
120+
```
121+
80122
❶ The `packages` section lists all Python packages optionally with [requirement specifiers](https://pip.pypa.io/en/stable/reference/requirement-specifiers/).
81123
In this case, we install the `vaderSentiment` package and pin it to version `3.3.2`. Because we are not
82-
specifying `<pythonResourcesDirectory>` the Maven plugin will embed the packages into the
124+
specifying `<pythonResourcesDirectory>` the plugins will embed the packages into the
83125
resulting JAR as a standard Java resource.
84126

85127
## 5. Determining and Pinning Transitive Dependencies
86128

87-
Package the application using Maven, this should install all the transitive dependencies
129+
When you package the application, it installs all the transitive dependencies
88130
in a newly created [virtual environment](https://docs.python.org/3/library/venv.html):
89131

90132
```shell
91-
mvn package
133+
./mvnw package
92134
```
93135

94-
If the compilation is successful, one can run the following command to get
95-
versions of all the installed Python packages:
136+
```shell
137+
./gradlew assemble
138+
```
96139

140+
If the compilation is successful, one can run the following command to get versions of all the installed Python packages if you use Maven:
141+
142+
On macOS and Linux:
97143
```shell
98144
./target/classes/org.graalvm.python.vfs/venv/bin/pip3 freeze -l
145+
```
146+
147+
On Windows:
148+
```shell
149+
.\target\classes\org.graalvm.python.vfs\venv\Scripts\pip3.exe freeze -l
150+
```
151+
152+
If you are using Gradle, run the following command:
153+
154+
On macOS and Linux:
155+
```shell
156+
./app/build/generated/graalpy/resources/org.graalvm.python.vfs/venv/bin/pip3 freeze -l
157+
```
158+
159+
On Windows:
160+
```shell
161+
.\app\build\generated\graalpy\resources\org.graalvm.python.vfs\venv\Scripts\pip3.exe freeze -l
162+
```
163+
164+
The output will look something like this:
165+
166+
```
99167
certifi==2024.8.30
100168
charset-normalizer==3.1.0
101169
idna==3.8
@@ -104,8 +172,8 @@ urllib3==2.2.2
104172
vaderSentiment==3.3.2
105173
```
106174

107-
Copy and paste the package names and versions to the `pom.xml`, and wrap
108-
them in `<package>` xml tag:
175+
Copy and paste the package names and versions.
176+
If you use Maven, paste them in the `pom.xml` section of the packages and wrap them in `<package>` xml tag:
109177

110178
`pom.xml`
111179
```xml
@@ -137,6 +205,20 @@ them in `<package>` xml tag:
137205
</build>
138206
```
139207

208+
For Gradle, paste them into the packages block:
209+
210+
`build.gradle.kts`
211+
```kotlin
212+
packages = setOf(
213+
"vaderSentiment==3.3.2",
214+
"certifi==2024.8.30",
215+
"charset-normalizer==3.1.0",
216+
"idna==3.8",
217+
"requests==2.32.3",
218+
"urllib3==2.2.2"
219+
)
220+
```
221+
140222
Note: one can use other Python tools, such as `pipdeptree` to generate the following
141223
dependency tree, where we can also see the version ranges.
142224

@@ -151,7 +233,7 @@ vaderSentiment==3.3.2
151233

152234
*Warning:
153235
Is it not recommended to manually alter the virtual environment.
154-
Any changes will be overridden by the GraalPy Maven plugin.*
236+
Any changes will be overridden by the GraalPy build plugins.*
155237

156238
## 8. Next steps
157239

@@ -165,4 +247,4 @@ Any changes will be overridden by the GraalPy Maven plugin.*
165247

166248
- Learn more about the GraalPy [Maven plugin](https://www.graalvm.org/latest/reference-manual/python/Embedding-Build-Tools/)
167249
- Learn more about the Polyglot API for [embedding languages](https://www.graalvm.org/latest/reference-manual/embed-languages/)
168-
- Explore in depth with GraalPy [reference manual](https://www.graalvm.org/latest/reference-manual/python/)
250+
- Explore in depth with GraalPy [reference manual](https://www.graalvm.org/latest/reference-manual/python/)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
plugins {
2+
application
3+
id("org.graalvm.python") version "24.1.1"
4+
}
5+
6+
// To make the paths of this reference solution (without 'app' subdirectory for sources)
7+
// and "gradle init ..." based solution (with sources in 'app') the same for README.md
8+
layout.buildDirectory.set(layout.projectDirectory.dir("app/build"))
9+
10+
if ("true".equals(System.getProperty("no.transitive.dependencies"))) {
11+
graalPy {
12+
packages = setOf("vaderSentiment==3.3.2") //
13+
}
14+
dependencies {
15+
implementation("org.graalvm.python:python:24.1.1")
16+
}
17+
} else {
18+
// The default profile shows the end result: all our transitive
19+
// dependencies are explicitly pinned to a specific version.
20+
graalPy {
21+
packages = setOf(
22+
"vaderSentiment==3.3.2",
23+
"certifi==2024.8.30",
24+
"charset-normalizer==3.1.0",
25+
"idna==3.8",
26+
"requests==2.32.3",
27+
"urllib3==2.2.2"
28+
)
29+
}
30+
dependencies {
31+
implementation("org.graalvm.python:python:24.1.1")
32+
}
33+
}
34+
35+
repositories {
36+
mavenLocal()
37+
maven {
38+
url = uri("https://repo.maven.apache.org/maven2/")
39+
}
40+
}
41+
42+
// This dependency is necessary only for the example Java code, not for building and running pip freeze:
43+
dependencies.add("implementation", "org.graalvm.python:python-embedding:24.1.1")
44+
45+
group = "org.example"
46+
version = "1.0-SNAPSHOT"
47+
description = "javase"
48+
java.sourceCompatibility = JavaVersion.VERSION_17
49+
50+
application {
51+
mainClass = "org.example.App"
52+
applicationDefaultJvmArgs = listOf("-Dgraalpy.resources=" + System.getProperty("graalpy.resources"))
53+
}
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)