Skip to content

Commit 715661c

Browse files
authored
Merge pull request #45 from adityasamant25/issue-5
Snapcraft: Update Java example to use core22
2 parents 29964da + 7b3dd41 commit 715661c

File tree

1 file changed

+126
-98
lines changed

1 file changed

+126
-98
lines changed

snapcraft/how-to/java-applications/java-applications.md

Lines changed: 126 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,62 +25,80 @@ Ready to get started? By the end of this guide, you'll understand how to make a
2525
2626
## Getting started
2727

28-
Snaps are defined in a single YAML file placed in the root folder of your project. The following example shows the entire *snapcraft.yaml* file for an existing project, [Freeplane](https://github.com/snapcraft-docs/freeplane). Don't worry, we’ll break this down.
28+
Snaps are defined in a single YAML file placed either in the root folder of your project or in a directory named `snap`. The following example shows the entire *snapcraft.yaml* file for an existing project, [Cal - The Console Calendar Generator](https://github.com/frossm/cal).
2929

30-
Using a few lines of yaml and the snapcraft tool, a Java application, it's dependencies and the correct JRE can be packaged as a snap. We’ll break this down.
30+
Using a few lines of yaml and the snapcraft tool, a Java application, its dependencies and the correct JRE can be packaged as a snap. Don't worry, we’ll break this down.
3131

32-
[details=snapcraft.yaml for Freeplane]
32+
[details=snapcraft.yaml for Cal]
3333

3434
```yaml
35-
name: freeplane
36-
title: Freeplane
37-
version: '1.8.1'
38-
summary: A free tool to structure and organise your information with mind mapping
35+
name: fcal
36+
version: '2.7.1'
37+
summary: Command line calendar display
3938
description: |
40-
Freeplane is a free and open source software application that supports
41-
thinking, sharing information and getting things done at work, in school
42-
and at home. The core of the software is tools for mind mapping (also known
43-
as concept mapping or information mapping) and using mapped information.
44-
45-
Occupying the middle ground between an editor and a diagramming tool,
46-
Freeplane allows the user to add content as quickly and naturally as they
47-
would in a text editor, yet producing structured content that can be
48-
manipulated as easily as a diagram.
49-
50-
Features include ordering ideas in nodes and freely positionable nodes,
51-
connecting nodes, automatic/conditional styles, scripting, add-ons, LaTeX,
52-
search/filtering, different export features, printing, password protection
53-
of nodes/maps and more.
54-
55-
base: core18
39+
fCal is a command line calendar utility. It will display a
40+
calendar on the command line with any month/year requested. Defaults
41+
to the current year. fCal can also display local holidays. See help.
42+
43+
grade: stable
5644
confinement: strict
45+
base: core22
46+
47+
title: fCal
48+
website: https://github.com/frossm/cal
49+
issues: https://github.com/frossm/cal/issues
50+
license: MIT
51+
52+
# Enable faster LZO compression
53+
compression: lzo
54+
55+
# Ignore useless library warnings
56+
lint:
57+
ignore:
58+
- library
5759

5860
apps:
59-
freeplane:
60-
extensions:
61-
- gnome-3-28
62-
command: freeplane-$SNAPCRAFT_PROJECT_VERSION/freeplane.sh
63-
environment:
64-
JAVA_HOME: $SNAP/usr/lib/jvm/java-11-openjdk-amd64
65-
PATH: $JAVA_HOME/jre/bin:$PATH
61+
fcal:
62+
command: cal-wrapper
6663
plugs:
67-
- home
6864
- network
69-
- cups-control
7065

7166
parts:
72-
freeplane:
73-
plugin: gradle
74-
source: https://github.com/freeplane/freeplane.git
75-
source-tag: release-$SNAPCRAFT_PROJECT_VERSION
76-
gradle-version: '5.1.1'
77-
gradle-output-dir: BIN
78-
gradle-options: [binZip, -xtest, -xcreateGitTag]
79-
override-build: |
80-
snapcraftctl build
81-
unzip -o DIST/freeplane_bin-*.zip -d $SNAPCRAFT_PART_INSTALL/
67+
wrapper:
68+
plugin: dump
69+
source: snap/local
70+
source-type: local
71+
72+
library:
73+
plugin: maven
74+
source: https://github.com/frossm/library.git
75+
source-type: git
76+
source-tag: 'v2023.12.03'
77+
maven-parameters:
78+
- install
79+
8280
build-packages:
83-
- unzip
81+
- maven
82+
- openjdk-11-jdk-headless
83+
84+
cal:
85+
plugin: maven
86+
source: https://github.com/frossm/cal.git
87+
source-branch: master
88+
source-type: git
89+
after:
90+
- library
91+
92+
build-packages:
93+
- maven
94+
- openjdk-11-jdk-headless
95+
96+
stage-packages:
97+
- openjdk-11-jre-headless
98+
99+
override-prime: |
100+
snapcraftctl prime
101+
rm -vf usr/lib/jvm/java-11-openjdk-*/lib/security/blacklisted.certs
84102
```
85103
86104
[/details]
@@ -90,35 +108,23 @@ parts:
90108
The `snapcraft.yaml` file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md. This data is used in the presentation of your app in the Snap Store.
91109

92110
```yaml
93-
name: freeplane
94-
title: Freeplane
95-
version: '1.8.1'
96-
summary: A free tool to structure and organise your information with mind mapping
111+
name: fcal
112+
version: '2.7.1'
113+
summary: Command line calendar display
97114
description: |
98-
Freeplane is a free and open source software application that supports
99-
thinking, sharing information and getting things done at work, in school
100-
and at home. The core of the software is tools for mind mapping (also known
101-
as concept mapping or information mapping) and using mapped information.
102-
103-
Occupying the middle ground between an editor and a diagramming tool,
104-
Freeplane allows the user to add content as quickly and naturally as they
105-
would in a text editor, yet producing structured content that can be
106-
manipulated as easily as a diagram.
107-
108-
Features include ordering ideas in nodes and freely positionable nodes,
109-
connecting nodes, automatic/conditional styles, scripting, add-ons, LaTeX,
110-
search/filtering, different export features, printing, password protection
111-
of nodes/maps and more.
115+
fCal is a command line calendar utility. It will display a
116+
calendar on the command line with any month/year requested. Defaults
117+
to the current year. fCal can also display local holidays. See help.
112118
```
113119

114120
## Base
115121

116122
The base keyword declares which _base snap_ to use with your project. A base snap is a special kind of snap that provides a run-time environment alongside a minimal set of libraries that are common to most applications:
117123

118124
```yaml
119-
base: core18
125+
base: core22
120126
```
121-
As used above, [`core18`](https://snapcraft.io/core18) is the current standard base for snap building and is based on [Ubuntu 18.04 LTS](http://releases.ubuntu.com/18.04/).
127+
As used above, [`core22`](https://snapcraft.io/core22) is the current standard base for snap building and is based on [Ubuntu 22.04 LTS](https://releases.ubuntu.com/22.04/).
122128

123129
See [Base snaps](/t/base-snaps/11198) for more details.
124130

@@ -132,66 +138,88 @@ confinement: devmode
132138

133139
Snaps are containerised to ensure more predictable application behaviour and greater security. Unlike other container systems, the shape of this confinement can be changed through a set of interfaces. These are declarations that tell the system to give permission for a specific task, such as accessing a webcam or binding to a network port.
134140

135-
It's best to start a snap with the confinement in warning mode, rather than strictly applied. This is indicated through the `devmode` keyword. When a snap is in devmode, runtime confinement violations will be allowed but reported. These can be reviewed by running `journalctl -xe`.
141+
It's best to start a snap with the confinement in warning mode, rather than strictly applied. This is indicated through the `devmode` keyword. When a snap is in `devmode`, runtime confinement violations will be allowed but reported. These can be reviewed by running `journalctl -xe`.
136142

137-
Because devmode is only intended for development, snaps must be set to strict confinement before they can be published as "stable" in the Snap Store. Once an app is working well in devmode, you can review confinement violations, add appropriate interfaces, and switch to strict confinement.
143+
Because `devmode` is only intended for development, snaps must be set to `strict` confinement before they can be published as "stable" in the Snap Store. Once an app is working well in `devmode`, you can review confinement violations, add appropriate interfaces, and switch to `strict` confinement.
138144

139145
## Apps
140146

141-
Apps are the commands and services exposed to end users. If your command name matches the snap `name`, users will be able run the command directly. If the names differ, then apps are prefixed with the snap `name` (`freeplane.command-name`, for example). This is to avoid conflicting with apps defined by other installed snaps.
147+
Apps are the commands and services exposed to end users. If your command name matches the snap `name`, users will be able to run the command directly. If the names differ, then apps are prefixed with the snap `name` (`fcal.command-name`, for example). This is to avoid conflicting with apps defined by other installed snaps.
142148

143149
If you don’t want your command prefixed you can request an alias for it on the [Snapcraft forum](https://forum.snapcraft.io/t/process-for-reviewing-aliases-auto-connections-and-track-requests/455). These are set up automatically when your snap is installed from the Snap Store.
144150

145151
```yaml
146152
apps:
147-
freeplane:
148-
extensions:
149-
- gnome-3-28
150-
command: freeplane-$SNAPCRAFT_PROJECT_VERSION/freeplane.sh
151-
environment:
152-
JAVA_HOME: $SNAP/usr/lib/jvm/java-11-openjdk-amd64
153-
PATH: $JAVA_HOME/jre/bin:$PATH
153+
fcal:
154+
command: cal-wrapper
154155
plugs:
155-
- home
156156
- network
157-
- cups-control
158157
```
159158

160-
Since Freeplane is a desktop application, we use the [`gnome-3-28` extension](/t/the-gnome-3-28-extension/13485) to configure and setup the desktop integration and permissions for the snap. Although Freeplane is a Java Swing application which doesn't need acces to GTK or GNOME, the GNOME extension is still useful because it sets up many toolkit-independent libraries and functionality such as mouse cursor themes, locales and the XDG runtime environment.
161-
162159
## Parts
163160

164-
Parts define how to build your app. Parts can be anything: programs, libraries, or other assets needed to create and run your application. In this case we have only one: the Freeplane source. In other cases these can point to local directories, remote git repositories or other revision control systems.
161+
Parts define how to build your app. Parts can be anything: programs, libraries, or other assets needed to create and run your application. These can point to local directories, remote git repositories or other revision control systems.
162+
In this case we have the following sources:
163+
* `cal`: A remote git repository that contains the source code for the `Cal` application.
164+
* `library`: A remote git repository that contains the source code for dependent libraries.
165+
* `wrapper`: Path to a local directory that contains a wrapper script to run the application in a bash shell.
166+
167+
For details on metadata specific to snapcraft parts, see [Snapcraft parts metadata](/t/snapcraft-parts-metadata/8336).
168+
169+
The `maven` plugin can build the application using standard parameters. This plugin requires a `pom.xml` in the root of the source tree. For more details on Maven-specific metadata, see [The Maven plugin](/t/the-maven-plugin/4282).
170+
171+
The `dump` plugin dumps the content from a specified source. For more details on metadata specific to the `dump` plugin, see [The Dump plugin](/t/the-dump-plugin/8007).
172+
165173

166-
The gradle plugin can build the application using standard parameters. In this case, however, the default build logic of the gradle plugin is not sufficient. While gradle by default build the `jar` target, Freeplane has a `binZip` target which build a handy zip file. We use `gradle-options` to specify that we want to build the `binZip` target and use an [`override-build` scriptlet to add additional logic](/t/scriptlets/4892#heading--overriding-the-build-step) to the build step to extract the zip in the directory which will later get added to the final snap. See the [parts lifecycle docs](/t/parts-lifecycle/12231#heading--parts-directories) for more information on these directories. Since we use the `unzip` command in the build script, we specify it in `build-packages` so it is installed before the build script runs. Finally, we use the `gradle-output-dir` key to point the snapcraft plugin to the location of the built `jar` files for Freeplane.
167174

168175
```yaml
169176
parts:
170-
freeplane:
171-
plugin: gradle
172-
source: https://github.com/freeplane/freeplane.git
173-
source-tag: release-$SNAPCRAFT_PROJECT_VERSION
174-
gradle-version: '5.1.1'
175-
gradle-output-dir: BIN
176-
gradle-options: [binZip, -xtest, -xcreateGitTag]
177-
override-build: |
178-
snapcraftctl build
179-
unzip -o DIST/freeplane_bin-*.zip -d $SNAPCRAFT_PART_INSTALL/
177+
wrapper:
178+
plugin: dump
179+
source: snap/local
180+
source-type: local
181+
182+
library:
183+
plugin: maven
184+
source: https://github.com/frossm/library.git
185+
source-type: git
186+
source-tag: 'v2023.12.03'
187+
maven-parameters:
188+
- install
189+
180190
build-packages:
181-
- unzip
182-
```
191+
- maven
192+
- openjdk-11-jdk-headless
183193
184-
For more details on Gradle-specific metadata, see [The Gradle plugin](/t/the-gradle-plugin/5390).
194+
cal:
195+
plugin: maven
196+
source: https://github.com/frossm/cal.git
197+
source-branch: master
198+
source-type: git
199+
after:
200+
- library
201+
202+
build-packages:
203+
- maven
204+
- openjdk-11-jdk-headless
205+
206+
stage-packages:
207+
- openjdk-11-jre-headless
208+
209+
override-prime: |
210+
snapcraftctl prime
211+
rm -vf usr/lib/jvm/java-11-openjdk-*/lib/security/blacklisted.certs
212+
```
185213

186214
## Building the snap
187215

188216
You can download the example repository with the following command:
189217

190218
```
191-
$ git clone https://github.com/galgalesh/freeplane-1
219+
$ git clone https://github.com/frossm/cal.git
192220
```
193221

194-
After you’ve created the snapcraft.yaml, you can build the snap by simply executing the snapcraft command in the project directory:
222+
After you’ve reviewed the `snapcraft.yaml`, you can build the snap by simply executing the snapcraft command in the project directory:
195223

196224
```bash
197225
$ snapcraft
@@ -200,19 +228,19 @@ $ snapcraft
200228
The resulting snap can be installed locally. This requires the `--dangerous` flag because the snap is not signed by the Snap Store. The `--devmode` flag acknowledges that you are installing an unconfined application:
201229

202230
```bash
203-
$ sudo snap install freeplane_*.snap --devmode --dangerous
231+
$ sudo snap install fcal_*.snap --devmode --dangerous
204232
```
205233

206234
You can then try it out:
207235

208236
```bash
209-
$ freeplane
237+
$ fcal
210238
```
211239

212240
Removing the snap is simple too:
213241

214242
```bash
215-
$ sudo snap remove freeplane
243+
$ sudo snap remove fcal
216244
```
217245

218246
## Publishing your snap
@@ -245,6 +273,6 @@ Use snapcraft to push the snap to the Snap Store.
245273
$ snapcraft upload --release=edge myjavasnap_*.snap
246274
```
247275

248-
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and [turn on automatic builds](https://build.snapcraft.io) so any further commits automatically get released to edge, without requiring you to manually build locally.
276+
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo. You can optionally enable [Build from GitHub](/t/build-from-github/26004) so any further commits automatically get released to edge, without requiring you to manually build locally.
249277

250278
Congratulations! You've just built and published your first Java snap. For a more in-depth overview of the snap building process, see [Creating a snap](/t/creating-a-snap/6799).

0 commit comments

Comments
 (0)