A quickstart guide for the java build tool maven
Maven is a build tool that is well supported for Free and Open Source projects. It has a centralised software hosting repository called Maven Central.
FOSS Galaxy is able to publish to Maven Central under the com.fossgalaxy
namespace.
Installation
Maven and Java should be packaged in most major distributions.
Fedora
For Fedora, OpenJDK is published as java-$version-openjdk
where $version
is
the version being used. At time of writing that is either 1.8.0, 11, or latest.
sudo dnf install -y java-11-openjdk java-11-openjdk-{devel,src,javadoc}
sudo dnf install maven
The packages pulled down in this example are:
- java-11-openjdk
- The Java runtime (Java itself)
- java-11-openjdk-devel
- Development files (compiler, linker, documentation tool, etc…)
- java-11-openjdk-src
- Source code for Java 11 (can be used for IDE integration), optional
- java-11-openjdk-javadoc
- Documentation for Java 11 (can be used for IDE integration), optional
Depending on how the project works, you might also need the -jmods
package.
This was introduced in Java 9 and doesn’t have much usage inside our projects
at present.
Usage
Maven has good support in the most popular Java IDEs available (intellij and eclipse). To use this functionality open the Maven project in your IDE of choice and you should be presented with the option to import the project settings.
Intellij
When opening a Maven project in Intellij, you must select the pom.xml file, not the folder. If you select the folder intellij will create it’s own project files and won’t use Maven.
If prompted to do so, enable auto import as this will keep your pom.xml
and
intellij project in sync.
Eclipse
Eclipse has maven integration by default. You can open a maven project by
using the import...
option in the file menu and selecting maven project from
the list of project types.
From the command line
You can also use Maven from the command line to build software. An IDE is recommended for actually doing development.
$ cd ~/path/to/project
$ mvn package
The maven package command will generate a jar file in the target/
directory.
You can also run unit tests using the mvn test
command.
Dependency Management
Maven maintains it’s own repository of dependencies. Some of these might be
packaged in your distribution, but in my experiance most will not be. Maven
will cache downloads in ~/.m2/
meaning that the dependencies will only be
downloaded once.
Dependencies tend to be numerious, but relatively small (a few hundred kb) and so are usually fast to download. Maven plugins (tools which are used as part of the build process) are downloaded in the same way.
Almost all of our Java software is published on Maven Central, as it the
dependencies. Because of this, the group and artifact IDs must be approved
before being published (we can publish anything under com.fossgalaxy.*
).
Maven will also download source and javadocs for dependencies if asked to do so. This can be helpful for debugging.