manparvesh

Playing around with maven

Date published: 27-Jan-2018

java
maven

Today I played around with maven while working on a personal project and it turned out to be a great decision.

What I learned

What I learned in the process:

Using maven plugins

Maven plugins can reduce a lot of work when it comes to automating things. I used maven-assembly-plugin to create an executable jar file, which includes all the resources and dependencies of the project as well. In my project, I added this part to my plugins:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>path.to.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

This will generate an executable jar file in the target folder for you. But, it can be tricky if you are using a local jar file as a dependency.

Using local maven repository

I also learned how to use an existing jar file as a local maven repository.

  • Run this command:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file -Dfile=<jarfilepath> -DgroupId=<groupId> -DartifactId=<artifactId> -Dversion=<version> -Dpackaging=jar -DlocalRepositoryPath=<localRepositoryPath>
  • This will create a local repository in the specified location, which you can then add to the pom file directly. This method is preferred way more than using the system scope in maven.

Extending a Windows application to Unix-based systems

I was planning to port my Java application built using SWT to other operating systems for a long time. After starting to use Maven as the package manager for my application (I am working on an old project when I wasn’t aware of Maven), I realized how easy that made a developer’s life. Earlier I was importing SWT normally, like this:

<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>

Now, for building and running the same app on Linux, I just had to add a new profile to maven, and it would do everything by itself! The updated pom file looks like this:

<profiles>
<profile>
<id>Windows-config</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>Linux-config</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
</profile>
</profiles>

Looks cool, right? I now understand why maven is so widely accepted and used. I would say that porting to maven was one of the best things I did for my project.

prev