Useful Maven Commands

IDE vs Maven

The configuration of a project in an IDE (Eclipse, IntelliJ, ...) is always machine and developer specific. It should not be committed to your git repository (and present in your .gitignore file).

The correct way to do things is to describe your project configuration in a build tool (for instance Maven) which allows:

  • for anybody with the build tool to compile your project
  • to run automated tests from scripts
  • to import your work in any IDE (typically with Import -> Maven Project)

Installing Maven

You can use the following command to install maven on a linux machine on which you do not have admin rights (including on INSA computers):

mkdir -p ~/bin  # create a bin/ directory in your home
cd ~/bin  # jump to it
wget https://dlcdn.apache.org/maven/maven-3/3.9.5/binaries/apache-maven-3.9.5-bin.tar.gz -O maven.tar.gz  # download maven
tar xf maven.tar.gz  # decompress it
echo 'export PATH=~/bin/apache-maven-3.9.5/bin:$PATH' >> ~/.bashrc  # add mvn's directory to the PATH
source ~/.bashrc  # reload terminal configuration

You can test that it works with: mvn --version

Compilation

# Compile all classes in the project
mvn compile
# compiles all classes of the project and produces a single jar file (a jar is an archive containing several classes)
# The jar file is placed in the target directory
mvn package

Important note: if these two commands do not work on your project, it will be essentially useless to anybody that tries to use it.

Running

Running a java program requires knowing two things:

  • the location of all compiled classes. For this, java uses the concept of the classpath: a list of directories and files where java will look for the compiled classes.
  • the name of class to that will be executed. TO be executed, the class must contain a public static void main(String[] args) method.

You can run a Java program with the java command by specifying the classpath (here target/chatsystem-XXX.jar) and the class to run (here the class Main in the chatsystem package):

java -cp target/chatsystem-XXX.jar chatsystem.Main

Another and more convenient option is to use directly your build tool to run the program: you only have to specify the main class and it will figure out the classpath itself (including all classes from your project and all classes from your dependencies).

mvn exec:java -Dexec.mainClass="chatsystem.Main" 

Distribution

To distribute your application, you would ideally like to provide a single executable that will contain everything necessary to run your application: your code, your dependencies and possibly some resource files (logo, ...).

With Maven, we saw that you can build a .jar containing all your code with maven package. However, this jar will not contain your dependencies that hence must be provided independently.

The solution to this problem is to build a fat jar: a jar file that contains all your code and all your dependencies. Following the tutorial here, you can configure maven so that maven package builds both a normal jar and a fat jar.

To distribute your application, you can then only provide the fat jar that the user will be able to run with java -jar path-to/fat-jar.jar.