|
| 1 | +# Java applications |
| 2 | + |
| 3 | +Distributing a Java application for Linux while reaching the widest possible audience is complicated. Typically, the user has to make sure the JRE/SDK version and their environment are configured correctly. When a Linux distribution changes the delivered JRE, this can be problematic for applications. |
| 4 | + |
| 5 | +Snaps solve these problems and ensure the correct JRE is shipped alongside the application at all times. |
| 6 | + |
| 7 | +## Why are snaps good for Java projects? |
| 8 | + |
| 9 | +* **Snaps are easy to discover and install** |
| 10 | + Millions of users can browse and install snaps graphically in the Snap Store or from the command-line. |
| 11 | +* **Snaps install and run the same across Linux** |
| 12 | + They bundle the exact version of whatever is required, along with all of your app's dependencies, be they Java modules or system libraries. |
| 13 | +* **Snaps automatically update to the latest version** |
| 14 | + Four times a day, users' systems will check for new versions and upgrade in the background. |
| 15 | +* **Upgrades are not disruptive** |
| 16 | + Because upgrades are not in-place, users can keep your app open as it's upgraded in the background. |
| 17 | +* **Upgrades are safe** |
| 18 | + If your app fails to upgrade, users automatically roll back to the previous revision. |
| 19 | + |
| 20 | +## Build a snap in 20 minutes |
| 21 | + |
| 22 | +Ready to get started? By the end of this guide, you'll understand how to make a snap of your Java app that can be published in the [Snap Store](https://snapcraft.io/store), showcasing it to millions of Linux users. |
| 23 | + |
| 24 | +> :information_source: For a brief overview of the snap creation process, including how to install *snapcraft* and how it's used, see [Snapcraft overview](/t/snapcraft-overview/8940). For a more comprehensive breakdown of the steps involved, take a look at [Creating a snap](/t/creating-a-snap/6799). |
| 25 | +
|
| 26 | +## Getting started |
| 27 | + |
| 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. |
| 29 | + |
| 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. |
| 31 | + |
| 32 | +[details=snapcraft.yaml for Freeplane] |
| 33 | + |
| 34 | +```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 |
| 39 | +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 |
| 56 | +confinement: strict |
| 57 | + |
| 58 | +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 |
| 66 | + plugs: |
| 67 | + - home |
| 68 | + - network |
| 69 | + - cups-control |
| 70 | + |
| 71 | +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/ |
| 82 | + build-packages: |
| 83 | + - unzip |
| 84 | +``` |
| 85 | +
|
| 86 | +[/details] |
| 87 | +
|
| 88 | +## Metadata |
| 89 | +
|
| 90 | +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. |
| 91 | + |
| 92 | +```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 |
| 97 | +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. |
| 112 | +``` |
| 113 | + |
| 114 | +## Base |
| 115 | + |
| 116 | +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: |
| 117 | + |
| 118 | +```yaml |
| 119 | +base: core18 |
| 120 | +``` |
| 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/). |
| 122 | + |
| 123 | +See [Base snaps](/t/base-snaps/11198) for more details. |
| 124 | + |
| 125 | +## Security model |
| 126 | + |
| 127 | +The next section describes the level of confinement applied to your app. |
| 128 | + |
| 129 | +```yaml |
| 130 | +confinement: devmode |
| 131 | +``` |
| 132 | + |
| 133 | +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. |
| 134 | + |
| 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`. |
| 136 | + |
| 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. |
| 138 | + |
| 139 | +## Apps |
| 140 | + |
| 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. |
| 142 | + |
| 143 | +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. |
| 144 | + |
| 145 | +```yaml |
| 146 | +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 |
| 154 | + plugs: |
| 155 | + - home |
| 156 | + - network |
| 157 | + - cups-control |
| 158 | +``` |
| 159 | + |
| 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 | + |
| 162 | +## Parts |
| 163 | + |
| 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. |
| 165 | + |
| 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. |
| 167 | + |
| 168 | +```yaml |
| 169 | +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/ |
| 180 | + build-packages: |
| 181 | + - unzip |
| 182 | +``` |
| 183 | + |
| 184 | +For more details on Gradle-specific metadata, see [The Gradle plugin](/t/the-gradle-plugin/5390). |
| 185 | + |
| 186 | +## Building the snap |
| 187 | + |
| 188 | +You can download the example repository with the following command: |
| 189 | + |
| 190 | +``` |
| 191 | +$ git clone https://github.com/galgalesh/freeplane-1 |
| 192 | +``` |
| 193 | + |
| 194 | +After you’ve created the snapcraft.yaml, you can build the snap by simply executing the snapcraft command in the project directory: |
| 195 | + |
| 196 | +```bash |
| 197 | +$ snapcraft |
| 198 | +``` |
| 199 | + |
| 200 | +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: |
| 201 | + |
| 202 | +```bash |
| 203 | +$ sudo snap install freeplane_*.snap --devmode --dangerous |
| 204 | +``` |
| 205 | + |
| 206 | +You can then try it out: |
| 207 | + |
| 208 | +```bash |
| 209 | +$ freeplane |
| 210 | +``` |
| 211 | + |
| 212 | +Removing the snap is simple too: |
| 213 | + |
| 214 | +```bash |
| 215 | +$ sudo snap remove freeplane |
| 216 | +``` |
| 217 | + |
| 218 | +## Publishing your snap |
| 219 | + |
| 220 | +To share your snaps you need to publish them in the Snap Store. First, create an account on [the dashboard](https://dashboard.snapcraft.io/dev/account/). Here you can customise how your snaps are presented, review your uploads and control publishing. |
| 221 | + |
| 222 | +You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps. |
| 223 | + |
| 224 | +Make sure the `snapcraft` command is authenticated using the email address attached to your Snap Store account: |
| 225 | + |
| 226 | +```bash |
| 227 | +$ snapcraft login |
| 228 | +``` |
| 229 | + |
| 230 | +## Reserve a name for your snap |
| 231 | + |
| 232 | +You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on [dashboard.snapcraft.io](https://dashboard.snapcraft.io/register-snap/), or by running the following command: |
| 233 | + |
| 234 | +```bash |
| 235 | +$ snapcraft register myjavasnap |
| 236 | +``` |
| 237 | + |
| 238 | +Be sure to update the `name:` in your `snapcraft.yaml` to match this registered name, then run `snapcraft` again. |
| 239 | + |
| 240 | +## Upload your snap |
| 241 | + |
| 242 | +Use snapcraft to push the snap to the Snap Store. |
| 243 | + |
| 244 | +```bash |
| 245 | +$ snapcraft upload --release=edge myjavasnap_*.snap |
| 246 | +``` |
| 247 | + |
| 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. |
| 249 | + |
| 250 | +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